Power BI

How to Use SWITCH() in Power BI: A DAX Function Guide

How to Use SWITCH() in Power BI: A DAX Function Guide

Table of Contents

Introduction

DAX is a formula language that offers powerful functions for handling complex logic, and one of the most versatile among them is SWITCH() in Power BI. It simplifies multiple conditional checks into a clean and readable structure, making it ideal for categorization and scenario-based calculations.

If you’re new to DAX concepts, you may also find this blogpost helpful: Understanding the difference between VALUES(), DISTINCT(), ALL() and ALLNOBLANKROW()

In this guide, you’ll learn how to use SWITCH() to group products based on profit percentages, create custom sorting logic, and apply conditional discount rules. Whether you’re building dashboards or improving data models, mastering SWITCH() will make your Power BI reports more dynamic and insightful.

Using SWITCH()

We use SWITCH() to perform conditional calculations by evaluating an expression against multiple possible values or conditions.

SWITCH() in Power BI
Figure 1: SWITCH() Expression

You can download the Power BI file using the link provided below the blog and practice this step by step. 

Let’s use SWITCH() to categorize products based on profit percentages:

  1. Right click on the Product table in the Fields.
  2. Click on New Column.

    Column tools open.

  1. Type the following formula in the formula bar and press Enter.
SWITCH() in Power BI
Figure 2: SWITCH() Function in Power BI

In the expression above:

  • The first argument in SWITCH() is TRUE(), it always returns TRUE.
  • In the second argument, Product Profit % (CC) is tested, if it is greater than 0.6 than the profit group is categorized as “V.High”.
  • However, if the condition in the case above is not satisfied, then the next case is tested, i.e., Product Profit % (CC) greater than 0.5. If this returns TRUE, then profit group is categorized as “High”.
  • The same logic is repeated for all cases.
  • If none of the condition is satisfied, the default result is “V.Low”.

Note: Since you have created a column containing text only, you will need a numerical column to sort this alphabetically.

4. Right click on the Product table in the Fields.

5. Click on New Column.

   Column tools open.

6. Type the following formula in the formula bar and press Enter.

SWITCH() in Power BI
Figure 4: first argument in SWITCH() is TRUE() it always returns TRUE

In the expression above:

  • The first argument in SWITCH() is TRUE() it always returns TRUE.
  • In the second argument, Product Profit % (CC) is tested, if it is greater than 0.6 than the profit group sorting order is categorized as “1”.
  • However, if the condition in the case above is not satisfied then the next case is tested, i.e., Product Profit % (CC) greater than 0.5. If this returns TRUE then profit group sorting order is categorized as “2”.
  • The same logic is repeated for all cases.
  • If none of the condition is satisfied, the default result is “5”.

7. Click on Data tab in the left Ribbon.

8. Click on Product table in the Fields.

SWITCH() in Power BI
Figure 5: Power BI Product table with calculated columns “Profit group – Switch (CC)” and “Profit group-Sort Order (CC)” created using DAX SWITCH function.

Now that you have created all the required columns, lets sort them.

9. Click on Profit group – Switch (CC) in the Product table.

Column tools open.

columns tools
Figure 6: Open columns tools

10. Click on Sort by column.

11. Click on Profit group-Sort Order (CC).

visualization pane.
Figure 7: Showing Sort by Column feature used to sort Profit Group by numeric order.

Now let’s visualize the results.

12. Click on Stacked Column chart in the Visualization Pane.

13. Drag and drop Category from the Product table to the Axis.

14. Drag and drop Product from the Product table to the Values. Rename this to “Number of Products”

15. Drag and drop Profit group – Switch (CC) from the Product table to the Legend.

Product table
Figure 8: Accessories contain the highest proportion of “V.High” products and only Clothing contains “V.Low” products. Let’s see the numerical stats for this analysis.

16. Click on the matrix visual in the visualization pane.

17. Drag and drop Category from the Products table to the rows.

18. Drag and drop Profit group – Switch (CC) from the Product table to the Column.

19. Drag and drop Product from Product table to the Values. Right click on Product in the values section and select Count.

Product table
Figure 9: Product in the values section and select Count
matrix
Figure 10: Notice that each cell of the matrix has an incoming filter context from rows as well as the columns.

Switching between cases using SWITCH(): Conditional Discount Rules

We also use SWITCH() to calculate different cases.

To understand how to use SWITCH() to calculate discount:

  1. Right click on the Product table in the Fields.
  2. Click on New Column.

Column tools open.

  1. Type the following formula in the formula bar and press Enter.
Formula bar
Figure 11: product colour is tested and if it is red then a value of 0.5 is assigned.

In the expression above:

  • In the first case, product colour is tested and if it is red then a value of 0.5 is assigned.
  • In the second case, the existence of two conditions is evaluated simultaneously using AND() and if both are satisfied then a value of 0.40 is assigned.
  • Otherwise, the value is 0.

4. Click on the “%” in the formatting section.

5. Click on the matrix visual in the visualization pane.

6. Drag and drop Category from the Products table to the rows.

7. Drag and drop New Discount % (CC) from the Product table to the Column.

8. Drag and drop Product from Product table to the Values. Right click on Product and select Count.

Product table
Figure 12: Select Count
Product table
Figure 13: Notice that the New Discount % campaign is focused on products from the category of Bikes and Components and no products from Clothing qualifies the set criteria.

Conclusion

The SWITCH() function is an essential tool in Power BI for handling multiple conditions efficiently. By using SWITCH(TRUE(), …), you can replace complex nested IF statements with cleaner and more maintainable logic. As your models grow more complex, SWITCH() will become a go-to function for writing scalable and readable DAX.

FAQs

 

The SWITCH() function in Power BI is a DAX function that evaluates an expression against multiple conditions and returns corresponding results. It is often used as a cleaner alternative to nested IF statements.

 

We use SWITCH() when we have multiple conditions to evaluate. It improves readability and makes code easier to maintain compared to multiple nested IF statements.

SWITCH(TRUE(), …) is a common pattern in DAX where each condition is evaluated sequentially. The function returns the result of the first condition that evaluates to TRUE.

Extended Learning & References     

Leave a Reply

Your email address will not be published. Required fields are marked *