07.3AND and &&

In Excel
ExcelAND
Match

You met && and AND with the operators. Here they are at the service of IF, when you want two conditions to be met at the same time.

The dealership, for example, pays the seller a bonus when a sale has at least 8 cars at a price of 25,000. A sale of 8 cars at 10,000 does not qualify. Both conditions must be met.

07.3.1Requiring two conditions at once

AND and && answer TRUE only if both conditions answer TRUE.

AND( <condition 1>, <condition 2> )
<condition 1> && <condition 2>

Let us add to the Sales table a column that flags the sales that qualify for the bonus, the big sales at a price of 25,000:

Bonus = IF( Sales[Qty] >= 8 && Sales[Price] = 25000, "Yes", "No" )
Power BI accepts this DAX forno, Measureyes, Calculated columnno, Calculated tableno, Virtual table
Bonus = IF( Sales[Qty] >= 8 && Sales[Price] = 25000, "Yes", "No" )
Table Sales
Sale IDQtyPriceBonus
V0011025,000Yes
V002350,000No
V003425,000No
V0041010,000No
V005625,000No
V006825,000Yes
V007350,000No
V0081510,000No
V009625,000No
V010350,000No
V011250,000No
V012510,000No
Table: Sales (12 rows)

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

Four sales reach 8 cars, but V004 and V008 have a price of 10,000. Only V001 and V006 meet both conditions.

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 hotel offers a gift to guests who stay at least 5 nights with breakfast. Add to the “Stays” table the calculated column “Gift,” which shows “Yes” when both conditions are met, and otherwise “No.”

    Stays
    GuestNightsBreakfast
    Alma6Yes
    Bruno7No
    Célia3Yes

    Which formula should you write for “Gift”?

  2. Combined useMeasureDifficulty

    A carrier wants to count the heavy parcels that go to the mountains. Write the “Heavy Mountain Parcels” measure, shown in a card, the number of parcels in the “Parcels” table that weigh more than 20 kilos and whose zone is “Mountain.”

    Parcels
    ParcelWeightZone
    C-40125Mountain
    C-40230City
    C-40312Mountain
    C-40422Mountain

07.3.2The four possible cases

Two conditions make four combinations. “And” answers TRUE in only one of them:

The answer of “and”
The answer of “and”
Condition 1Condition 2Condition 1 && Condition 2
TRUETRUETRUE
TRUEFALSEFALSE
FALSETRUEFALSE
FALSEFALSEFALSE

For V004, the quantity answers TRUE, but the price answers FALSE. The result is FALSE.

07.3.3AND accepts only two arguments

Watch out!

A reminder from the lesson on operators: AND accepts only two arguments. With three conditions, you have to slip one AND inside another:

AND( AND( Sales[Qty] >= 8, Sales[Price] = 25000 ), Sales[Amount] > 200000 )

With &&, the conditions simply follow one another:

Sales[Qty] >= 8 && Sales[Price] = 25000 && Sales[Amount] > 200000

Both forms answer TRUE for sale V001 alone, at 250,000. This flexibility is why && is generally preferred.

Tip

In CALCULATE, the comma between two filters already means “and.” The two formulas below return the same result, as seen in “Six basic functions”:

CALCULATE( SUM( Sales[Amount] ), Sales[Qty] >= 8, Sales[Price] = 25000 )
Power BI accepts this DAX foryes, Measureyes, Calculated columnno, Calculated tableno, Virtual table
CALCULATE( SUM( Sales[Amount] ), Sales[Qty] >= 8 && Sales[Price] = 25000 )
Power BI accepts this DAX foryes, Measureyes, Calculated columnno, Calculated tableno, Virtual table

They add up the amounts of V001 and V006, that is 450,000.

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

You add to Sales the column Discount = IF( Sales[Qty] >= 8 && Sales[Price] = 10000, "Yes", "No" ). How many rows get “Yes”?

Question 2

What happens with AND( Sales[Qty] >= 8, Sales[Price] = 25000, Sales[Amount] > 200000 )?

Question 3

What does CALCULATE( SUM( Sales[Amount] ), Sales[Price] = 25000, Sales[Qty] < 8 ) return?

Key takeaways
  • AND and && answer TRUE only if both conditions answer TRUE.
  • AND accepts only two arguments. && chains as many conditions as you want.
  • In CALCULATE, the comma between two filters already means “and.”