Short answer: IF ( condition, then [, else] ) returns one of two results; without else it returns BLANK. SWITCH ( expression, value1, result1, value2, result2, ..., else ) compares one expression with many values, and SWITCH ( TRUE (), condition1, result1, condition2, result2, ..., else ) tests conditions in order, which replaces nested IFs for ranges and bands. Combine tests with && (and), || (or), NOT and IN { ... }. IF.EAGER computes both branches up front, which can be faster in some cases.
Status labels, price bands, traffic lights and measure switches are all conditions. Written as five nested IFs they are hard to read and easy to break; as one SWITCH ( TRUE () ) they read like a table.
What does each look like?
-- IF: two outcomes
Margin Status = IF ( [Margin %] >= 0.3, "Healthy", "Low" )
-- SWITCH: one expression, many values
Region Group =
SWITCH (
SELECTEDVALUE ( Customer[Country] ),
"DE", "DACH",
"AT", "DACH",
"CH", "DACH",
"FR", "West",
"Other"
)
-- SWITCH TRUE: ordered conditions for ranges
Attainment Band =
VAR A = DIVIDE ( [Revenue], [Target] )
RETURN
SWITCH (
TRUE (),
ISBLANK ( A ), BLANK (),
A >= 1.1, "Above plan",
A >= 1, "On plan",
A >= 0.9, "Close",
"Behind"
)SWITCH ( TRUE () ) returns the result of the first condition that is TRUE, so order matters: test the highest band first.
How do you combine conditions?
| Write | Means | Example |
|---|---|---|
| && or AND ( a, b ) | both true | [Revenue] > 0 && [Margin] > 0 |
| || or OR ( a, b ) | at least one true | Customer[Country] = "DE" || Customer[Country] = "AT" |
| NOT ( x ) | the opposite | NOT ISBLANK ( [Revenue LY] ) |
| IN { ... } | equals one of a list | Customer[Country] IN { "DE", "AT", "CH" } |
AND and OR take exactly two arguments; && and || can be chained.
How do you switch measures with a slicer?
Metric =
SWITCH (
SELECTEDVALUE ( 'Metric Picker'[Metric], "Revenue" ),
"Revenue", [Revenue],
"Margin", [Margin],
"Orders", [Orders],
BLANK ()
)'Metric Picker' is a small disconnected table of names. Field parameters now do this with formats included; the SWITCH version is still useful inside other measures.
What type does the result have?
Microsoft notes that IF can return a variant if the branches have different types, and tries to return one type when both are numeric: IF ( condition, 1.0, 0 ) returns decimals only. Mixing text and numbers (IF ( x, "n/a", 0 )) makes the result a variant, which visuals cannot sum and which can break sorting; return BLANK instead of a text placeholder in numeric measures.
What about errors and blanks?
-- first non-blank
Revenue or Zero = COALESCE ( [Revenue], 0 )
-- catch an error (use sparingly)
Rate Safe = IFERROR ( VALUE ( Settings[Rate Text] ), 0 )In conditions, BLANK behaves like 0 or FALSE: IF ( [Revenue] > 0, ... ) is FALSE for BLANK, and IF ( [Revenue] = 0, ... ) is TRUE for BLANK. Test ISBLANK first when it matters (see BLANK handling). IFERROR hides problems and can be slow; prefer DIVIDE and checks that avoid the error.
Is IF.EAGER faster?
IF evaluates the branch the condition picks. IF.EAGER evaluates both branches first, like two variables, then picks one. Microsoft says the result is the same and performance may differ because of different execution plans. Try it only when a profiler shows IF is slow, typically when both branches use the same expensive measure.
What goes wrong?
- Nested IFs ten deep. Use
SWITCH ( TRUE () ). - Band order. Test the strictest condition first in
SWITCH ( TRUE () ). - Text in numeric measures. "n/a" turns the measure into a variant; use BLANK.
- IF without else. Returns BLANK, which is often right, but say so in the measure's description.
- Conditions on columns in measures.
IF ( Sales[Amount] > 100, ... )in a measure has no row; use an aggregate or an iterator.
Where can you check this?
- Microsoft Learn: IF function (DAX)
- Microsoft Learn: SWITCH function (DAX)
- Microsoft Learn: IF.EAGER function (DAX)
Where do you start?
Find your deepest nested IF and rewrite it as SWITCH ( TRUE () ) with the bands in order. DIVIDE handles the most common condition of all: a zero denominator.