SWITCH DAX Function (Logical)
Returns different results depending on the value of an expression.
Syntax
Parameter | Attributes | Description |
---|---|---|
Expression |
The expression to be evaluated. |
|
Value | Repeatable |
If expression has this value the corresponding result will be returned. |
Result | Repeatable |
The result to be returned if Expression has corresponding value. |
Else | Optional |
If there are no matching values the Else value is returned. |
Return values
A scalar value coming from one of the Result expressions, if there was a match with Value, or from the Else expression, if there was no match with any Value.
Remarks
All result expressions and the else expression must be of the same data type.
Examples
A common use of SWITCH is to match the result of an expression with constant value:
SWITCH ( [A], 0, "Zero", 1, "One", 2, "Two", "Other numbers" )
However, the
By using TRUE as a first argument, SWITCH can replace a list of cascading IF statements.
The following code:
IF ( [A] > [B], "First case", IF ( [A] = [B], "Second case", IF ( [A] = 0, "Third case", "Fourth case" ) ) )
can be written as:
SWITCH ( TRUE, [A] > [B], "First case", [A] = [B], "Second case", [A] = 0, "Third case", "Fourth case" )
Related articles
Learn more about SWITCH in the following articles:
-
Optimizing mutually exclusive calculations
This article describes how to optimize DAX expressions with mutually exclusive calculations that might cause slow query performance. » Read more
-
Optimizing IF and SWITCH expressions using variables
This article describes how variables should be used in DAX expressions involving IF and SWITCH statements in order to improve performance. » Read more
Related functions
Other related functions are:
Last update: Jan 23, 2021 » Contribute » Show contributors
Contributors: Alberto Ferrari, Marco Russo
MSDN documentation: https://docs.microsoft.com/en-us/dax/switch-function-dax