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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
TrentSP
Frequent Visitor

Rolling Weighted Average

I'm looking for help in trying to calculate a rolling weighted average in my dataset. Below is an example of the data I'm working with:

 

CategoryYearXY1-Year X/Y2-Year X/Y
A20008316650% 
B20008615356% 
C20008912472% 
A20017312658%53%
B20018816254%55%
C20016815644%56%
A20028512270%64%
B20029017452%53%
C20027112457%50%
A20039113866%68%
B200310013574%61%
C20039311085%70%
A20048615954%60%
B20046110260%68%
C20048512667%75%

 

The 2-Year X/Y is what I'm trying to calculate. Reading other posts in these forums, I came up with the following strategy. I created a new column called "Date" which is 

 

Date = DATE('Data'[Year], 1, 1)
 
I'm using this as the start of the period for the rolling average. Then I created another calculated column defined as
 
2-Year X/Y = CALCULATE( DIVIDE( SUM('Data'[X]), SUM('Data'[Y]) ), DATESBETWEEN('Data'[Date], 'Data'[Date], 'Data'[Date]+2*366) )
 
When I use this approach the results aren't coming out how I'd expect. There is definitely an issue with the way I'm calculating the 2-Year X/Y. My guess is that my issue is with how I'm using the DATESBETWEEN() function? I'm fairly new to BI so any advice here would be appreciated!
1 ACCEPTED SOLUTION
Ashish_Mathur
Super User
Super User

Hi,

You may download my PBI file from here.

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

9 REPLIES 9
TrentSP
Frequent Visitor

Thank you to everyone who replied, the issue is resolved on my end now! The reason why the solutions weren't working for me at first was because of an issue with how I was linking the "Calendar" table to the "Data" table. But this is now fixed and the results look great!

You are welcome.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
TrentSP
Frequent Visitor

Thanks for the responses!

 

@amitchandak Unfortunately, trying to update the function similar to what you explained did not fix my issue. Looks like I still have the same problems with the the data.

 

@Greg_Deckler My apologies if my original explanation wasn't clear. What I'm looking for is a way to calculate the two-year rolling average ratio of X / Y. For example, if I want the 2-Year X / Y for category C in the year 2003 then this would be (93+71) / (110+124) = 70% --> (XC for 2003 + XC for 2002) / (YC for 2003 + YC for 2002). I also want the ability to remove the A, B, C categories and have the 2-year X/Y ratio work properly for the year in total. Does that make sense?

 

@Ashish_Mathur The file you provided looks great! Everything seems to be working properly. However, when I applied the same formulas to my actual data set I couldn't seem to get it to work. Adapting from the work you provided I used the following for a calculated column:

 

2-Year X/Y = IF( ISBLANK( CALCULATE( SUM('Data'[X]), DATEADD('Data'[Date],-2,YEAR) ) ), BLANK(), CALCULATE( SUM('Data'[X]), DATESBETWEEN('Data'[Date], EDATE('Data'[Date], -12), 'Data'[Date]) ) ) / CALCULATE( SUM('Data'[Y]), DATESBETWEEN('Data'[Date], EDATE('Data'[Date],-12), 'Data'[Date]) )

 

Can you critique my formula above? Everything is coming through blank making me think the first SUM()/DATEADD() is not working properly. I tried applying everything exactly as it was laid out in your original file at first (creating a calendar table, creating measures to sum X and Y, etc.) but when it wasn't working I played with it ending with what I have above. I used a SUM() function instead of creating a summing measure and the DATEADD() function instead of PREVIOUSYEAR() so that I could use it for periods other than 2 years if desired. Why is this not correct / a valid methodology?

 

Thanks!!

Hi @TrentSP ,

 

Measure 3 = 
VAR x = 
CALCULATE(
    [X values],
    DATEADD('Calendar'[Date], -2, YEAR)
)
RETURN
IF(
    x = BLANK(),
    BLANK(),
    CALCULATE([X values], DATESBETWEEN('Calendar'[Date],EDATE(MIN('Calendar'[Date]),-12),MAX('Calendar'[Date])))
    /
    CALCULATE([y values],DATESBETWEEN('Calendar'[Date],EDATE(MIN('Calendar'[Date]),-12),MAX('Calendar'[Date])))
)

I can get the correct result if I use DATEADD() function.

o11.PNG

If you use @Ashish_Mathur 's formula, you must create a calendar table and the [year] column is from calendar table.

 

Best regards,
Lionel Chen

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

 

@TrentSP 

I also suggested a measure. So in case, it is used as a column it will not work. Anyway, @Ashish_Mathur , has provided a working solution. Test that as measure and let us know.

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Hi,

Mine's a measure (not a calculated column formula).


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Ashish_Mathur
Super User
Super User

Hi,

You may download my PBI file from here.

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Greg_Deckler
Community Champion
Community Champion

OK, walk me through what is supposed to happen in order to get your desired numbers. So how is that 53% supposed to be calculated? In just plain language.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...
amitchandak
Super User
Super User

One of the date to refer to you fact date for reference of rollup

I typically does it like this

Rolling 2 year = CALCULATE( DIVIDE( SUM('Data'[X]), SUM('Data'[Y]) ,DATESINPERIOD('Date'[Date],ENDOFMONTH(Data[Date]),-2,Year))
Rolling 2 year = CALCULATE( DIVIDE( SUM('Data'[X]), SUM('Data'[Y]) ,DATESINPERIOD('Date'[Date Filer],MAX(Data[Date]),-2,Year))

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.