Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
kpost
Super User
Super User

Seeking clarity on optimization re: Variables, Switch Statements

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 a different 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]
    )
 
 
QUESTION:
Is this more efficient than having each measure calculated as a variable and then returning the correct value, as below:
 
 
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
    )
 
 
Since this is a switch statement, I'm assuming the first one is more efficient because each of the three measures would only be calculated if the selection matches what is chosen in the slicer.  But what I don't know for sure is whether every variable in a DAX query is calculated every time, or if they are only calculated if they are needed during evaluation.
 
What is the order of operations here in terms of how Power BI processes DAX queries? I love using variables to increase readability of my DAX queries, but sometimes (situations like this), it seems like it will interfere with performance.  Any insight would be helpful, thank you.
1 ACCEPTED SOLUTION
v-tianyich-msft
Community Support
Community Support

Hi @kpost ,

 

In DAX, variables () are evaluated once at the point they are defined in the formula. This means that all variables in your second are calculated regardless of the statement's outcome.

Given this, your assumption is correct. The first approach, where the measures and are calculated directly within the statement based on the slicer selection, is more efficient. This is because, in this scenario, only the measure corresponding to the selected option is calculated.

 

Hope it helps!

 

Best regards,
Community Support Team_ Scott Chang

 

If this post helps then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

1 REPLY 1
v-tianyich-msft
Community Support
Community Support

Hi @kpost ,

 

In DAX, variables () are evaluated once at the point they are defined in the formula. This means that all variables in your second are calculated regardless of the statement's outcome.

Given this, your assumption is correct. The first approach, where the measures and are calculated directly within the statement based on the slicer selection, is more efficient. This is because, in this scenario, only the measure corresponding to the selected option is calculated.

 

Hope it helps!

 

Best regards,
Community Support Team_ Scott Chang

 

If this post helps then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Prices go up Feb. 11th.

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

Find out what's new and trending in the Fabric community.