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

Get certified in Microsoft Fabric—for free! For a limited time, the Microsoft Fabric Community team will be offering free DP-600 exam vouchers. Prepare now

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
OCT PBI Update Carousel

Power BI Monthly Update - October 2024

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

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

October NL Carousel

Fabric Community Update - October 2024

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