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>] )
| Parameter | Type | Description |
|---|---|---|
expression | Value | The value to match. |
value | Value repeatable | A value to compare it with. |
result | Value repeatable | The result when the value just before matches. |
else | Value 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" )
)
| 2 | 9 | 9, no else |
|---|---|---|
| two | other | (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"
)
)
| Liters | Size |
|---|---|
| 40 | Small |
| 250 | Medium |
| 1200 | Large |
| 0 | None |
Blank is matched strictly
EVALUATE
ROW (
"Blank against 0", SWITCH ( BLANK (), 0, "matched", "else" ),
"Blank against blank", SWITCH ( BLANK (), BLANK (), "matched", "else" )
)
| Blank against 0 | Blank against blank |
|---|---|
| else | matched |
Related functions
IF, TRUE, SELECTEDVALUE, COALESCE.