Receiving All Columns in One Step

kellyjkon

New Member
Joined
Nov 30, 2022
Messages
11
Office Version
  1. 2019
Platform
  1. Windows
Hi everyone,

I want to group my vehicle list by year and model in power query. Then I want to insert the data in one step from DesiredOutput as a column List.
I know how to add one by one by with Add.Column as below, however, I don't want it to be a lot of manual steps as I will be add 15 columns.
Is there an M code where we can do these in one step?

Present:
Table.AddColumn(#"Previous Step", "New Column", each Table.Column([DesiredOutput],"Column Name"))
Table.AddColumn(#"Previous Step", "New Column", each Table.Column([DesiredOutput],"Column Name"))

Desired(maybe smt like that?)
Table.AddColumnS(#"Previous Step", "New Column","Second Column","Third Column", each Table.Column([DesiredOutput],"Column Name","Column Name 2","Column Name 3"))


1670249012835.png


Thanks!!
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Why don't you just expand the table by clicking the double arrow and choose your columns there?
 
Upvote 0
Because I will separate all the data - which I have grouped in Table - with commas and turn them into a single line per year and model
 
Upvote 0
As you can see below, i have to add columns from "Group By Data" table, after that I will concatenate all list values with comma in distinct rows per year and model.
But in this way let's say i need to 10 column from "Group By Data", I will have to create 10 "added columns" plus 10 "extracted value" steps and it is not effective at all.


1670323449960.png
 
Upvote 0
This example may help

Power Query:
let
    tbl1 = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    tbl2 = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    tbl3 = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
    tbl = Table.FromColumns({{tbl1,tbl2,tbl3}}, {"OriginalColumn1"}),
    columns = {"Column1","Column2","Column3","Column4"},
    AddedColumns = List.Accumulate(columns, tbl, (s,c)=> Table.AddColumn(s,c, each 
                    Text.Combine( List.Transform(Table.Column([OriginalColumn1], c), (x)=> Text.From(x)), ", " ))),
    Result = Table.FromColumns({{"Table1", "Table2", "Table3"}} & List.Skip(Table.ToColumns(AddedColumns)), {"Table Name"} & List.Skip(Table.ColumnNames(AddedColumns)))
in
    Result

Book2
ABCDEF
1Column1Column2Column3Column4
21713237
32115122
41411186
5199412
62016242
731085
8
9Column1Column2Column3Column4
10269822
113173139
1240123327
1318353436
1414162128
151112938
161937157
175322520
182133024
19234610
20
21Column1Column2Column3Column4
22612103
231618820
242957
251513119
261141714
27
28Table NameColumn1Column2Column3Column4
29Table117, 21, 14, 19, 20, 313, 15, 11, 9, 16, 1023, 1, 18, 4, 24, 87, 22, 6, 12, 2, 5
30Table226, 3, 40, 18, 14, 1, 19, 5, 2, 239, 17, 12, 35, 16, 11, 37, 32, 13, 48, 31, 33, 34, 21, 29, 15, 25, 30, 622, 39, 27, 36, 28, 38, 7, 20, 24, 10
31Table36, 16, 2, 15, 1112, 18, 9, 13, 410, 8, 5, 1, 173, 20, 7, 19, 14
32
Sheet1
 
Upvote 0
This example may help

Power Query:
let
    tbl1 = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    tbl2 = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    tbl3 = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
    tbl = Table.FromColumns({{tbl1,tbl2,tbl3}}, {"OriginalColumn1"}),
    columns = {"Column1","Column2","Column3","Column4"},
    AddedColumns = List.Accumulate(columns, tbl, (s,c)=> Table.AddColumn(s,c, each
                    Text.Combine( List.Transform(Table.Column([OriginalColumn1], c), (x)=> Text.From(x)), ", " ))),
    Result = Table.FromColumns({{"Table1", "Table2", "Table3"}} & List.Skip(Table.ToColumns(AddedColumns)), {"Table Name"} & List.Skip(Table.ColumnNames(AddedColumns)))
in
    Result

Book2
ABCDEF
1Column1Column2Column3Column4
21713237
32115122
41411186
5199412
62016242
731085
8
9Column1Column2Column3Column4
10269822
113173139
1240123327
1318353436
1414162128
151112938
161937157
175322520
182133024
19234610
20
21Column1Column2Column3Column4
22612103
231618820
242957
251513119
261141714
27
28Table NameColumn1Column2Column3Column4
29Table117, 21, 14, 19, 20, 313, 15, 11, 9, 16, 1023, 1, 18, 4, 24, 87, 22, 6, 12, 2, 5
30Table226, 3, 40, 18, 14, 1, 19, 5, 2, 239, 17, 12, 35, 16, 11, 37, 32, 13, 48, 31, 33, 34, 21, 29, 15, 25, 30, 622, 39, 27, 36, 28, 38, 7, 20, 24, 10
31Table36, 16, 2, 15, 1112, 18, 9, 13, 410, 8, 5, 1, 173, 20, 7, 19, 14
32
Sheet1
Thanks but i have only one table to get data which i pointed as "Group By Data" . With that solution i guess i need to create duplicate "Group By Data" tables 3 times?
I want to create 5 new column from my grouping data table in one step with M query.
 
Upvote 0
Thanks but i have only one table to get data which i pointed as "Group By Data" . With that solution i guess i need to create duplicate "Group By Data" tables 3 times?
I want to create 5 new column from my grouping data table in one step with M query.
Looks like @JGordon11's solution is what you need.

Replace "Source" by what last step name you have in your original query.

No need to replicate the query 3 times.

Power Query:
let
    tbl = Table.FromColumns({Source[DesiredOutput]}, {"OriginalColumn1"}),
    columns = {"Column1","Column2","Column3","Column4"},
    AddedColumns = List.Accumulate(columns, tbl, (s,c)=> Table.AddColumn(s,c, each 
                    Text.Combine( List.Transform(Table.Column([OriginalColumn1], c), (x)=> Text.From(x)), ", " ))),
    Result = Table.FromColumns({{"Table1", "Table2", "Table3"}} & List.Skip(Table.ToColumns(AddedColumns)), {"Table Name"} & List.Skip(Table.ColumnNames(AddedColumns)))
in
    Result
 
Upvote 0

Forum statistics

Threads
1,223,395
Messages
6,171,857
Members
452,427
Latest member
samk379

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