07.1IF

In Excel
ExcelIF
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.

IF( <condition>, <value_if_true>, [<value_if_false>] )

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" )
Power BI accepts this DAX forno, Measureyes, Calculated columnno, Calculated tableno, Virtual table
Size = IF( Sales[Qty] >= 8, "Big sale", "Small sale" )
Table Sales
Sale IDQtySize
V00110Big sale
V0023Small sale
V0034Small sale
V00410Big sale
V0056Small sale
V0068Big sale
V0073Small sale
V00815Big sale
V0096Small sale
V0103Small sale
V0112Small sale
V0125Small sale
Table: Sales (12 rows)

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.

Tip

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.

Watch out!

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.

  1. 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
    StudentGrade
    Alma14
    Bilal8
    Chloé10

    Which formula should you write for “Result”?

  2. 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
    GuestNights
    Alma3
    Bruno7
    Célia10

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" )
Power BI accepts this DAX forno, Measureyes, Calculated columnno, Calculated tableno, Virtual table
Size = IF( Sales[Qty] >= 8, "Big sale" )
Table Sales
Sale IDQtySize
V00110Big sale
V0023
V0034
V00410Big sale
V0056
V0068
V0073
V00815
V0096
V0103
V0112
V0125
Table: Sales (12 rows)

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:

Target Reached = IF( [Revenue] >= 400000, "Reached", "Not reached" )
Power BI accepts this DAX foryes, Measureyes, Calculated columnno, Calculated tableno, Virtual table
Table visual, The “Target Reached” measure by city
CityTarget Reached
New YorkReached
ChicagoReached
TorontoNot reached
TotalReached

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.

Watch out!

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:

rejected in a measureSize = IF( Sales[Qty] >= 8, "Big sale", "Small sale" )

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.

  1. 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”?

  2. 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
    DayRain
    11/1/202620
    11/2/202615
    11/3/202622

07.1.5Nested IFs

To get more than two results, place one IF inside another, as in Excel:

Category = IF( Sales[Amount] >= 200000, "Large", IF( Sales[Amount] >= 100000, "Medium", "Small" ) )
Power BI accepts this DAX forno, Measureyes, Calculated columnno, Calculated tableno, Virtual table
In plain words

“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" ) )
Table Sales
Sale IDAmountCategory
V001250,000Large
V002150,000Medium
V003100,000Medium
V004100,000Medium
V005150,000Medium
V006200,000Large
V007150,000Medium
V008150,000Medium
V009150,000Medium
V010150,000Medium
V011100,000Medium
V01250,000Small
Table: Sales (12 rows)

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.

Tip

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.

  1. 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”?

  2. 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
    SellerSales
    Alma60,000
    Bilal30,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?

Key takeaways
  • 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.