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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Bug when using Calculate with variables

Hey, everyone.

 

I'm having a problem with Calculate and Variables.

 

This is the data I generated for the Proof of Concept

 

Capturar.PNG

 

3.PNG

 

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:

1.PNG

 

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:

2.PNG

 

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.

1 ACCEPTED SOLUTION
ChrisMendoza
Resident Rockstar
Resident Rockstar

@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] )
    )
)

1.PNG






Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

Proud to be a Super User!



View solution in original post

2 REPLIES 2
ChrisMendoza
Resident Rockstar
Resident Rockstar

@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] )
    )
)

1.PNG






Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

Proud to be a Super User!



mattbrice
Solution Sage
Solution Sage

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?

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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