Automate additional Columns in Power Query

ecrodrig

Board Regular
Joined
Jan 21, 2022
Messages
99
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I have a data source with months of the year. Each month I add a new column for the current month. An example I currently have Jan - July and then a Totals Column. Everything shows up fine in Power Query but when I add August the August column is showing up after the Totals Column rather than the July Column. I see where the issue is. The Column headers are hard coded. I needed to duplicate a column and once I reordered that column it hardcoded the headers. How can I automate it so that the headers are not hard coded? I was able to fix a previous issue where it wasn't detecting any new columns now I just need to figure out how to automate the hardcoded headers.

Any help is appreciated. Thanks
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Months = List.Accumulate({1..12}, {}, (s,c)=> s & {Text.Start(Date.MonthName(#date(1,c,1)),3)}),
    tcn = Table.ColumnNames(Source),
    typelist = List.Accumulate(tcn, {}, (s,c)=> if List.Contains(Months,c) then s & {{c, type number}} else s),
    tbl = Table.TransformColumnTypes(Source, typelist),
    MonthColumns = List.Zip(typelist){0},
    tbl1 = Table.AddColumn(tbl, "Total", each List.Sum(Record.ToList(Record.SelectFields(_, MonthColumns))))
in
    tbl1

Book2
ABCDEFGHIJKLMNOPQRS
1ProductMisc.JanFebMarAprMayJunProductMisc.JanFebMarAprMayJunTotal
2A100000465561198372983612341000A10000046556119837298361234100031216
3B1000001572652495801196723451000B100000157265249580119672345100032988
4C100000431892953566507834561000C10000043189295356650783456100026713
5
Sheet1
 
Upvote 0

Forum statistics

Threads
1,223,636
Messages
6,173,485
Members
452,516
Latest member
archcalx

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