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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

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
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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