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

Get inspired! Check out the entries from the Power BI DataViz World Championships preliminary rounds and give kudos to your favorites. View the vizzies.

Reply
cld32650
Frequent Visitor

Moving Average of Multiple Series

I would like to calculate the moving average of multiple series data as a measure. The data is monthly spanning 40+ years. 

 

The columns of data are "Scenario", "Year", "Series", and "Value". A slicer filters the table for "Scenario", but no filtering is performed on series because I want to visualize the moving average of each series in one figure. Table below shows how data is currently formatted (Aggregated to annual for simplicity).

ScenarioYearSeriesValue
120201100
120211106
120221109
12020298
12021299
120222105

Ignoring the edge cases in this scenario, the 3 year moving average in 2021 for series 1 would return 105; the 3yr moving average in 2021 for series 2 would return 100.67.

The data will be displayed on a scatter plot as a tooltip, where the scatter plot displays other data for every year, and series, filtered by a scenario slicer. 

 

To get started I have tried to move the calculation to a calculated column, but the calculation is simply returning the same number from the Value column on the same row (almost like a copy/paste to another column).

Below is the code I have tried. Any suggestions to get the calculation correct would be greatly appreciated - keep in mind please the end goal is to move all of this to a measure.

 

Var PeriodToUse = 
    Filter(
        All(testtable[Year]),
        And(
            testtable[Year] < 2030,
            testtable[Year] > 2026
        )
    )
Var Result =
    Calculate(
        AVERAGEX('testtable',[Value]),
        PeriodToUse
    )

RETURN
Result

 

 

1 ACCEPTED SOLUTION
suparnababu8
Super User
Super User

Hello @cld32650 

 

suparnababu8_0-1739494919197.png

I hope you are looking for this. Attched pbix file here for your reference.

 

 

Thanks!

 

 

View solution in original post

5 REPLIES 5
suparnababu8
Super User
Super User

Hello @cld32650 

 

suparnababu8_0-1739494919197.png

I hope you are looking for this. Attched pbix file here for your reference.

 

 

Thanks!

 

 

Thank you for the worked example. For others that are interested, the code within the file is copied below -  note, this creates a trailing moving average, whereas the question is about a centered moving average. An additional variable to define the end year was the only needed adjustment to finalize the code.

 

Moving Average 3 Years = 
VAR CurrentYear = MAX('MovingAvgExample'[Year])
VAR StartYear = CurrentYear - 2
RETURN
CALCULATE(AVERAGEX(
        FILTER(ALL('MovingAvgExample'),
            'MovingAvgExample'[Year] >= StartYear && 'MovingAvgExample'[Year] <= CurrentYear &&
            'MovingAvgExample'[Series] = MAX('MovingAvgExample'[Series]) && 'MovingAvgExample'[Scenario] = MAX('MovingAvgExample'[Scenario])),'MovingAvgExample'[Value]  ))

 

Hi,@cld32650 
We are delighted that you have found a solution and are willing to share it.

 

Accepting your post as the solution is incredibly helpful to our community, as it enables members with similar issues to find answers more quickly.

 

Thank you for your valuable contribution to the community, and we wish you all the best in your work.

 

Best Regards,

Leroy Lu

ryan_mayu
Super User
Super User

you can try this

 

Column = CALCULATE(average('Table'[Value]),ALLEXCEPT('Table','Table'[Scenario],'Table'[Series]))
 
11.PNG
or 
 
Measure = CALCULATE(average('Table'[Value]),ALLEXCEPT('Table','Table'[Scenario],'Table'[Series]))
12.PNG
 




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

Proud to be a Super User!




Thank you for the response. I have not verified this solution because another workable response was provided earlier.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

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

Feb2025 NL Carousel

Fabric Community Update - February 2025

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

Top Kudoed Authors