Custom Column to Detect Past 3 Months and Calculate Average in Power Query

programsam

Board Regular
Joined
Feb 10, 2016
Messages
123
Greetings, I have a file that updates monthly and when a new month starts, a column is added dynamically. I'm trying to find a way to have Power Query detect the prior three months and then average them out. I cannot do this by column position alone as every month a new column will be added to the right.

In the image below, I'm trying to have it detect May, June and July 2023 and average out the values in the columns.

1691094810381.png
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
For some reason I cannot edit the original post but ideally, I'd like to have a 3 month average as well as an average based on the # of days within the 3 month period.
 
Upvote 0
I put the specific months in my language to test.

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    col = List.Accumulate({-1,-2,-3}, {}, (s,c) => let dt = Date.AddMonths(Date.From(DateTime.LocalNow()),c) in  s & {Text.Proper(Date.MonthName(dt) & " " & Text.From(Date.Year(dt)))}),
    delCols = Table.SelectColumns(Source,col),
    result = Table.AddColumn(delCols, "Average", each List.Average(Record.FieldValues(_)))
in
    result


Book1
UVWXYZAAABACADAE
1February 2022March 2022February 2023February 2024March 2023April 2023Mei 2023Juni 2023Juli 2023MTD
2123344567
3
4
5
6Juli 2023Juni 2023Mei 2023Average
76545
8
Sheet1
 
Upvote 0
Or you could just add the average at the end

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    cols = List.Accumulate({-1,-2,-3}, {}, (s,c) => let dt = Date.AddMonths(Date.From(DateTime.LocalNow()),c) in  s & {Text.Proper(Date.MonthName(dt) & " " & Text.From(Date.Year(dt)))}),
    result = Table.AddColumn(Source, "Average", (r)=> List.Average(List.Transform(cols, each Record.Field(r,_))))
in
    result
 
Upvote 1
Solution
Or you could just add the average at the end

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    cols = List.Accumulate({-1,-2,-3}, {}, (s,c) => let dt = Date.AddMonths(Date.From(DateTime.LocalNow()),c) in  s & {Text.Proper(Date.MonthName(dt) & " " & Text.From(Date.Year(dt)))}),
    result = Table.AddColumn(Source, "Average", (r)=> List.Average(List.Transform(cols, each Record.Field(r,_))))
in
    result
this one worked without issue, thank you!
 
Upvote 0

Forum statistics

Threads
1,223,322
Messages
6,171,448
Members
452,404
Latest member
vivek562

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top