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

  1. 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.

  2. 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.

  3. 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 NameOpen DateStatusStateCity
Store A2020-01-01ANYNew York
Store B2019-05-15BCALos Angeles
Store C2021-07-20ATXHouston
Store D2018-11-30CFLMiami

Calculated Column: Active Store Name Using the DAX expression:

Active Store Name = IF([Status] = "A", [Store Name], "Inactive " & [Store Name])

Resulting Table

Store NameOpen DateStatusStateCityActive Store Name
Store A2020-01-01ANYNew YorkStore A
Store B2019-05-15BCALos AngelesInactive Store B
Store C2021-07-20ATXHoustonStore C
Store D2018-11-30CFLMiamiInactive 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.