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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
sonshine14916
Regular Visitor

Need help wit DAX Averagex(rolling average)

Hi,

I am not able to achieve the intended result with AverageX DAX expression. Can you please help to understand what is wrong with my AverageX DAX expression.

Background:

I have simplified the problem by having a table : MvgAvg which looks like this: It has a date column( always 1st of Month to indicate monthly sales) and Sales. I want to calculate the moving average for last 3 months. 

i.e for 1/12/2022 = Average sales of (Dec, Nov, Oct)  which is = (1 + 2 +3 )/ 3 = 2...

     for 1/11/2022 = Average sales of ( Nov, Oct,Sep) which is =  ( 2+3+4)/3 = 3 ..and so on..

sonshine14916_0-1649311529694.png

 

I have created a measure [Total Sales]  = SUMX(MvgAvg,MvgAvg[Sales])

I have created this Measure Avg: 

Avg = CALCULATE(
AVERAGEX(
SUMMARIZE(
'MvgAvg',
'MvgAvg'[Month],
'MvgAvg'[Year]),
[Total Sales]),

DATESINPERIOD(MvgAvg[Date],LASTDATE(MvgAvg[Date]), -3, MONTH)

))

 

But, the result is same as the value in Column Sales instead of '3 month rolling average'.

Can you please help me understand what is the problem. 

sonshine14916_1-1649311357970.png

 

Thanks in advance...

 

1 ACCEPTED SOLUTION
Jihwan_Kim
Super User
Super User

Hi,

Please check the below picture and the attached pbix file.

I tried to create a sample pbix like below.

I hope the below can help to provide some ideas on how to apply it to your data model.

 

Picture1.png

 

Value three months rolling avg: =
VAR newtable =
    CALCULATETABLE (
        ADDCOLUMNS (
            SUMMARIZE ( Data, 'Calendar'[Month-Year] ),
            "@valuetotal", [Value total:]
        ),
        DATESINPERIOD ( 'Calendar'[Date], LASTDATE ( 'Calendar'[Date] ), -3, MONTH )
    )
RETURN
    IF (
        HASONEVALUE ( 'Calendar'[Month-Year] ),
        AVERAGEX ( newtable, [@valuetotal] )
    )

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

View solution in original post

2 REPLIES 2
sonshine14916
Regular Visitor

Hi Jihwan_win,

thank you for your quick confirmation and it worked fine.

The difference I noticed is that logic works if the  Calendar(date) table relationship is existing.

Summarise with the MvgAvg date somehow doesn't return it.

I am trying to also see if it works with CALCULATE, AVERAGEX, SUMMARIZE, DATESINPERIOD. But thanks for finding the solution. 

 

Thanks.

Jihwan_Kim
Super User
Super User

Hi,

Please check the below picture and the attached pbix file.

I tried to create a sample pbix like below.

I hope the below can help to provide some ideas on how to apply it to your data model.

 

Picture1.png

 

Value three months rolling avg: =
VAR newtable =
    CALCULATETABLE (
        ADDCOLUMNS (
            SUMMARIZE ( Data, 'Calendar'[Month-Year] ),
            "@valuetotal", [Value total:]
        ),
        DATESINPERIOD ( 'Calendar'[Date], LASTDATE ( 'Calendar'[Date] ), -3, MONTH )
    )
RETURN
    IF (
        HASONEVALUE ( 'Calendar'[Month-Year] ),
        AVERAGEX ( newtable, [@valuetotal] )
    )

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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