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

Get inspired! Check out the entries from the Power BI DataViz World Championships preliminary rounds and give kudos to your favorites. View the vizzies.

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
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code FABINSIDER for a $400 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

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

March2025 Carousel

Fabric Community Update - March 2025

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

Top Solution Authors
Top Kudoed Authors