DAX logical functions: every one with an example

A reference of all DAX logical functions, IF, IF.EAGER, SWITCH, AND, OR, NOT, TRUE, FALSE, IFERROR, COALESCE and the bit functions BITAND, BITOR, BITXOR, BITLSHIFT and BITRSHIFT, with the operators &&, || and IN, examples, and the mistakes to avoid.

Short answer: DAX logical functions build conditions and choose between results: IF and SWITCH choose, AND, OR and NOT combine tests (or the operators &&, || and IN), TRUE and FALSE are the constants, IFERROR catches errors, COALESCE returns the first non-blank value, and the BIT functions work on the bits of whole numbers. This page lists all of them with an example each.

Which functions choose a result?

FunctionDoesExample
IF ( test, then [, else] )one of two results; BLANK without elseIF ( [Margin %] < 0, "Loss", "Profit" )
IF.EAGER ( test, then, else )like IF, but evaluates both branches firstIF.EAGER ( [Revenue] > 0, [Revenue] / 1000, 0 )
SWITCH ( expr, v1, r1, ..., else )compares one expression with many valuesSWITCH ( [Month No], 1, "Jan", 2, "Feb", "Other" )
SWITCH ( TRUE (), c1, r1, ..., else )the first condition that is TRUE winsSWITCH ( TRUE (), [A] >= 1, "On plan", "Behind" )
IFERROR ( value, if_error )a fallback when the value is an errorIFERROR ( VALUE ( T[Text] ), BLANK () )
COALESCE ( e1, e2, ... )the first expression that is not BLANKCOALESCE ( [Forecast], [Budget], 0 )
Choosing

In depth: IF and SWITCH.

Which functions combine tests?

Function or operatorTRUE whenExample
AND ( a, b ) or a && bboth are TRUE[Revenue] > 0 && [Margin] > 0
OR ( a, b ) or a || bat least one is TRUECustomer[Country] = "DE" || Customer[Country] = "AT"
NOT ( a )a is FALSENOT ( ISBLANK ( [Revenue LY] ) )
x IN { ... }x equals one of the valuesCustomer[Country] IN { "DE", "AT", "CH" }
TRUE ( ), FALSE ( )the logical constantsSWITCH ( TRUE ( ), ... )
Combining

AND and OR take exactly two arguments: three tests need AND ( a, AND ( b, c ) ) or a && b && c, which reads better.

Which functions work on bits?

FunctionReturnsExampleResult
BITAND ( a, b )bits set in bothBITAND ( 13, 11 )9
BITOR ( a, b )bits set in eitherBITOR ( 9, 10 )11
BITXOR ( a, b )bits set in exactly oneBITXOR ( 9, 10 )3
BITLSHIFT ( n, k )n shifted k bits leftBITLSHIFT ( 2, 3 )16
BITRSHIFT ( n, k )n shifted k bits rightBITRSHIFT ( 16, 2 )4
Bit functions

The common use is flag columns, where one number stores several yes or no options:

-- Features: 1 = email, 2 = SMS, 4 = push
Has SMS = BITAND ( Customer[Features], 2 ) > 0

What goes wrong?

  • Three arguments to AND. An error; use &&.
  • BLANK in tests. BLANK compares like 0 or an empty string: [x] = 0 is TRUE for BLANK; use ISBLANK or == (strict equal) when the difference matters.
  • IFERROR as a habit. It hides real errors and slows queries; prevent the error (DIVIDE, ISBLANK checks).
  • COALESCE with mixed types. COALESCE ( [Revenue], "none" ) makes a variant; keep one type.
  • SWITCH order. The first TRUE wins; put the strictest condition first.

Where can you check this?

Where do you start?

Rewrite one nested IF as SWITCH ( TRUE () ) and replace IF ( ISBLANK ( x ), y, x ) with COALESCE ( x, y ). Information functions are the tests you combine.

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.