OpenBI Docs Buy

SWITCH

The result paired with the first value equal to an expression, or the else value; SWITCH(TRUE(), …) tests conditions in turn.

Group: Logical. Compatible with DAX.

Syntax

SWITCH ( <expression>, <value>, <result> [, <value>, <result>] … [, <else>] )
ParameterTypeDescription
expressionValueThe value to match.
valueValue
repeatable
A value to compare it with.
resultValue
repeatable
The result when the value just before matches.
elseValue
optional
The result when no value matches; BLANK when left out.

Returns

Same type as its results. The result of the first value that matches, else the else value or BLANK.

Remarks

  • The values are tried in order and the first one that matches wins; only its result is evaluated.
  • SWITCH ( TRUE (), condition1, result1, condition2, result2, ..., else ) gives the result of the first condition that holds: the usual way to write bands. As the first TRUE wins, test 1000 or more before 100 or more.
  • A blank matches only a blank, not 0 or empty text, unlike =.
  • Text matches without regard to upper and lower case.
  • Every value must be of the expression's kind: a number cannot be matched with a text, and with TRUE () every value must be a condition. Otherwise the query fails.

Examples

Matching a value

EVALUATE
ROW (
    "2", SWITCH ( 2, 1, "one", 2, "two", "other" ),
    "9", SWITCH ( 9, 1, "one", 2, "two", "other" ),
    "9, no else", SWITCH ( 9, 1, "one", 2, "two" )
)
299, no else
twoother(blank)

Bands with SWITCH ( TRUE () )

EVALUATE
VAR Lines = DATATABLE ( "Liters", INTEGER, { { 40 }, { 250 }, { 1200 }, { 0 } } )
RETURN
    ADDCOLUMNS (
        Lines,
        "Size",
            SWITCH (
                TRUE (),
                [Liters] >= 1000, "Large",
                [Liters] >= 100, "Medium",
                [Liters] > 0, "Small",
                "None"
            )
    )
LitersSize
40Small
250Medium
1200Large
0None

Blank is matched strictly

EVALUATE
ROW (
    "Blank against 0", SWITCH ( BLANK (), 0, "matched", "else" ),
    "Blank against blank", SWITCH ( BLANK (), BLANK (), "matched", "else" )
)
Blank against 0Blank against blank
elsematched

IF, TRUE, SELECTEDVALUE, COALESCE.

Last updated · OpenBI Studio 0.3.74