07.3AND and &&
In Excel
| Excel | AND |
|---|---|
| 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.
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" )| Sale ID | Qty | Price | Bonus |
|---|---|---|---|
| V001 | 10 | 25,000 | Yes |
| V002 | 3 | 50,000 | No |
| V003 | 4 | 25,000 | No |
| V004 | 10 | 10,000 | No |
| V005 | 6 | 25,000 | No |
| V006 | 8 | 25,000 | Yes |
| V007 | 3 | 50,000 | No |
| V008 | 15 | 10,000 | No |
| V009 | 6 | 25,000 | No |
| V010 | 3 | 50,000 | No |
| V011 | 2 | 50,000 | No |
| V012 | 5 | 10,000 | No |
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.
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 Guest Nights Breakfast Alma 6 Yes Bruno 7 No Célia 3 Yes Which formula should you write for “Gift”?
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 Parcel Weight Zone C-401 25 Mountain C-402 30 City C-403 12 Mountain C-404 22 Mountain
07.3.2The four possible cases
Two conditions make four combinations. “And” answers TRUE in only one of them:
| Condition 1 | Condition 2 | Condition 1 && Condition 2 |
|---|---|---|
| TRUE | TRUE | TRUE |
| TRUE | FALSE | FALSE |
| FALSE | TRUE | FALSE |
| FALSE | FALSE | FALSE |
For V004, the quantity answers TRUE, but the price answers FALSE. The result is FALSE.
07.3.3AND accepts only two arguments
A reminder from the lesson on operators: AND accepts only two arguments. With three conditions, you have to slip one AND inside another:
With &&, the conditions simply follow one another:
Both forms answer TRUE for sale V001 alone, at 250,000. This flexibility is why && is generally preferred.
In CALCULATE, the comma between two filters already means “and.” The two formulas below return the same result, as seen in “Six basic functions”:
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?
- 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.”