Forum Discussion
When does a variable gets calculated? Before or after RETURN?
The DAX language gets run top to bottom. With how you have written your code, all 4 of your complex code will be run everytime and will be very slow. On top of this, the engine scans your code and tries to only load the tables it needs.
If you were hoping that only code will be run when its needed, the switch statement is designed to do that but it too has its own limits. You would need to put you 'complex calc' inside the switch statement (or any IF statement) to achieve the same effect of only running the code when its needed. There will be occasions where a switch statement will be too big or complex and the optimiser will break and everything will load, giving you headaches.
You can get around this by using Calculation groups to take the place of the switch statement.
As an addition to my reply. Variables are best used when you want to calculate something up front and store the result, to be reused over and over again later in the measure. I often also use variables for things that I will need to change when i copy and paste the code into a new measure, to reuse the method.