Kevin Post on 28 May 2024 15:59:43
My suggestion is born of this post I made on the Power BI Forum.
https://community.fabric.microsoft.com/t5/Desktop/Seeking-clarity-on-optimization-re-Variables-Switch-Statements/m-p/3950901#M1264840
Essentially it boils down to this:
DAX queries should be evaluated 'from the bottom up', such that variables are only calculated if needed to evaluate the (explicit or implied) return statement.
Here is the specific scenario laid out in my question:
Let's say there is a single-select slicer with the following options from a column, 'Selection_Options'[Options]:
"Inbound"
"Outbound"
"Total"
In an accompanying line graph visual, depending on the slicer selection, a measure with a switch statement will be used to display the corresponding measure.
Here is the accompanying measure used in the visual.
Switch Measure =
VAR Selection = SELECTEDVALUE('Selection_Options'[Options])
RETURN
SWITCH(
TRUE(),
Selection = "Inbound", [Received],
Selection = "Outbound", [Shipped],
Selection = "Total", [Total]
)
As it stands, this measure is MORE efficient, in this scenario, than the following measure, because every single variable is calculated every single time the DAX query evaluates.
Switch Measure =
VAR Selection = SELECTEDVALUE('Selection_Options'[Options])
VAR inbound = [Received]
VAR outbound = [Shipped]
VAR total = [Total]
RETURN
SWITCH(
TRUE(),
Selection = "Inbound", inbound,
Selection = "Outbound", outbound,
Selection = "Total", total
)
My suggestion is to modify the way DAX calculations are evaluated, such that they would be equivalently efficient. It would help Microsoft's Power BI stay competitive with other vendors of data analytics software. And it would help all users at all times, there is no scenario in which unnecessary variable evaluation helps efficiency.
If, for example, "Total" is selected in the slicer, there should be no need for DAX to evaluate the variables "inbound" and "outbound". To be perfectly frank I was shocked to discover Microsoft engineers had not already done this!