Optimizing IF and SWITCH expressions using variables
In DAX, variables are useful to write more readable code. Variables are also useful to optimize code execution, because a good usage of variables prevents multiple evaluations of the same expression. However, in certain conditions their use could be counterproductive, negatively affecting performance.
First, let us demonstrate how variables can improve code execution. For example, consider the following report computing the year-to-date over last year’s year-to-date, only considering months where there are sales in both years.