[DAX] SWITCH

SWITCH = Nested If Statements

Syntax

SWITCH(expression,
value1, result1,
value2, result2,


else
)

  • value1 is a constant value to be matched with the evaluated result of expression.
  • result1 is any scalar expression (i.e. one that returns a scalar value) to be evaluated if the results of expression match the corresponding value1.
  • else is any scalar expression to be evaluated if the result of expression doesn’t match any of the value arguments
  • expression, value1, and result1 are the only mandatory parameters in SWITCH.

Example 1.

YearHalf =
SWTICH (
‘Calendar’[Month],
1, “1H”,
2, “1H”,
3, “1H”,
4, “1H”,
5, “1H”,
6, “1H”,
7, “2H”,
8, “2H”,
9, “2H”,
10, “2H”,
11, “2H”,
12, “2H”,
“-”
)

Conclusion

We can find that SWITCH is easier to understand than nested if statements. Here are some pros of SWITCH.

  • Easy to debug when there is an error message

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store