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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
arutsjak90
Helper I
Helper I

Rolling average with filter

Hi, I am trying to create 3months back rolling average with filter, becaue it needs to be rolling average per Plant. What I have created (or edited)  below.

*The table I'm working on was created with the Summarize function

COGSTotal = SUMMARIZE(COGS,COGS[TotalByPlantKey],COGS[Plant],COGS[FixedDate],"Total COGS",SUM(COGS[ CostOfSale]))

 

Total COGSFixedDatePlant3monthsRollingAverage
201/01/20201 
302/01/20201 
203/01/202012.33333
504/01/202013.33333
401/01/20202 
202/01/20202 
303/01/202023
304/01/202024
*A circular error appears
3MonthsRollingAverage =
VAR LastDate_ =
LASTDATE ( COGSTotal[FixedDate])
RETURN
CALCULATE (
AVERAGEX ( VALUES (COGSTotal[FixedDate]), CALCULATE ( SUM ( COGSTotal[Total COGS] ) ) ),
FILTER (
COGSTotal,
COGSTotal[Plant]=EARLIER(COGSTotal[Plant]) &
[FixedDate] <= LastDate_ && [FixedDate] > DATEADD ( LastDate_, -3, MONTH ) ) )
1 ACCEPTED SOLUTION
dedelman_clng
Community Champion
Community Champion

Hi @arutsjak90 -

 

Make sure you have a calendar table, linked to the "Fixed Date" column in your table, and then try this code

 

Rolling 3 Mo Avg =
VAR __ThisMonth =
    MAX ( COGS[FixedDate] )
VAR __3MoBack =
    EDATE ( __ThisMonth, -3 ) + 1
RETURN
    CALCULATE (
        SUM ( COGS[Total COGS] ),
        ALLEXCEPT ( COGS, COGS[Plant] ),
        DATESBETWEEN ( DateTab[Date], __3MoBack, __ThisMonth )
    ) / 3

 

2021-01-28 08_04_46-Window.png

 

Hope this helps

David

 

 

View solution in original post

9 REPLIES 9
dedelman_clng
Community Champion
Community Champion

Hi @arutsjak90 -

 

Make sure you have a calendar table, linked to the "Fixed Date" column in your table, and then try this code

 

Rolling 3 Mo Avg =
VAR __ThisMonth =
    MAX ( COGS[FixedDate] )
VAR __3MoBack =
    EDATE ( __ThisMonth, -3 ) + 1
RETURN
    CALCULATE (
        SUM ( COGS[Total COGS] ),
        ALLEXCEPT ( COGS, COGS[Plant] ),
        DATESBETWEEN ( DateTab[Date], __3MoBack, __ThisMonth )
    ) / 3

 

2021-01-28 08_04_46-Window.png

 

Hope this helps

David

 

 

Thank you for your time, but it is not working.. 
after entering this code, I get the sum of the whole Total COGS column divided by 3, not splited into plants and not rolling, very strange

 

pawelkajstura11_0-1611845338841.png

 

You need to create it as a measure, not as a column.

Thanks, but still the same result

 

pawelkajstura11_0-1611846559064.png

 

Please share a copy of your pbix with sensitive data removed. Dropbox, Google Drive, OneDrive, GitHub, etc.

I have sent it via private message

Please post the link in the thread, not PM, in case others want to look at it or help.

 

That being said, you did not create the proper relationships in your model, as instructed. This will cause the measure code to not work correctly.

 

Link "COSG[Fixed Date]" to DateTab[Date].

 

Hope this helps

David

https://drive.google.com/drive/folders/16GoTloQOeYLuvgu6eMl6M0LSgKA4iJzj?usp=sharing

Thank you! 

Is it possible to create calculated column instead of measure?

I would not recommend creating it as a calcaulted column. Measures can be used in mostly the same way as columns and are more dynamic.

 

If you run into an issue using the measure in further calculations, please start a new thread instead of continuing this one.

 

David

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

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.