Power BI is turning 10, and we’re marking the occasion with a special community challenge. Use your creativity to tell a story, uncover trends, or highlight something unexpected.
Get startedJoin us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.
Hey, everyone.
I'm having a problem with Calculate and Variables.
This is the data I generated for the Proof of Concept
I'm trying to obtain the sum of amount in each category, which is fairly easy using calculate such as:
Amount in Legal (no var) = CALCULATE(SUM('Planilha1 (2)'[Amount]);FILTER('Planilha1 (2)';'Planilha1 (2)'[Category] = "Legal"))
Result:
I can do this for each one and it would work.
The problem is when I try to use amount as a variable:
Amount in Legal = var amount = SUM('Planilha1 (2)'[Amount]) return CALCULATE(amount;FILTER('Planilha1 (2)';'Planilha1 (2)'[Category] = "Legal"))
Result:
This is only a silly example, but it has affected me because I like to use lots of variables to keep the code organized and I have about 100 measures in each report.
If someone else has encountered this problem let me know. I'm also uploading this to Power BI Issues.
I can upload my pbix example for anyone who asks me.
Thanks,
Bruno Rodrigues.
Solved! Go to Solution.
@Anonymous -
I might have chosen a different method that you may find useful based off of what you've shown:
Measure = IF ( ISFILTERED ( Planiha1[Client] ), SUM ( Planiha1[Amount] ), CALCULATE ( SUM ( Planiha1[Amount] ), ALLEXCEPT ( Planiha1, Planiha1[Category] ) ) )
Proud to be a Super User!
@Anonymous -
I might have chosen a different method that you may find useful based off of what you've shown:
Measure = IF ( ISFILTERED ( Planiha1[Client] ), SUM ( Planiha1[Amount] ), CALCULATE ( SUM ( Planiha1[Amount] ), ALLEXCEPT ( Planiha1, Planiha1[Category] ) ) )
Proud to be a Super User!
The problem is that variables are evaluated in the context they are initialized, not when they are used. In your example "amount" was already computed before it was used in your calculate function, so it returned the total of the "'Planilha1 (2)'[Amount]" column. ok?
This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.
Check out the June 2025 Power BI update to learn about new features.
User | Count |
---|---|
72 | |
71 | |
57 | |
38 | |
36 |
User | Count |
---|---|
82 | |
67 | |
61 | |
46 | |
45 |