07.2SWITCH
In Excel
| Excel | SWITCH |
|---|---|
| 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.
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:
“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" )| Customer ID | Name | City | Country |
|---|---|---|---|
| C001 | Orvane | New York | United States |
| C002 | Kumora | New York | United States |
| C003 | Talvio | New York | United States |
| C004 | Ribelia | Chicago | United States |
| C005 | Movara | Chicago | United States |
| C006 | Northel | Toronto | Canada |
| C007 | Sam Lee | Toronto | Canada |
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:
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.
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 Name Plan Inès A Hugo M Jade S Which formula should you write for “Plan Name”?
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 Order Method Price O1 D 20 O2 P 30 O3 E 15
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:
“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" )| 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 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.
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”?
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 Student Grade Alma 17 Bilal 15 Chloé 12 Dario 9
07.2.4The order of the conditions matters
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" )| Sale ID | Amount | Category |
|---|---|---|
| V001 | 250,000 | Medium |
| V002 | 150,000 | Medium |
| V003 | 100,000 | Medium |
| V004 | 100,000 | Medium |
| V005 | 150,000 | Sedan |
| V006 | 200,000 | Sedan |
| V007 | 150,000 | SUV |
| V008 | 150,000 | City car |
| V009 | 150,000 | Sedan |
| V010 | 150,000 | SUV |
| V011 | 100,000 | SUV |
| V012 | 50,000 | City car |
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”?
- 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.