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
spoony
Helper I
Helper I

Moving Average on filtered data

Hi, I got the moving average working as below:

 

 

MovingAverageCost = CALCULATE (
    AVERAGE ('Cost Table'[Cost]),
    DATESINPERIOD (
       'Cost Table'[Month],
        LASTDATE ( 'Cost Table'[Month] ),
        -3,
        MONTH
    )
)

 

But each time i take out certain months with a filter, i need the moving average to ignore those months taken out and calculate a new average based on the filtered months. Is there something i can add to the code to do that? To make it easier its always consecutive, like for [January, February, March, April], if i filter out [January, February],  [March]'s moving average should be equal to its average. 

 

 

 

1 ACCEPTED SOLUTION

Hi @spoony

 

How do you want your data to be used, in a matrix or a Visual?

 

Reason is that this would work below, but I am thinking it might not be exactly what you are looking to achieve.

 

You already have got your Average Measure [Avg]

 

Then you create a new measure which will count the months selected.

Month Count = CALCULATE(
     DISTINCTCOUNT('Cost Table'[Month] ),
          ALLSELECTED('Cost Table'[Month] )
    )

And then if you create this final measure it will then have the total correct, but if in a matrix each line will appear to be incorrect.

Moving Average = DIVIDE([Avg],[Month Count])

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!







Power BI Blog

View solution in original post

4 REPLIES 4
GilbertQ
Super User
Super User

Hi @spoony

 

This is a total guess, but it might work.

 

What about if you modify your measure to the following:

 

MovingAverageCost = CALCULATE (
    AVERAGE ('Cost Table'[Cost]),
    DATESINPERIOD (
       ALLSELECTED('Cost Table'[Month]),
        LASTDATE ( ALLSELECTED('Cost Table'[Month] )),
        -3,
        MONTH
    )
)




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!







Power BI Blog

Hi @GilbertQ,

 

It produces an error: Datesbetween and Datesinperiod functions are only accpeting date column reference as a first arguement.

Hi @spoony

 

How do you want your data to be used, in a matrix or a Visual?

 

Reason is that this would work below, but I am thinking it might not be exactly what you are looking to achieve.

 

You already have got your Average Measure [Avg]

 

Then you create a new measure which will count the months selected.

Month Count = CALCULATE(
     DISTINCTCOUNT('Cost Table'[Month] ),
          ALLSELECTED('Cost Table'[Month] )
    )

And then if you create this final measure it will then have the total correct, but if in a matrix each line will appear to be incorrect.

Moving Average = DIVIDE([Avg],[Month Count])

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!







Power BI Blog

Hi @GilbertQ, ya it needs to be on a graph and matrix. I'll test it out anyway.

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.