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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
milkynight
Helper I
Helper I

4 week moving average for specific product

Hi all,

 

I would like to ask for your help/guidance on how to calculate 4w moving average for product codes/ product family. The idea is to have a Product code & Product family filters so the users can choose the product they would like to see the movement.

 

So far i can only build the 4WMA for 1 specific product code, and if i have total of 44 or more, i have to build 44+ times which is a manual process.

 

This is the link to get my data sample: https://drive.google.com/file/d/17G4tDpAsS0RtPI0-sAhVosqS2svfbF1E/view?usp=sharing 

 

I appreciate your help for a better way. Thanks much!

1 ACCEPTED SOLUTION
amitchandak
Super User
Super User

@milkynight , Create a new week year table with distinct year and week (Say Date)

 

Add columns

Year Week = [year]*100 +[week]

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

 

Try measures like

 

Last 4 weeks = CALCULATE(sum('Table'[sales]), FILTER(ALL('Date'),'Date'[Week Rank]>=max('Date'[Week Rank])-4 && 'Date'[Week Rank]<=max('Date'[Week Rank])))

 

Last 4 weeks Average = CALCULATE(Average ('Table'[sales]), FILTER(ALL('Date'),'Date'[Week Rank]>=max('Date'[Week Rank])-4 && 'Date'[Week Rank]<=max('Date'[Week Rank])))

 

Last 4 weeks Average = CALCULATE(sum('Table'[sales]), FILTER(ALL('Date'),'Date'[Week Rank]>=max('Date'[Week Rank])-4 && 'Date'[Week Rank]<=max('Date'[Week Rank])))/4

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

View solution in original post

2 REPLIES 2
milkynight
Helper I
Helper I

Thanks @amitchandak , I tried again and it worked nicely. Appreciate your help!

 

Just want to check if we need to have Week Rank column? As the Week column has been in the right order i think.

amitchandak
Super User
Super User

@milkynight , Create a new week year table with distinct year and week (Say Date)

 

Add columns

Year Week = [year]*100 +[week]

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

 

Try measures like

 

Last 4 weeks = CALCULATE(sum('Table'[sales]), FILTER(ALL('Date'),'Date'[Week Rank]>=max('Date'[Week Rank])-4 && 'Date'[Week Rank]<=max('Date'[Week Rank])))

 

Last 4 weeks Average = CALCULATE(Average ('Table'[sales]), FILTER(ALL('Date'),'Date'[Week Rank]>=max('Date'[Week Rank])-4 && 'Date'[Week Rank]<=max('Date'[Week Rank])))

 

Last 4 weeks Average = CALCULATE(sum('Table'[sales]), FILTER(ALL('Date'),'Date'[Week Rank]>=max('Date'[Week Rank])-4 && 'Date'[Week Rank]<=max('Date'[Week Rank])))/4

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

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.