07.1IF
In Excel
| Excel | IF |
|---|---|
| Match |
So far, your formulas ran the same calculation on every row. Often, you will want a result that depends on a condition, as with the IF function in Excel. In DAX, it is IF too.
Your manager, for example, calls any sale of at least 8 cars a “big sale.” He wants to spot them at a glance in the Sales table.
07.1.1The syntax of IF
IF chooses between two results depending on a condition.
The first argument is a condition, like the ones in the lesson on operators. If it answers TRUE, IF returns the second argument. If it answers FALSE, IF returns the third.
It is the IF function of Excel, with the same name and the same commas between the arguments.
07.1.2IF in a calculated column
Let us add to the Sales table a column that classifies each sale by its quantity:
Size = IF( Sales[Qty] >= 8, "Big sale", "Small sale" )| Sale ID | Qty | Size |
|---|---|---|
| V001 | 10 | Big sale |
| V002 | 3 | Small sale |
| V003 | 4 | Small sale |
| V004 | 10 | Big sale |
| V005 | 6 | Small sale |
| V006 | 8 | Big sale |
| V007 | 3 | Small sale |
| V008 | 15 | Big sale |
| V009 | 6 | Small sale |
| V010 | 3 | Small sale |
| V011 | 2 | Small sale |
| V012 | 5 | Small sale |
This is a reproduction of the Power BI interface for teaching purposes. It differs from the real interface.
On each row, IF reads the quantity of the row, then answers. Four sales reach 8 cars, V001, V004, V006 and V008.
A calculated column is calculated at data refresh, then stored. On a table of several million rows, it makes the file heavier. When the classification only serves to display a figure, a measure is often enough.
Sale V006 has exactly 8 cars. With >=, it becomes a big sale. With >, it would stay a small sale.
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 school records the grade of each student in the “Grades” table. Add to the “Grades” table the calculated column “Result,” which shows “Pass” from 10 up, and otherwise “Fail.”
Grades Student Grade Alma 14 Bilal 8 Chloé 10 Which formula should you write for “Result”?
Combined useCalculated columnDifficulty
A hotel charges 100 dollars a night, and 80 dollars from 7 nights up. This reduced price then applies to every night of the stay. Add to the “Stays” table the calculated column “Amount,” the price of the stay, that is the nights multiplied by the price of one night.
Stays Guest Nights Alma 3 Bruno 7 Célia 10
07.1.3Without a third argument
The third argument is optional. If it is left out, IF returns a blank value when the condition answers FALSE:
Size = IF( Sales[Qty] >= 8, "Big sale" )| Sale ID | Qty | Size |
|---|---|---|
| V001 | 10 | Big sale |
| V002 | 3 | |
| V003 | 4 | |
| V004 | 10 | Big sale |
| V005 | 6 | |
| V006 | 8 | |
| V007 | 3 | |
| V008 | 15 | |
| V009 | 6 | |
| V010 | 3 | |
| V011 | 2 | |
| V012 | 5 |
This is a reproduction of the Power BI interface for teaching purposes. It differs from the real interface.
The eight small sales stay blank. A blank value is neither zero nor a text. It means there is nothing.
07.1.4IF in a measure
IF can also be written in a measure. Its condition then applies to a result, for example the result of another measure. Your manager sets each city a target of 400,000, and wants to see, in a table by city, which ones reach it:
| City | Target Reached |
|---|---|
| New York | Reached |
| Chicago | Reached |
| Toronto | Not reached |
| Total | Reached |
This is a reproduction of the Power BI interface for teaching purposes. It differs from the real interface.
In a table by city, New York and Chicago show “Reached,” with 900,000 and 500,000. Toronto shows “Not reached,” with 300,000.
The “Total” row, for its part, shows “Reached.” It does not sum up the rows above, which often comes as a surprise. On this row, no city filters the sales. Power BI therefore recalculates the measure, SUM adds up the 12 sales, that is 1,700,000, then IF compares this total with 400,000. This behavior of the “Total” row will be covered in detail later.
In a measure, the condition cannot read a column on its own, except inside a function that goes through a table row by row, like FILTER or SUMX, covered later. A measure has no current row, and Power BI rejects the formula:
In a measure, the condition therefore applies to an aggregation, that is a calculation that sums up a column into a single value, like SUM( Sales[Qty] ) >= 8, or to another measure.
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 useMeasureDifficulty
A library already has the “Late Returns” measure, the number of books returned late. Write the “Alert” measure, which shows “Follow up” above 10 late returns, and otherwise “Normal.”
Which formula should you write for “Alert”?
Combined useMeasureDifficulty
A weather station records the rain that falls each day, in millimeters, in the “Rain” column of the “Readings” table. Write the “Period” measure, which shows “Rainy” when the total rain is over 50, and otherwise “Dry.”
Readings Day Rain 11/1/2026 20 11/2/2026 15 11/3/2026 22
07.1.5Nested IFs
To get more than two results, place one IF inside another, as in Excel:
“For each sale, I want ‘Large’ if its amount reaches 200,000, otherwise ‘Medium’ if it reaches 100,000, otherwise ‘Small.’”
Category = IF( Sales[Amount] >= 200000, "Large", IF( Sales[Amount] >= 100000, "Medium", "Small" ) )| Sale ID | Amount | Category |
|---|---|---|
| V001 | 250,000 | Large |
| V002 | 150,000 | Medium |
| V003 | 100,000 | Medium |
| V004 | 100,000 | Medium |
| V005 | 150,000 | Medium |
| V006 | 200,000 | Large |
| V007 | 150,000 | Medium |
| V008 | 150,000 | Medium |
| V009 | 150,000 | Medium |
| V010 | 150,000 | Medium |
| V011 | 100,000 | Medium |
| V012 | 50,000 | Small |
This is a reproduction of the Power BI interface for teaching purposes. It differs from the real interface.
The first IF tests the first condition. If it answers FALSE, it hands over to the second IF, which tests the next one. Two sales are large, V001 and V006, and only one is small, V012.
Beyond two or three levels, a formula of nested IFs becomes hard to read. SWITCH, presented later, writes it more simply.
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 useMeasureDifficulty
A hotel already has the “Occupancy” measure, the share of rooms occupied, between 0 and 1. Write the “Level” measure, which shows “Full” from 0.9 up, “Medium” from 0.5 up, and otherwise “Low.”
Which formula should you write for “Level”?
Combined useCalculated columnDifficulty
A store pays its sellers a bonus, based on the “Sales” column of the “Sellers” table. Add to the “Sellers” table the calculated column “Bonus,” 10% of the sales from 50,000 up, 5% from 20,000 up, and otherwise 0.
Sellers Seller Sales Alma 60,000 Bilal 30,000 Chloé 10,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 Size = IF( Sales[Qty] > 8, "Big sale", "Small sale" ). How many rows get “Big sale”?
Question 2
In a table by city, the measure Target = IF( [Revenue] >= 400000, "Reached" ) is calculated on the Toronto row, at 300,000. What does it return?
Question 3
In a table by city, the measure Volume = IF( SUM( Sales[Qty] ) >= 20, "High", "Low" ) is calculated on the Chicago row. What does it show?
- IF( <condition>, <value_if_true>, [<value_if_false>] ) returns the second argument if the condition answers TRUE, and the third otherwise.
- Without a third argument, IF returns a blank value when the condition answers FALSE.
- In a calculated column, the condition can read the value of the row. In a measure, it applies to an aggregation or to another measure.
- For more than two results, you can nest IFs, at the cost of a formula that is harder to read.