Returns different results depending on the value of an expression.
The expression to be evaluated.
If expression has this value the corresponding result will be returned.
The result to be returned if Expression has corresponding value.
If there are no matching values the Else value is returned.
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.
All result expressions and the else expression must be of the same data type.
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" )
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" )
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
Other related functions are:
Contributors: Alberto Ferrari, Marco Russo
MSDN documentation: https://docs.microsoft.com/en-us/dax/switch-function-dax