07.2SWITCH

In Excel
ExcelSWITCH
Match

With IF, you can choose between two results. When a column can take several values, a chain of IFs quickly becomes unreadable. SWITCH matches each value with its result, in a single function.

Your report must, for example, show the sales by country, while the Customers table only gives the city.

07.2.1The syntax of SWITCH

SWITCH compares an expression with a list of values, then returns the result paired with the first value that is equal.

SWITCH( <expression>, <value>, <result>, [<value>, <result> …], [<else>] )

Values and results go in pairs. The last argument is optional. It is the result returned when no value matches. If it is left out, SWITCH returns a blank value.

07.2.2SWITCH in a calculated column

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

Country = SWITCH( Customers[City], "New York", "United States", "Chicago", "United States", "Toronto", "Canada", "Other" )
Power BI accepts this DAX forno, Measureyes, Calculated columnno, Calculated tableno, Virtual table
In plain words

“For each customer, I want to compare their city with New York, Chicago and Toronto, and write ‘United States’ or ‘Canada’ depending on the city found, otherwise ‘Other.’”

Country = SWITCH( Customers[City], "New York", "United States", "Chicago", "United States", "Toronto", "Canada", "Other" )
Table Customers
Customer IDNameCityCountry
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.

On each row, SWITCH reads the city, compares it with "New York," then with "Chicago," then with "Toronto," and returns the matching country. No row gets “Other,” since all three cities are covered.

With IF, the same result takes three nested IFs:

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

SWITCH writes the column only once, and each city fits on its own line.

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 records the plan of each member with a letter in the “Members” table. Add the calculated column “Plan Name,” which writes “Annual” for “A,” “Monthly” for “M” and “Session” for “S.”

    Members
    NamePlan
    InèsA
    HugoM
    JadeS

    Which formula should you write for “Plan Name”?

  2. Combined useCalculated columnDifficulty

    A grocery store adds a fee depending on the method of each order, “D” for a delivery at 5 dollars, “P” for a free pickup, and 10 dollars for any other method. Add to the “Orders” table the calculated column “Final Price,” the price in the “Price” column plus the fee of the method, read in the “Method” column.

    Orders
    OrderMethodPrice
    O1D20
    O2P30
    O3E15

07.2.3SWITCH( TRUE() ) for conditions

SWITCH compares the expression with values. To test conditions, like thresholds, write TRUE(), which returns TRUE, in place of the expression. SWITCH then compares TRUE with the answer of each condition, TRUE or FALSE, and returns the result of the first one that answers TRUE:

Category = SWITCH( TRUE(), Sales[Amount] >= 200000, "Large", 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, ‘Medium’ if it reaches 100,000, otherwise ‘Small,’ testing the thresholds in order, from the first to the last.”

Category = SWITCH( TRUE(), Sales[Amount] >= 200000, "Large", 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 result is the same as the nested IFs of the lesson on IF. Two sales are large, nine are medium, and only one is small, V012.

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 bakery already has the “Loaves Sold” measure, which adds up the loaves sold. Write the “Day Rating” measure, which shows “Excellent” from 300 loaves up, “Good” from 150 up, and otherwise “Quiet.”

    Which formula should you write for “Day Rating”?

  2. Combined useCalculated columnDifficulty

    A school gives an honors level to each grade of the “Grades” table. Add the calculated column “Honors,” which shows “Excellent” from 16 up, “Very good” from 14 up, “Good” from 12 up, and otherwise “No honors.”

    Grades
    StudentGrade
    Alma17
    Bilal15
    Chloé12
    Dario9

07.2.4The order of the conditions matters

Watch out!

SWITCH stops at the first condition that answers TRUE. Let us swap the two thresholds:

Category = SWITCH( TRUE(), Sales[Amount] >= 100000, "Medium", Sales[Amount] >= 200000, "Large", "Small" )
Power BI accepts this DAX forno, Measureyes, Calculated columnno, Calculated tableno, Virtual table
Category = SWITCH( TRUE(), Sales[Amount] >= 100000, "Medium", Sales[Amount] >= 200000, "Large", "Small" )
Table Sales
Sale IDAmountCategory
V001250,000Medium
V002150,000Medium
V003100,000Medium
V004100,000Medium
V005150,000Sedan
V006200,000Sedan
V007150,000SUV
V008150,000City car
V009150,000Sedan
V010150,000SUV
V011100,000SUV
V01250,000City car
Table: Sales (12 rows)

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

Sale V001, at 250,000, answers TRUE from the first condition, 100,000 or more. It becomes “Medium,” and the “Large” condition is never reached. V006 meets the same fate.

When the conditions fit inside one another, like these thresholds, write them from the most demanding to the least demanding.

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 = SWITCH( TRUE(), Sales[Qty] >= 5, "Medium", Sales[Qty] >= 10, "Big", "Small" ). How many rows get “Big”?

Question 2

You add to Customers the column Country = SWITCH( Customers[City], "New York", "United States", "Chicago", "United States" ). What does it hold on the row of Northel, in Toronto?

Question 3

You add to Products the column Range = SWITCH( Products[Category], "Sedan", "Family", "SUV", "Family", "Small" ). How many products get “Family”?

Key takeaways
  • SWITCH( <expression>, <value>, <result>, …, [<else>] ) returns the result of the first value equal to the expression.
  • Without a last argument, SWITCH returns a blank value when no value matches.
  • SWITCH( TRUE(), <condition>, <result>, … ) returns the result of the first condition that answers TRUE.
  • The order of the conditions matters. When they fit inside one another, like thresholds, write them from the most demanding to the least demanding.