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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

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
July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Solution Authors