Ultimate Guide To Multiple IF Statements In Power BI

by | Power BI

If you are looking to create more complex logic, then using multiple if statements in Power BI is a great way to do that. The IF function allows you to perform different actions based on the evaluation of a single condition. It is particularly useful when you want to display different results or apply different calculations depending on the data in your model.

In this article, we’ll explore the use of multiple IF statements in Power BI and provide examples to help you understand and implement this useful technique in your own reports. Also we will touch on how to improve this even further using SWITCH / True logic with DAX.

Understanding the Syntax of IF Statements in Power BI

Understanding the Syntax of IF Statements in Power BI

The IF function in Power BI works similarly to the IF function in Excel. It evaluates a logical test and returns one value if the result is true, and another value if the result is false.

The syntax for the IF function is as follows:

IF(logical_test, value_if_true, value_if_false)
  • Logical_test: The condition you want to test. This can be a comparison, such as A1 > 10, or a function that returns TRUE or FALSE.
  • Value_if_true: The value to return if the logical test evaluates to true.
  • Value_if_false: The value to return if the logical test evaluates to false.

For example, if you want to check if the Sales value is greater than 100, you can use the following IF statement:

IF(Sales > 100, “Above Target”, “Below Target”)

This function will return “Above Target” if the Sales value is greater than 100, and “Below Target” if it is not.

DAX Languages Advisor - Data Mentor
DAX Languages Advisor – Data Mentor

Now, let’s explore how you can implement multiple if statements in your Power BI reports.

Using Nested IF Statements In DAX And Power BI

Using Nested IF Statements

Nested IF statements allow you to test multiple conditions and return different values based on those conditions. You can nest up to 64 IF functions within a single formula in Power BI.

The syntax for a nested IF statement is:

IF(logical_test1, value_if_true1, IF(logical_test2, value_if_true2, value_if_false2))

Here, if logical_test1 is true, value_if_true1 is returned; otherwise, logical_test2 is evaluated, and if true, value_if_true2 is returned; otherwise, value_if_false2 is returned.

For example, if you want to categorize customers based on their total purchases, you can use the following nested IF statement:

IF(TotalPurchases >= 1000, “High-Value Customer”, IF(TotalPurchases >= 500, “Medium-Value Customer”, “Low-Value Customer”))

In this case, if the TotalPurchases is greater than or equal to 1000, the function will return “High-Value Customer”. If not, it will check if the TotalPurchases is greater than or equal to 500 and return “Medium-Value Customer” if true. If neither condition is met, it will return “Low-Value Customer”.

When working with complex scenarios, you may also want to consider using the SWITCH and SELECTEDVALUE functions.

Now, let’s go over an example of using multiple if statements in Power BI.

Example of Using Multiple IF Statements

Example of Using Multiple IF Statements

Suppose you have a dataset with sales data, and you want to create a new column that categorizes sales based on their amount. You can use multiple IF statements to achieve this.

  1. Load your dataset into Power BI Desktop.
  2. In the Data view, click on Transform Data to open the Power Query Editor.
  3. In the Power Query Editor, click on New Column in the Home tab.
  4. In the formula bar, use the following syntax to create the new column:
=IF(Sales < 100, “Low”, IF(Sales < 500, “Medium”, “High”))

This formula checks the value of the Sales column and categorizes the sales into “Low”, “Medium”, or “High” based on their value.

Click on the OK button to apply the changes and close the Power Query Editor. This same technique can be used as a calculated column also.

Now, in your report, you’ll have a new column called “Sales Category” that categorizes the sales data based on their amount.

This is a very simplified version of using IF statements. Further along in this article we will jump into how to do this using the SWITCH function within DAX.

Common Errors and Solutions With Nested IF Functions

Common Errors and Solutions

While working with multiple IF statements in Power BI, you may encounter some common errors. Let’s look at them and how you can resolve them.

1. Error due to unmatched parentheses:

Make sure that each IF statement is properly closed with the appropriate number of closing parentheses.

2. Incorrectly formatted conditions:

Ensure that your logical tests are properly formatted, such as using the correct operators (>, <, =) and appropriate values for comparison.

3. Inaccurate or unexpected results:

Double-check the logical conditions and values you’re comparing, as well as the order of the conditions in your nested IF statements.

By carefully reviewing your formulas and addressing any errors, you can effectively work with multiple IF statements in Power BI.

A Better Way To Write Multiple IFs Using The SWITCH Function

For those transitioning from Excel, the familiarity with IF statements—often complex and nested within each other—is quite common. However, Power BI offers a more streamlined approach to crafting this logic through the DAX language, making it more accessible and easier to comprehend.

Here’s an example of a nested IF function from Data Mentor:

Here’s an example of the same formula but using the SWITCH function:

Using SWITCH True Logic Instead Of IF Statements

SWITCH true formula logic enables you to calculate just like an IF statement. It produces particular results based on whether something you evaluate is true or false. If they’re true, they will return a result.

The best part of this technique is that you can make the results into a variable. You can set it up just like a text or a number, but it can also be a measure. 

In the tutorial video, you can easily learn how to write the true or false logic. To access the video, just click the link or you can also search for it in YouTube on the Enterprise DNA channel.

Writing The Correct Format For SWITCH True Logic

This is the kind of format that you should use.

Another example:

An amazing technique that you can do is to use simple ampersands (&) to have multiple evaluations for every row.

The && operator, combines two boolean expressions and returns true only if both expressions are true.

In this particular example there are multiple evaluations on every row.

The SWITCH true logic iterates through every formula in every row and returns the corresponding results.

This is a superior way of creating any logic that would be otherwise done using Nested IF statements. This technique looks much cleaner and easier to understand, especially if you need to revise it.

Data Mentor – Languages Advisor

Using SWITCH functions within SWITCH functions – An Advanced Technique

There’s one last thing that I want to share with you if you want to structure your formulas for more advanced logic.

I used to have an advanced example where I had a SWITCH measure which branched out into another SWITCH measure. It also evaluates each different row, and then if the results are true it will evaluate the next measure. It also evaluated another SWITCH statement within that measure.

An example:

The formula can really get tricky, but the most amazing part is that it’s written very clearly in a manner that’s easy to understand.

** Related Links **
How To Use SWITCH True Logic In Power BI
Scenario Analysis Techniques Using Multiple ‘What If’ Parameters
Advanced Analytics in Power BI: Layering Multiple ‘What If’ Analysis

Final Thoughts

Final Thoughts

Multiple IF statements in Power BI can help you tackle a wide range of data analysis challenges. By nesting IF functions, you can craft detailed logic to categorize, filter, and calculate data.

Mastering this technique opens up a world of data-driven insights. The more complex your data, the more useful these nested IF statements become.

Mastering the SWITCH TRUE logic function in Power BI can be a game-changer for your data analysis. It is a powerful feature that allows you to make complex decisions based on multiple conditions, and it is often more efficient and easier to read than nested IF statements.

When you combine the SWITCH function with the TRUE function, you can create dynamic and flexible expressions that adapt to your data. This is particularly useful in scenarios where you have a range of possible values and want to compare them against a single expression.

If you’d like to learn more about all the techniques discussed in this article, check out the following video:

Frequently Asked Questions

Frequently Asked Questions

What is the IF statement syntax in Power BI?

The syntax for the IF function in Power BI is IF(logical_test, value_if_true, value_if_false). The function evaluates a logical test and returns the value_if_true if the test is true, and the value_if_false if the test is false.

How to handle multiple conditions in DAX?

To handle multiple conditions in DAX, you can use the AND and OR functions. The AND function returns TRUE if all the conditions are met, while the OR function returns TRUE if any of the conditions are met.

You can use these functions within the IF function to create more complex logic.

What is the IF function in Power Query?

The IF function in Power Query is used to apply conditional logic to data transformation steps. The syntax for the IF function is if condition then value else other.

It evaluates a condition and returns a value if the condition is true, and returns another value if the condition is false.

How to use IF function with multiple conditions in Power Query?

To use the IF function with multiple conditions in Power Query, you can nest IF functions within each other.

For example, if A > 10 then “Greater than 10” else if A > 5 then “Greater than 5” else “Less than or equal to 5”.

In this example, the second condition is only evaluated if the first condition is false.

How to write an IF formula in Power BI?

To write an IF formula in Power BI, use the syntax IF(logical_test, value_if_true, value_if_false).

For example, IF(Sales > 1000, “High”, “Low”).

This formula checks if the Sales value is greater than 1000, and if true, returns “High”; otherwise, it returns “Low”.

How can I use SWITCH TRUE logic with text values?

In Power BI, you can use the SWITCH TRUE logic with text values by following the same syntax and structure. Here’s an example:

What is the syntax for SWITCH TRUE with multiple conditions?

The syntax for SWITCH TRUE with multiple conditions is as follows:

How to use SWITCH TRUE with a date column?

To use SWITCH TRUE with a date column, you can compare the date column to specific dates. Here’s an example:

What are some common scenarios for using SWITCH TRUE?

Some common scenarios for using SWITCH TRUE include:

  • Categorizing data based on a numeric range
  • Assigning priority levels to tasks
  • Grouping data based on specific criteria
  • Implementing custom business logic

How to handle blank values in SWITCH TRUE logic?

To handle blank values in SWITCH TRUE logic, you can include a separate condition to check for blank values and return a specific result. Here’s an example:

author avatar
Sam McKay, CFA
Sam is Enterprise DNA's CEO & Founder. He helps individuals and organizations develop data driven cultures and create enterprise value by delivering business intelligence training and education.

Related Posts