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
Anonymous
Not applicable

Trouble with 12 Month Moving average

Hi,

I;m having trouble figuring out how to do 12 month moving averages.

My Model looks like this:

 

russgove2_0-1634673431256.png

 

So users have onservations on specific dates and they can enter a nimber of scores for each observation.

Note that the relationship between Date and Observation Date cannot be mad active because it will break other measures in my report.

 

So I created these measures to show the count of scores, the total of the scores and the  Average of the scores by month:

ScoreCount = CALCULATE(
COUNTROWS(Scores),
 
USERELATIONSHIP(Observations[observationdate],Dates[Date])
)

 
TotalScores =
CALCULATE(
Sum(Scores[score]),
USERELATIONSHIP(Observations[observationdate],Dates[Date])
)
 
AverageScore =
[TotalScores]
/
[ScoreCount]
 
 
So far so good:
russgove2_1-1634673788511.png

Now i am trying to keep it simple and just do a two  month rolling average, so the Feb numbers should conolidate the Jan and Feb numbers, Mar should consolidate feb and Mar).

 

DId some googling on this and avideo was telling me to do something like this:

Rolling2ScoreCount =
var MaxDate=CALCULATE(MAX(Dates[Date]),USERELATIONSHIP(Observations[observationdate],Dates[Date]))
return
CALCULATE(
COUNTROWS(Scores),
 
Filter(
ALL(Observations),
AND(
Observations[observationdate] <=MaxDate,
Observations[observationdate] > DATEADD(

))

 

 

but that  demo had an active relationship  and min doesnt.

 

Anyone know how i can calulate the rolling 2 month average without an active relationship?

 

(link to pbx on github attached)

 

TIA

Russell



https://github.com/russgove/pbi/blob/main/RollingAverageUserRelationship.pbix 

2 REPLIES 2
Anonymous
Not applicable

**bleep**, after writing all this out the answe dawned on me :

Rolling2ScoreCount =
var MaxDate=CALCULATE(MAX(Dates[Date]),USERELATIONSHIP(Observations[observationdate],Dates[Date]))
return
var MinDate=CALCULATE(MAX(Dates[Date]),
USERELATIONSHIP(Observations[observationdate],Dates[Date]),
DATEADD(Dates[Date],-2,MONTH)
)
return
CALCULATE(
COUNTROWS(Scores),
 
Filter(
ALL(Observations),
AND(
Observations[observationdate] <=MaxDate,
Observations[observationdate] > MinDate

)))
 
is there a better way to do this?

I don't see the point deactivating the relastionship between date table and Observations. Whatever, simple enough to author the measures,

R2M Count = 
CALCULATE(
    [ScoreCount],
    DATESINPERIOD( Dates[Date], MAX( Dates[Date] ), -2, MONTH )
)
R2M Total = 
CALCULATE(
    [TotalScores],
    DATESINPERIOD( Dates[Date], MAX( Dates[Date] ), -2, MONTH )
)

Screenshot 2021-10-20 025055.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

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.