IF and SWITCH in DAX: conditions with examples

How IF, IF.EAGER and SWITCH work in DAX, nested conditions, SWITCH TRUE for ranges, measure switching from a slicer, data types of the result, AND, OR, NOT and IN, COALESCE and IFERROR, blanks in conditions, and performance.

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?

WriteMeansExample
&& or AND ( a, b )both true[Revenue] > 0 && [Margin] > 0
|| or OR ( a, b )at least one trueCustomer[Country] = "DE" || Customer[Country] = "AT"
NOT ( x )the oppositeNOT ISBLANK ( [Revenue LY] )
IN { ... }equals one of a listCustomer[Country] IN { "DE", "AT", "CH" }
Logical operators and functions

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?

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.

Keep reading

More from the blog.

Every post

Next

Building this? Say hello.

A question about this post, a pattern you would build differently, or the next thing you want written out: Behnia reads every message.