Table of Contents

Code action DI007 (Improvement) Simplify SWITCH statement

Description

A SWITCH statement that specifies TRUE() for the <Expression> argument, and where all <Value> arguments are simple comparisons of the same variable/measure, can be simplified.

Example

Change:

SWITCH(
	TRUE(),
	[Selected Currency] = "EUR", [Total Sales EUR],
	[Selected Currency] = "USD", [Total Sales USD],
	[Selected Currency] = "DKK", [Total Sales DKK],
	[Total Sales]
)

To:

SWITCH(
	[Selected Currency],
	"EUR", [Total Sales EUR],
	"USD", [Total Sales USD],
	"DKK", [Total Sales DKK],
	[Total Sales]
)

Why is Tabular Editor suggesting this?

A common DAX pattern to specify a conditional expression with more than 2 conditions, is to use the SWITCH statement with TRUE() as the first argument. By using this technique, one can then provide condition-expression-pairs for the remaining SWITCH arguments. The first condition that evaluates to TRUE will determine the result of the SWITCH statement.

However, when all conditions are simple equality comparisons against the same value ([Selected Currency] in the example above), the SWITCH statement should be simplified to its intended form, where the first argument is the expression to evaluate, and the remaining arguments are pairs of values and results. The first value to match the expression will determine the result of the SWITCH statement.