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
Jack007
Frequent Visitor

How does variable works with the Calculate Function?

I am learning variables in BI. 

I want to create a measure that calculates revenue for all years(remove filter context on calendar). I came up with 2 ways to do it. 

Method 1. Create a measure called Revenue,  

 
 

Jack007_0-1695806937545.png

then I created a new measure called Total Revenue using the code below.
Total Revenue =    
    var aa=
    CALCULATE([Revenue],ALL('Calendar'))
    return aa
This gives me the corrected result because revenue is same across years.

 

Method 2. 

Total Revenue =
    Var revenue_calc=
    SUMX('Sales by store','Sales by store'[quantity_sold]*'Sales by store'[unit_price])
    CALCULATE(revenue_calc,ALL('Calendar'))
    return Total Revenue
I'm not getting the correct result because revenue varies from year to year.
 
1 ACCEPTED SOLUTION
Alf94
Solution Supplier
Solution Supplier

Hi @Jack007 ,

 

Your second measure does not return the expected result because the revenue_calc value is calculated once, and never more. It is calculated when your variable is declared, when the calendar filter context is active. Your variable therefore stores revenues for the selected year. Then, in the "calculate()" part, this value is called directly and not recalculated, so the ALL() function doesn't change anything.

 

I am not sure my explanations are understandable 🙂 but you can find the idea in this article.

 

If I answered your question, please mark my post as a solution.

 

Best,

View solution in original post

1 REPLY 1
Alf94
Solution Supplier
Solution Supplier

Hi @Jack007 ,

 

Your second measure does not return the expected result because the revenue_calc value is calculated once, and never more. It is calculated when your variable is declared, when the calendar filter context is active. Your variable therefore stores revenues for the selected year. Then, in the "calculate()" part, this value is called directly and not recalculated, so the ALL() function doesn't change anything.

 

I am not sure my explanations are understandable 🙂 but you can find the idea in this article.

 

If I answered your question, please mark my post as a solution.

 

Best,

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!

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.

December 2024

A Year in Review - December 2024

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