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

Learn from the best! Meet the four finalists headed to the FINALS of the Power BI Dataviz World Championships! Register now

Reply
Anonymous
Not applicable

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 carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.