07.4OR and ||

In Excel
ExcelOR
Match

Sometimes, a single condition met is enough for you. OR and || answer TRUE as soon as one of the conditions answers TRUE. They complete AND and &&, and they hold a trap when you mix them.

Your management, for example, follows two zones, the United States and Canada. A customer is in the United States if they are in New York or in Chicago. Just one of the two conditions is enough.

07.4.1One condition out of two is enough

OR and || answer TRUE if at least one of the two conditions answers TRUE.

OR( <condition 1>, <condition 2> )
<condition 1> || <condition 2>

Let us add to the Customers table the zone of each customer:

Zone = IF( Customers[City] = "New York" || Customers[City] = "Chicago", "United States", "Canada" )
Power BI accepts this DAX forno, Measureyes, Calculated columnno, Calculated tableno, Virtual table
Zone = IF( Customers[City] = "New York" || Customers[City] = "Chicago", "United States", "Canada" )
Table Customers
Customer IDCityZone
C001New YorkUnited States
C002New YorkUnited States
C003New YorkUnited States
C004ChicagoUnited States
C005ChicagoUnited States
C006TorontoCanada
C007TorontoCanada
Table: Customers (7 rows)

This is a reproduction of the Power BI interface for teaching purposes. It differs from the real interface.

The customers in New York and those in Chicago answer TRUE to one of the two conditions. Only the two customers in Toronto answer FALSE to both.

The same column can also be written with OR, which takes the two conditions between its parentheses, in place of ||:

Zone = IF( OR( Customers[City] = "New York", Customers[City] = "Chicago" ), "United States", "Canada" )
Power BI accepts this DAX forno, Measureyes, Calculated columnno, Calculated tableno, Virtual table

The result is the same as with ||, five customers in “United States” and two in “Canada.” Choose the one that reads best to you.

Watch out!

OR accepts only two conditions. With three conditions or more, chain || instead, or place one OR inside another OR.

Practice

Each exercise is solved with the function you just discovered. Two exercises per function, a direct use and a combined use, from easiest to hardest, on data other than the course data. Pick the right formula out of five.

  1. Direct useCalculated columnDifficulty

    A sports club gives a reduced rate to people under 18 and to students. Add to the “Members” table the calculated column “Reduced Rate,” which shows “Yes” or “No.”

    Members
    NameAgeStatus
    Inès16Employee
    Hugo22Student
    Jade40Employee

    Which formula should you write for “Reduced Rate”?

  2. Combined useMeasureDifficulty

    A weather station is on alert when it is at least 35 degrees, or when the wind reaches 90 km/h. The “Degrees” and “Wind” columns of the “Readings” table give these values for each day. Write the “Alert Days” measure, shown in a card, the number of days when one of the two conditions is met.

    Readings
    DayDegreesWind
    8/10/20263620
    8/11/20262595
    8/12/20262840

07.4.2The four possible cases

“Or” answers FALSE in only one case, when both conditions answer FALSE:

The answer of “or”
The answer of “or”
Condition 1Condition 2Condition 1 || Condition 2
TRUETRUETRUE
TRUEFALSETRUE
FALSETRUETRUE
FALSEFALSEFALSE

07.4.3IN, for several values of the same column

IN, seen in “Six basic functions,” can also be written in IF. When all the conditions apply to the same column, IN saves you from repeating it. The column is written once, followed by IN and the values between curly braces:

Zone = IF( Customers[City] IN { "New York", "Chicago" }, "United States", "Canada" )
Power BI accepts this DAX forno, Measureyes, Calculated columnno, Calculated tableno, Virtual table
Zone = IF( Customers[City] IN { "New York", "Chicago" }, "United States", "Canada" )
Table Customers
Customer IDNameCityZone
C001OrvaneNew YorkUnited States
C002KumoraNew YorkUnited States
C003TalvioNew YorkUnited States
C004RibeliaChicagoUnited States
C005MovaraChicagoUnited States
C006NorthelTorontoCanada
C007Sam LeeTorontoCanada
Table: Customers (7 rows)

This is a reproduction of the Power BI interface for teaching purposes. It differs from the real interface.

The result is the same as with ||, five customers in “United States” and two in “Canada.”

07.4.4Mixing “and” and “or”

Watch out!

In the same condition, && comes before ||, just as multiplication comes before addition. This condition therefore reads “a price of 50,000, or else a price of 25,000 with more than 8 cars”:

Sales[Price] = 50000 || Sales[Price] = 25000 && Sales[Qty] > 8

It answers TRUE for the four sales at a price of 50,000, and for V001, at a price of 25,000 with 10 cars, that is five sales.

To require more than 8 cars in both cases, place parentheses around the “or”:

( Sales[Price] = 50000 || Sales[Price] = 25000 ) && Sales[Qty] > 8

The sales at a price of 50,000 have only 2 or 3 cars. Only V001 answers TRUE.

As soon as “and” and “or” are mixed, parentheses make the condition clear, even when they change nothing.

Quiz

The questions often rely on the course tables. Keep them in view in their floating window, which you can move and enlarge.

Question 1

What does COUNTROWS( FILTER( Sales, Sales[Price] = 10000 || Sales[Price] = 50000 && Sales[Qty] >= 10 ) ) return?

Question 2

You add to Sales the column Follow-up = IF( Sales[Qty] >= 10 || Sales[Price] = 50000, "Yes", "No" ). How many rows get “Yes”?

Question 3

What does CALCULATE( SUM( Sales[Amount] ), Customers[City] IN { "Chicago", "Toronto" } ) return?

Key takeaways
  • OR and || answer TRUE as soon as one of the conditions answers TRUE.
  • OR accepts only two arguments. || chains as many conditions as you want.
  • For several values of the same column, IN saves you from repeating the column, as in Customers[City] IN { "New York", "Chicago" }.
  • && comes before ||. Parentheses make the condition clear.