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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Neha_PowerBI
Frequent Visitor

Cannot replace Measures with Variables

Hi,

I have a measure 

Calculated Buy  =
var SalesStock = [Sales] + [Stock]
Return
IF( [Buy] <> BLANK(),
IF( [Buy] > SalesStock, [Buy], SalesStock ))
and then I pass it through another measure
Calculated Buy New =
SUMX(
    SUMMARIZE(
        'Article', 'Article'[Division], 'Article'[Gender], 'Article'[Code], 'Article'[Type], 'Article'[RBU], "Total",[Calculated Buy Value]),
        [Total]    
)
I get the correct Total. But if I try to remove the Measure and include it in Calculated Buy as a variable. It's giving me incorrect Totals. Can anyone please help me to undesrant what's wrong in the below code:
Calculated Buy New =
var 
Buy  =
var SalesStock = [Sales ] + [Stock_]
var Check = 
IF( [Buy] <> BLANK(),
IF( [Buy] > SalesStock, [Buy], SalesStock ))
SUMX(
    SUMMARIZE(
        'Article', 'Article'[Division], 'Article'[Gender], 'Article'[Code], 'Article'[Type], 'Article'[RBU], "Total",[Check]),
        [Total]    
)
Thanks in advance. 

@variables @sumx

1 ACCEPTED SOLUTION
johnt75
Super User
Super User

Variables aren't really variable, they're constants. They're only calculated once when you define them, so they're useful for storing the results of a calculation if you want to refer to it multiple times but if you need to perform the same calculation multiple times with different filter contexts then you cannot use variables for that.

Also, a couple of general points. You shouldn't use SUMMARIZE to add calculated columns to a table, you should use SUMMARIZE just to give the static columns and wrap it in ADDCOLUMNS to add the calculations.

And you shouldn't use equality operators to compare to blank as that can give unexpected results, as 0 equals blanks. Instead of variable <> BLANK(), use NOT(ISBLANK(variable)).

View solution in original post

1 REPLY 1
johnt75
Super User
Super User

Variables aren't really variable, they're constants. They're only calculated once when you define them, so they're useful for storing the results of a calculation if you want to refer to it multiple times but if you need to perform the same calculation multiple times with different filter contexts then you cannot use variables for that.

Also, a couple of general points. You shouldn't use SUMMARIZE to add calculated columns to a table, you should use SUMMARIZE just to give the static columns and wrap it in ADDCOLUMNS to add the calculations.

And you shouldn't use equality operators to compare to blank as that can give unexpected results, as 0 equals blanks. Instead of variable <> BLANK(), use NOT(ISBLANK(variable)).

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.