Power Query: Apply a Transformation to a Dynamic Column Range

takoyaki

New Member
Joined
Oct 24, 2022
Messages
30
Office Version
  1. 365
Platform
  1. Windows
Hello all,

I've written a transformation to look up a column header in a table and return the number of hours corresponding to that value. The original value in the cell is then divided by this number. The amount of header columns and their names can vary depending on the workbook so it doesn't make practical sense to repeat this transformation for every single column in the table. I'm wondering if there is a way this transformation can be applied to all columns starting with the first column to the right of Column1 ((index 1) and to the last column at the far right (index 12). I believe it would be best to refer to them by their index number rather than their name, if possible.

The top table is how the data originally looks, followed by the lookup table, and the resulting table after the transformation is applied to column 1/1/2022. Any help is greatly appreciated!

Power Query Example.png


M-Code so far:

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"1/1/2022", Int64.Type}, {"2/1/2022", Int64.Type}, {"3/1/2022", Int64.Type}, {"4/1/2022", Int64.Type}, {"5/1/2022", Int64.Type}, {"6/1/2022", Int64.Type}, {"7/1/2022", Int64.Type}, {"8/1/2022", Int64.Type}, {"9/1/2022", Int64.Type}, {"10/1/2022", Int64.Type}, {"11/1/2022", Int64.Type}, {"12/1/2022", Int64.Type}}),
    #"Divided Column" = Table.TransformColumns(#"Changed Type", {{"1/1/2022", each _ / Hours[Hours]{List.PositionOf(Hours[Period],Date.FromText(Table.ColumnNames(#"Changed Type"){Table.Schema(#"Changed Type"){[Name="1/1/2022"]}[Position]}))}}})
in
    #"Divided Column"
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Power Query:
let
    tbl1 = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    tbl2 = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    tbl3 = Table.TransformColumnTypes(tbl2,{{"Period", type date}}),
    tbl4 = List.Accumulate(List.Skip(Table.ColumnNames(tbl1)), tbl1, (s,c)=> 
        Table.TransformColumns(s, {{c, each _/tbl3{[Period = Date.From(c)]}[Hours]}}))
in
    tbl4

Book1
ABCDEFGHIJKLMN
1Column11/1/20222/1/20223/1/20224/1/20225/1/20226/1/20227/1/20228/1/20229/1/202210/1/202211/1/202212/1/2022
2a4332341757601415537321
3b423818443950401549305222
4c5611515895925243627296
5d28234512471913541025335
6e42620481673381446531
7
8PeriodHours
91/1/2022205
102/1/2022218
113/1/2022156
124/1/2022135
135/1/2022162
146/1/2022184
157/1/2022101
168/1/2022160
179/1/2022156
1810/1/2022101
1911/1/2022127
2012/1/2022101
21
22Column11/1/20222/1/20223/1/20224/1/20225/1/20226/1/20227/1/20228/1/20229/1/202210/1/202211/1/202212/1/2022
23a0.2097560980.1467889910.2179487180.1259259260.3518518520.3260869570.009900990.256250.3525641030.3663366340.0236220470.207920792
24b0.2048780490.1743119270.1153846150.3259259260.2407407410.271739130.3960396040.093750.3141025640.2970297030.4094488190.217821782
25c0.2731707320.0504587160.3269230770.429629630.0555555560.3206521740.2475247520.150.2307692310.2673267330.2283464570.059405941
26d0.1365853660.1055045870.2884615380.0888888890.2901234570.103260870.1287128710.33750.0641025640.019801980.4173228350.346534653
27e0.0195121950.1192660550.1282051280.3555555560.0987654320.0380434780.3267326730.050.089743590.4554455450.0393700790.306930693
28
Sheet1
 
Upvote 0
Solution
Power Query:
let
    tbl1 = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    tbl2 = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    tbl3 = Table.TransformColumnTypes(tbl2,{{"Period", type date}}),
    tbl4 = List.Accumulate(List.Skip(Table.ColumnNames(tbl1)), tbl1, (s,c)=>
        Table.TransformColumns(s, {{c, each _/tbl3{[Period = Date.From(c)]}[Hours]}}))
in
    tbl4

Book1
ABCDEFGHIJKLMN
1Column11/1/20222/1/20223/1/20224/1/20225/1/20226/1/20227/1/20228/1/20229/1/202210/1/202211/1/202212/1/2022
2a4332341757601415537321
3b423818443950401549305222
4c5611515895925243627296
5d28234512471913541025335
6e42620481673381446531
7
8PeriodHours
91/1/2022205
102/1/2022218
113/1/2022156
124/1/2022135
135/1/2022162
146/1/2022184
157/1/2022101
168/1/2022160
179/1/2022156
1810/1/2022101
1911/1/2022127
2012/1/2022101
21
22Column11/1/20222/1/20223/1/20224/1/20225/1/20226/1/20227/1/20228/1/20229/1/202210/1/202211/1/202212/1/2022
23a0.2097560980.1467889910.2179487180.1259259260.3518518520.3260869570.009900990.256250.3525641030.3663366340.0236220470.207920792
24b0.2048780490.1743119270.1153846150.3259259260.2407407410.271739130.3960396040.093750.3141025640.2970297030.4094488190.217821782
25c0.2731707320.0504587160.3269230770.429629630.0555555560.3206521740.2475247520.150.2307692310.2673267330.2283464570.059405941
26d0.1365853660.1055045870.2884615380.0888888890.2901234570.103260870.1287128710.33750.0641025640.019801980.4173228350.346534653
27e0.0195121950.1192660550.1282051280.3555555560.0987654320.0380434780.3267326730.050.089743590.4554455450.0393700790.306930693
28
Sheet1
Thank you so much, it's almost perfect! In my real workbook I have about 50 columns of data before the date columns with numbers below them that I need to skip this transformation to, along with the very last column to the rightmost of the table. How would I adjust this code to reflect this?
 
Upvote 0
Thank you so much, it's almost perfect! In my real workbook I have about 50 columns of data before the date columns with numbers below them that I need to skip this transformation to, along with the very last column to the rightmost of the table. How would I adjust this code to reflect this?
I moved the column at the rightmost of the table to the leftmost and adjusted the code to account for this, now it's working perfectly! Cheers!

Power Query:
let
    tbl1 = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    tbl2 = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    tbl3 = Table.TransformColumnTypes(tbl2,{{"Period", type date}}),
    tbl4 = List.Accumulate(List.Skip(Table.ColumnNames(tbl1),51), tbl1, (s,c)=> 
        Table.TransformColumns(s, {{c, each _/tbl3{[Period = Date.From(c)]}[Hours]}}))
in
    tbl4
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,149
Members
453,021
Latest member
Justyna P

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