Power BI DAX: IF Conditional Expression
DAX expression:
Active Store Name =
IF ( [Status] = "A", [Store Name], "Inactive - " & [Store Name] )
The IF function in DAX plays a crucial role in creating conditional calculated columns in Power BI.
How it works:
Role of the IF Function
Logical Test: • The IF function starts by evaluating a logical test. This test is a condition that returns either TRUE or FALSE. For example, checking if a column value equals a specific value.
True Result: • If the logical test is TRUE, the IF function returns a specified value. This could be a value from another column, a calculated value, or a static value.
False Result: • If the logical test is FALSE, the IF function returns a different specified value. This allows for flexibility in handling different scenarios based on the condition.
Example Scenario
Imagine you have a dataset where you need to label stores as "Active" or "Inactive" based on their status. The IF function can be used to create a new column that dynamically assigns these labels:
Active Store Name = IF([Status] = "A", [Store Name], "Inactive " & [Store Name])
• Logical Test: [Status] = "A"
• This checks if the store's status is "A" (active).
• True Result: [Store Name]
• If the status is "A", it returns the store's name.
• False Result: "Inactive " & [Store Name]
• If the status is not "A", it returns "Inactive " followed by the store's name.
Illustration
Sample Table: Stores
Store Name | Open Date | Status | State | City |
Store A | 2020-01-01 | A | NY | New York |
Store B | 2019-05-15 | B | CA | Los Angeles |
Store C | 2021-07-20 | A | TX | Houston |
Store D | 2018-11-30 | C | FL | Miami |
Calculated Column: Active Store Name Using the DAX expression:
Active Store Name = IF([Status] = "A", [Store Name], "Inactive " & [Store Name])
Resulting Table
Store Name | Open Date | Status | State | City | Active Store Name |
Store A | 2020-01-01 | A | NY | New York | Store A |
Store B | 2019-05-15 | B | CA | Los Angeles | Inactive Store B |
Store C | 2021-07-20 | A | TX | Houston | Store C |
Store D | 2018-11-30 | C | FL | Miami | Inactive Store D |
In this example:
• For stores with a status of "A", the Active Store Name column returns the store's name.
• For stores with a status other than "A", the Active Store Name column returns "Inactive " followed by the store's name.