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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
rmsaunders
Frequent Visitor

Variable usage in CALCULATE statement

Hello. I'm new to Power BI and DAX, and I'm trying to get the following code more compact:
 
This works:
 
calc_TY_Sales_YTD =
VAR var_Metric = SELECTEDVALUE(table_Sales_Metrics[Metric])
VAR var_TY_sales_YTD =
SWITCH(TRUE(),
var_Metric = "Units", CALCULATE(SUM(SALES_TABLE[qty_sold]), DATESBETWEEN(DATE_TABLE[date], [calc_Report_Start_Date], [calc_Report_End_Date])),
var_Metric = "Cost $", CALCULATE(SUM(SALES_TABLE[dollar_sold]), DATESBETWEEN(DATE_TABLE[date], [calc_Report_Start_Date], [calc_Report_End_Date])),
var_Metric = "Retail $", CALCULATE(SUM(SALES_TABLE[retail_sold]), DATESBETWEEN(DATE_TABLE[date], [calc_Report_Start_Date], [calc_Report_End_Date])))
 
I'd rather do this...
 
calc_TY_Sales_YTD =
VAR var_Metric = SELECTEDVALUE(table_Sales_Metrics[Metric])
VAR var_Sales_measure =
SWITCH(TRUE(),
var_Metric = "Units", SUM(SALES_TABLE[qty_sold]),
var_Metric = "Cost $", SUM(SALES_TABLE[dollar_sold]),
var_Metric = "Retail $", SUM(SALES_TABLE[retail_sold]))
VAR var_TY_sales_YTD =
CALCULATE(var_Metric, DATESBETWEEN(DATE_TABLE[date], [calc_Report_Start_Date], [calc_Report_End_Date]))

...or this...
 
calc_TY_Sales_YTD =
VAR var_Metric = SELECTEDVALUE(table_Sales_Metrics[Metric])
VAR var_Sales_measure =
SWITCH(TRUE(),
var_Metric = "Units", SALES_TABLE[qty_sold],
var_Metric = "Cost $", SALES_TABLE[dollar_sold],
var_Metric = "Retail $", SALES_TABLE[retail_sold])
VAR var_TY_sales_YTD =
CALCULATE(SUM(var_Metric), DATESBETWEEN(DATE_TABLE[date], [calc_Report_Start_Date], [calc_Report_End_Date]))
 
...but neither of thise seem to work.
 
Am I stuck with the first method, or am I missing something?
 
Thanks.
1 ACCEPTED SOLUTION
v-jiascu-msft
Microsoft Employee
Microsoft Employee

Hi @rmsaunders,

 

Do you have any performance issue? Or just want to compact the formula? If so, please try out this one. 

 

calc_TY_Sales_YTD =
VAR var_Metric =
    SELECTEDVALUE ( table_Sales_Metrics[Metric] )
VAR var_TY_sales_YTD =
    CALCULATE (
        SWITCH (
            var_Metric,
            "Units", SUM ( SALES_TABLE[qty_sold] ),
            "Cost $", SUM ( SALES_TABLE[dollar_sold] ),
            "Retail $", SUM ( SALES_TABLE[retail_sold] )
        ),
        DATESBETWEEN (
            DATE_TABLE[date],
            [calc_Report_Start_Date],
            [calc_Report_End_Date]
        )
    )
RETURN
    var_TY_sales_YTD

If it doesn't help, please provide a sample that we can download or copy with. Please mask the sensitive parts first.

 

 

 

Best Regards,

Community Support Team _ Dale
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

3 REPLIES 3
v-jiascu-msft
Microsoft Employee
Microsoft Employee

Hi @rmsaunders,

 

Do you have any performance issue? Or just want to compact the formula? If so, please try out this one. 

 

calc_TY_Sales_YTD =
VAR var_Metric =
    SELECTEDVALUE ( table_Sales_Metrics[Metric] )
VAR var_TY_sales_YTD =
    CALCULATE (
        SWITCH (
            var_Metric,
            "Units", SUM ( SALES_TABLE[qty_sold] ),
            "Cost $", SUM ( SALES_TABLE[dollar_sold] ),
            "Retail $", SUM ( SALES_TABLE[retail_sold] )
        ),
        DATESBETWEEN (
            DATE_TABLE[date],
            [calc_Report_Start_Date],
            [calc_Report_End_Date]
        )
    )
RETURN
    var_TY_sales_YTD

If it doesn't help, please provide a sample that we can download or copy with. Please mask the sensitive parts first.

 

 

 

Best Regards,

Community Support Team _ Dale
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

Greg_Deckler
Super User
Super User

Hmm, probably a better way, can you share sample data that can be copied and pasted so that I can try a few things? Please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490.

 

Also, there are some techniques here that might help.

https://community.powerbi.com/t5/Quick-Measures-Gallery/Time-Intelligence-quot-The-Hard-Way-quot-TIT...

 

 



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

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!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.