The IF function in DAX checks if a given expression is true or false, and returns one value when it’s true, otherwise it returns a second value.
In DAX you can write the IF function in two different ways: in columns and measures.
The example of creating a calculated column using the IF statement:
CompanySize = IF ( Stores[EmployeeCount] <= 10, “Micro”, IF ( Stores[EmployeeCount] <=50, “Small”, “Medium”))
Notice that when you need to introduce more than two conditions, you have to use the nesting IF statements.
When using the IF function in a measure, you need to create a row context, as the formula requires rows to operate. One way to do this is by using the iterator functions, like SUMX, and then put the IF statement inside that function.
The example of using the IF function in DAX in a measure:
Total Discount Sales = SUMX(sales, IF( Sales[PromotionKey] <> 1, Sales[UnitPrice] * Sales[SalesQuantity]))
When you need to nest multiple IF functions, the SWITCH function might be a better option. This function provides a more elegant way to write an expression that returns more than two possible values.
Check out the video below: