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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
donpep0
Frequent Visitor

Moving average 3 weeks

I am new to Power Query and I'm trying to create a query template that applies a 3 moving average to the volume and sales for the next 3 weeks grouped by weekday for each product.

My input table looks like this.

INPUT
SPAN07/01/2022Fri345589
SPAN07/08/2022Fri453124
SPAN07/08/2022Fri453124
SPAN07/15/2022Fri789793
SPAN07/22/2022Fri564464
SPAN07/29/2022Fri645548
PC07/01/2022Fri689645
PC07/08/2022Fri564646
PC07/08/2022Fri646754
PC07/15/2022Fri564744
PC07/22/2022Fri549646
PC07/29/2022Fri979319
SPAN07/06/2022Wed235542
SPAN07/13/2022Wed234589
SPAN07/20/2022Wed354545
SPAN07/27/2022Wed545435
PC07/06/2022Wed656646
PC07/13/2022Wed968346
PC07/13/2022Wed574731
PC07/20/2022Wed878464
PC07/27/2022Wed579643


Note: I didn't include all the data but there is data for each weekday (Mon, Tue, Wed, Thu, Fri, Sat, Sun) and there are more products.

Below are the steps that I've been doing:
Sort the data first by call type and then by weekday.
Then grouped the rows and create an index for each product-weekday with the next code:

 

#"Grouped Rows" = Table.Group(#"Sorted Rows", {"product", "weekday"}, {{"Index", each
Table.AddIndexColumn(_,"Index", 1,1), type table}}),


#"Expanded Index" = Table.ExpandTableColumn(#"Grouped Rows", "Index", {"date", "volume", "sales",
"Index",} , {"Index.date", "Index.volume", "Index.sales", "Index.Index",}

Then I apply the 3 moving Average to the volume:
step1 = Table.AddColumn(#"Expanded Index", "Average_vol", each if [Index.Index] > 2 then List.Average(List.Range(#"Expanded Index"[Index.volume], _[Index.Index] -3, 3)) else null, type number)

So now I have this:

 
 

 

SPAN07/06/2022Wed2355421Null
SPAN07/13/2022Wed2345892Null
SPAN07/20/2022Wed3545453558.666667
SPAN07/27/2022Wed5454354523
SPAN07/01/2022Fri3455891Null
SPAN07/08/2022Fri4531242Null
SPAN07/08/2022Fri4531243279
SPAN07/15/2022Fri7897934347
SPAN07/22/2022Fri5644645460.333333
SPAN07/29/2022Fri6455486601.666667
PC07/06/2022Wed6566461Null
PC07/13/2022Wed9683462Null
PC07/13/2022Wed5747313574.333333
PC07/20/2022Wed8784644513.666667
PC07/27/2022Wed5796435612.666667
PC07/01/2022Fri6896451Null
PC07/08/2022Fri5646462Null
PC07/08/2022Fri6467543681.666667
PC07/15/2022Fri5647444714.666667
PC07/22/2022Fri5496465714.666667
PC07/29/2022Fri9793196569.666667


the highlighted cells are the moving average expected for the next week.

It's working but now I need to include the numbers in red for the "next 2-week average" and the 3rd

So now I'm stuck on that step and I need to create the template.

Do you know how can I apply the moving average for the next 2 weeks including the calculated numbers for the next week(red numbers)?

1 REPLY 1
v-jingzhang
Community Support
Community Support

Hi @donpep0 

 

What do you currently have for the next 2nd and 3rd week? If you have null values in them, you can possibly use Fill Down feature. After that, transform the column to show null for index 1 and 2. 

vjingzhang_0-1662017486155.png

 

I'm not sure how to tell which week is expected to be the current week thus which values should be expected for next weeks. Can you explain the logic why you highlight those values? Is it because the latest week in the table is the week that 07/29/2022 lies in? If so, do we need to add new rows for those "next dates"?

 

Best Regards,
Community Support Team _ Jing

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.