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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

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
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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