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

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes! Register now.

Reply
beanboy
Frequent Visitor

When to use variables vs measures

Hi everyone, came across some DAX behavior I could use some clarification on. I have some pseudo-code below, but basically I am taking a unique Customers table and counting the rows that meet two conditions (based on the related Sales fact table). 

What is stumping me is trying to aggregate this at a State level, for example. If I have the code use the VARIABLES in the return statement, it does not aggregate at a State level. It still outputs correctly in a table of Customers, but does not aggregate. However, if I instead copy each variable to a separate measure, and then switch the DAX to reference the MEASURES, the measure now correctly aggregates at a state level. 

 

I know this for whatever reason is working as intented, so my question is why does it behave this way?

 

Also, while I'm here, is my syntax for COUNTROWS using a FILTER statement correct, or could it be done more efficiently?

 

#NewCustomers = 

VAR __PriorMonthStart = EOMONTH ( TODAY(), -2 ) +1



VAR __VARIABLENumSalesAllPrior = 
    CALCULATE (
        [NumSales],
        'SalesData'[DateReceived] < __PriorMonthStart
    )


VAR __VARIABLENumSalesMonthPrior = 
    CALCULATE ( 
        [NumSales],
        DATESINPERIOD (
            'Calendar'[Date],
            __PriorMonthStart,
            1,
            MONTH
        )
    )




RETURN 
    CALCULATE ( 
        COUNTROWS ( 'Customers(unique)' ),
        FILTER (
            'Customers(unique)',
            AND (
                [MEASURENumSalesAllPrior] = 0,
                [MEASURENumSalesMonthPrior] > 0
            )
        )
    )

 

1 ACCEPTED SOLUTION
lbendlin
Super User
Super User

Measures are dynamic, they recalculate for each different filter context.  You may not always want that, so you materialize intermediate results into variables (which is actually a misnomer as for all intents and purposes these are constants, not variables)  so the values are no longer impacted by subsequent measure calculations.

 

You can materialize intermediate steps into scalar or table variables but the final result of the measure always must be a scalar value.

View solution in original post

2 REPLIES 2
beanboy
Frequent Visitor

Thank you. I understand conceptually what you mean, still trying to master it though. 

lbendlin
Super User
Super User

Measures are dynamic, they recalculate for each different filter context.  You may not always want that, so you materialize intermediate results into variables (which is actually a misnomer as for all intents and purposes these are constants, not variables)  so the values are no longer impacted by subsequent measure calculations.

 

You can materialize intermediate steps into scalar or table variables but the final result of the measure always must be a scalar value.

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

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

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.