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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
apatwal
Helper III
Helper III

DAX in building Rolling 4 Week Average

Hi,

 

I need help in building 4 week rolling average for "Net Change in Price and Cost" with below sample data

apatwal_0-1646156987085.png

 

Change in Price, Change in Cost and Net Change in Price and Cost are all 3 measures.

Net Change in Price and Cost = Change in Price - Change in Cost

 

I do have separate date table and the above data shows weekly data and my report has Invoice Date as Filter.

I need to calculate rolling 4 week average, here the tweak is that for first week start date 12/07 rolling average should be same while for next 12/14 it should (-4.80% + 0.18%)/2 = -2.31%

for 12/21 it should be (-4.80% + 0.18% - 0.04%)/3 = -1.55%

for 12/28 it should be (-4.80% + 0.18% - 0.04% -0.65%)/4 = -1.33%

for 01/04 it should be (0.18% - 0.04% -0.65% +1.03%)/4 = 0.13%

as shown below

apatwal_1-1646157306045.png

 

I have created below DAX but its not working as expected

Net Change in Price and Cost 4 Wk Rolling Avg =
var start_day = MIN('Date'[Week Start Date])-21
var end_day = MAX('Date'[Week Start Date])

return
CALCULATE([Net Change in Price and Cost],
DATESBETWEEN('Date'[Week Start Date],start_day,end_day),
REMOVEFILTERS('Date'[Week Start Date]))/CALCULATE(DISTINCTCOUNT('Date'[Week Start Date]),DATESBETWEEN('Date'[Week Start Date],start_day,end_day),REMOVEFILTERS('Date'[Week Start Date]))
 
Can someone help me on this??

 

1 ACCEPTED SOLUTION
v-stephen-msft
Community Support
Community Support

Hi @apatwal ,

 

Here's my solution.

1.Create a index measure

Index = 
CALCULATE (
    COUNT ( 'Date'[Week Start Date] ),
    FILTER (
        ALL ( 'Date' ),
        [Week Start Date] <= MAX ( 'Table'[Week Start Date] )
    )
)

vstephenmsft_3-1646378111282.png

 

 

2.Create another measure to get the rolling 4 week average.

Rolling 4 week average = 
CALCULATE (
    AVERAGEX ( 'Table', [Net Change in Prce and Cost] ),
    FILTER (
        ALL ( 'Table' ),
        [Index] <= MAXX ( 'Table', [Index] )
            && [Index]
                >= MAXX ( 'Table', [Index] ) - 3
    )
)

vstephenmsft_4-1646378130991.png

 

 

 

 

Best Regards,

Stephen Tao

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

5 REPLIES 5
v-stephen-msft
Community Support
Community Support

Hi @apatwal ,

 

Here's my solution.

1.Create a index measure

Index = 
CALCULATE (
    COUNT ( 'Date'[Week Start Date] ),
    FILTER (
        ALL ( 'Date' ),
        [Week Start Date] <= MAX ( 'Table'[Week Start Date] )
    )
)

vstephenmsft_3-1646378111282.png

 

 

2.Create another measure to get the rolling 4 week average.

Rolling 4 week average = 
CALCULATE (
    AVERAGEX ( 'Table', [Net Change in Prce and Cost] ),
    FILTER (
        ALL ( 'Table' ),
        [Index] <= MAXX ( 'Table', [Index] )
            && [Index]
                >= MAXX ( 'Table', [Index] ) - 3
    )
)

vstephenmsft_4-1646378130991.png

 

 

 

 

Best Regards,

Stephen Tao

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Somehow it didn't work for me. Index part didn't work. I also do not have week start date in sales table. Is there anyway i can connect with you to fix the issue i have. Thank you so much. Appreciate !

Anonymous
Not applicable

Thanks so much. This really worked for me. Hope to learn a lot from these community

amitchandak
Super User
Super User

@apatwal , Create a week rank on week start date or on week year

 

new columns

Week Rank = RANKX(all('Date'),'Date'[Week Start date],,ASC,Dense)
OR
Week Rank = RANKX(all('Date'),'Date'[Year Week],,ASC,Dense) //YYYYWW format

 

 

Avg of rolling 4 weeks , assuming [Net Change in Price and Cost] is measure

 

Last 4 weeks = CALCULATE(Averagex(Values('Date'[Week Rank]), [Net Change in Price and Cost]), FILTER(ALL('Date'),'Date'[Week Rank]>=max('Date'[Week Rank])-4 && 'Date'[Week Rank]<=max('Date'[Week Rank])))

 

Power BI — Week on Week and WTD
https://medium.com/@amitchandak.1978/power-bi-wtd-questions-time-intelligence-4-5-98c30fab69d3
https://community.powerbi.com/t5/Community-Blog/Week-Is-Not-So-Weak-WTD-Last-WTD-and-This-Week-vs-La...
https://www.youtube.com/watch?v=pnAesWxYgJ8

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

Hi @amitchandak 

 

thanks for your reply!

 

But looks there is some issue in calculations

apatwal_0-1646206288007.png

Used same DAX provided by you..

Rolling 4 Week Average =
CALCULATE(
AVERAGEX(
VALUES('Date'[Week Rank]),
MainTable[Net Change in Price and Cost]
),
FILTER(
ALL('Date'),
'Date'[Week Rank] >= MAX('Date'[Week Rank])-4 &&
'Date'[Week Rank] <= MAX('Date[Week Rank])
)
)

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!

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.