Power Query Running Totals For Each Subsequent Months

legalhustler

Well-known Member
Joined
Jun 5, 2014
Messages
1,214
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am using Power Query for Excel and wanted a dynamic running total for each month shown on the far right. The source data comes from another system so after the end of December it automatically shows January, February, March etc. For each row starting with Oct as the first month I want the cumulative running total for the current month + the previous month values.

So for the first row Oct should show $10, Nov $30 (10+20) and Dec $60 (10+20+30). The second row should show $5, $15 (5+10), and $30 (5+10+15) for Oct, Nov, Dec, respectively.

When January data shows I want it to dynamically add the values for Oct+Nov+Dec. I'm hoping the solution is efficient so it doesn't slow down my query. Thanks in advance

RegionStationIDTypeOctNovDec
North1110000Low$10$20$30
South2229999Low$5$10$15
East3338888Medium$20$40$60
West4447777High$3$6$9
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
This code replaces the month columns with the running totals from the first month column found formatted as "mmm". Is that what you are asking for?

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    tcc  =Table.ColumnCount(Source),
    tcn = Table.ColumnNames(Source),
    MonthNameList = List.Generate(()=>1,(x)=> x<13,(x)=> x+1, (x)=> Text.Start(Date.MonthName(#date(2022,x,1)), 3)),
    numLeadCols = List.PositionOfAny(tcn, MonthNameList),
    tblAddListCol = Table.AddColumn(Source,"Running", each List.Generate(()=>1,(x)=>x<=tcc-numLeadCols, (x)=> x+1, (x)=> List.Sum(List.Range(Record.ToList(_),numLeadCols,x)))),
    monthlist = List.Skip(tcn,numLeadCols),
    tblDeleteCols = Table.RemoveColumns(tblAddListCol,monthlist),
    Extract = Table.TransformColumns(tblDeleteCols, {"Running", each Text.Combine(List.Transform(_, Text.From), ","), type text}),
    Split = Table.SplitColumn(Extract, "Running", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), monthlist),
    MonthTypeList = List.Transform(monthlist, each {_, Currency.Type}),
    ChgType = Table.TransformColumnTypes(Split,MonthTypeList)
in
    ChgType
 
Upvote 0
Maybe try

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Month = List.Skip(Table.ColumnNames(Source),4),
    Arec = Table.AddColumn(Source, "M", each Record.FromList(List.Skip(List.Accumulate(List.Skip(Record.ToList(_),4),{0},(s,l)=> s&{List.Last(s)+l } ),1),Month)),
    Expanded = Table.ExpandRecordColumn(Table.RemoveColumns(Arec,Month), "M", Month)
in
    Expanded
 
Upvote 0
If you have an actual question, you are better of starting a new thread & posting your data.
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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