Join us for an expert-led overview of the tools and concepts you'll need to pass exam PL-300. The first session starts on June 11th. See you there!
Get registeredPower BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.
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
)
)
)
Solved! Go to Solution.
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.
Thank you. I understand conceptually what you mean, still trying to master it though.
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.
This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.
Check out the June 2025 Power BI update to learn about new features.
User | Count |
---|---|
19 | |
14 | |
13 | |
11 | |
8 |