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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
swisdom
Helper I
Helper I

Get rolling average of a Measure

I have two datasets, a series of Returns and a series of Shipments. I have a measure calculating the monthly Return Rate over the running total of shipments. I would like to create another measure which is a rolling 6 month average of that Return Rate, but the DAX code I have written is not doing what I want. Its simple enough in Excel but I'm having some trouble with the DAX.

 

Link to sample .pbix file 


The code I have used:

 

6 Mo Avg Rate = 
VAR NumOfMonths = 6
VAR LastCurrentDate =
    MAX ( 'Date'[Date] )
VAR Period =
    DATESINPERIOD ( 'Date'[Date], LastCurrentDate, - NumOfMonths, MONTH )
VAR Result =
    CALCULATE (
        AVERAGEX (
            VALUES ( 'Date'[Date] ),
            [Return Rate]
        ),
        Period
    )
VAR FirstDateInPeriod = MINX ( Period, 'Date'[Date] )
VAR LastDateWithRMA = MAX ( 'RMA'[Date Submitted] )
RETURN
    IF ( FirstDateInPeriod <= LastDateWithRMA, Result )

 


Results I get from that code:

swisdom_0-1641607148662.png


Results I would *expect* to get:

swisdom_1-1641607179601.png

 

Any tips?

2 ACCEPTED SOLUTIONS
aj1973
Community Champion
Community Champion

Hi @swisdom 

In your date table add a column for Year Month

aj1973_0-1641690466924.png

in your measure replace Date by Year Month

aj1973_1-1641690527875.png

Replace the fields Calendar Year and Month by Year Month

aj1973_3-1641690676813.png

 

Result

aj1973_2-1641690569976.png

 

 

 

Regards
Amine Jerbi

If I answered your question, please mark this thread as accepted
and you can follow me on
My Website, LinkedIn and Facebook

View solution in original post

aj1973
Community Champion
Community Champion

@swisdom 

In your measure change these 2

aj1973_0-1641739181998.png

 

Also don't forget to render the column Year Month to Date format

aj1973_1-1641739274169.png

 

Let me know otherwise I will send you the file.

 

Regards
Amine Jerbi

If I answered your question, please mark this thread as accepted
and you can follow me on
My Website, LinkedIn and Facebook

View solution in original post

10 REPLIES 10
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/
amitchandak
Super User
Super User

@swisdom , Try with Month Year

 

change this

 

VAR Result =
    CALCULATE (
        AVERAGEX (
            VALUES ( 'Date'[Year Month] ),
            [Return Rate]
        ),
        Period
    )
Full Power BI Video 20 Hours YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

swisdom_0-1641689144223.png

That didn't seem to quite do the trick but its a little more normal?

aj1973
Community Champion
Community Champion

Hi @swisdom 

In your date table add a column for Year Month

aj1973_0-1641690466924.png

in your measure replace Date by Year Month

aj1973_1-1641690527875.png

Replace the fields Calendar Year and Month by Year Month

aj1973_3-1641690676813.png

 

Result

aj1973_2-1641690569976.png

 

 

 

Regards
Amine Jerbi

If I answered your question, please mark this thread as accepted
and you can follow me on
My Website, LinkedIn and Facebook

So weird I did exactly that and I'm still getting the same results? Can you take a look at my file below and double check? I see that you got the correct results in your version so I'm not sure what I did wrong.

Link to file 

Well done @Ashish_Mathur 👍



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Thank you @parry2k 


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

@swisdom 

In your measure change these 2

aj1973_0-1641739181998.png

 

Also don't forget to render the column Year Month to Date format

aj1973_1-1641739274169.png

 

Let me know otherwise I will send you the file.

 

Regards
Amine Jerbi

If I answered your question, please mark this thread as accepted
and you can follow me on
My Website, LinkedIn and Facebook

That did it, thank you so much!

aj1973
Community Champion
Community Champion

@swisdom Welcome, eventhough I thought @Ashish_Mathur solution was more close to what you were looking for.

Regards
Amine Jerbi

If I answered your question, please mark this thread as accepted
and you can follow me on
My Website, LinkedIn and Facebook

Helpful resources

Announcements
June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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