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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
hvanstalle
Frequent Visitor

Rolling average with variable measure

Hello,

 

I created a Rolling average on our efficiency measurement and it works fine:

 

CALCULATE(IF([Efficiency (%)]<>BLANK(),
AVERAGEX(
DATESBETWEEN(
Datetable_months[Datum],
DATEADD(varDate, -12, MONTH),
DATEADD(varDate, 0, MONTH)
),
CALCULATE([Efficiency (%)])
)
)

 

But as I wanted to make this more flexible and also easy to switch between some other KPI measures I created the possibility to choose between the KPI they want. But now my rolling average isn't working anymore. Any idea why? It gives me the correct item, but without the average.

 

12MRol =
VAR varDate = LASTDATE(Datetable_months[Datum])
VAR varItem =
SWITCH(
SELECTEDVALUE(Selection[Item]),
"Efficiency (%)", [Efficiency (%)],
"Home load rate (%)", [Home load rate (%)],
"Load factor (%)",[Load factor (%)],
"Use factor (%)",[Use factor (%)],
"Time use factor (%)", [Time use factor (%)])

Return

CALCULATE(IF(varItem<>BLANK(),
AVERAGEX(
DATESBETWEEN(
Datetable_months[Datum],
DATEADD(varDate, -12, MONTH),
DATEADD(varDate, 0, MONTH)
),
CALCULATE(varItem)
)
))
 
Thanks in advance!
2 REPLIES 2
daxer-almighty
Solution Sage
Solution Sage

Please try to understand how variables in DAX work. Since DAX is a functional language, variables are really... CONSTANT. Once you've grabbed their values, they will never change throughout the execution of the code. So, doing calculate(varItem) = varitem. Always. There is no change here. By the way, a measure is always implicitly wrapped in CALCULATE, so doing this CALCULATE( [Measure] ) is the same as just calling the measure itself.

Ok, thanks for your feedback.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors