Add Multiple Columns in One Step - Power Query

Shreesurya

Board Regular
Joined
Jul 7, 2014
Messages
50
Hi,

How to add Three Custom Column in Powerquery with static Data in one step.

I don't want to create a new step every time I add the column.

Below is an example of a table I want to add. Please help me with this.

ABC
123
123
123
123
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
I am not trying to append two columns, I want to create 3 separate columns manually and have a static number in each row of the column.

I am adding 3 columns every time in the query, I am trying to reduce the step by adding all 3 columns at once in a step.
 
Upvote 0
Power Query:
let
   NumRows = 10,
   Source = Table.FromList(List.Generate(() => [x=1,A=1,B=2,C=3], each [x] <=NumRows,each [x=[x]+1,A=1,B=2,C=3],each [[A],[B],[C]]), Splitter.SplitByNothing(), null, null, ExtraValues.Error),
   ExpandRecords = Table.ExpandRecordColumn(Source,"Column1", {"A".."C"}, {"A".."C"})
in
   ExpandRecords
 
Upvote 0
If you know the names of the columns you want to add and hold them in a list, you can easily add any number of extra columns. The below steps assume that you hold the column names that you want to add in a list called "myListName". Try something like this

#"Added Custom items from LIST" = Table.AddColumn(#"myPreviousStep", "Custom.1", each if Table.HasColumns(#"myPreviousStep",myListName) then #"myPreviousStep" else Table.Combine({#"myPreviousStep",Table.FromRows({},myListName)})),
#"Expanded LIST called myListName" = Table.ExpandTableColumn(#"Added Custom items from LIST", "Custom.1", myListName),
#"Replaced values in items from myListName" = Table.ReplaceValue(#"Expanded LIST called myListName",null,1,Replacer.ReplaceValue,myListName),
#"Removed Duplicates rows" = Table.Distinct(#"Replaced values in items from myListName", {"Client Name"}),
 
Upvote 0
Or faster.....

If you know the names of the columns you want to add and hold them in a list, you can easily add any number of extra columns. The below steps assume that you hold the column names that you want to add in a list called "myListName". Try something like this:-

Power Query:
#"Added Custom items from LIST" = Table.AddColumn(#"myPreviousStep", "Custom", each if Table.HasColumns(#"myPreviousStep",myListName) then #"myPreviousStep" else Table.Combine({#"myPreviousStep",Table.FromRows({},myListName)})),
    #"Expand list items" = #"Added Custom items from LIST"{0}[Custom],
    #"Replaced values in items from myListName" = Table.ReplaceValue(#"Expand list items",null,1,Replacer.ReplaceValue,myListName),
 
Upvote 0
Power Query:
= let a={"A","B","C"} in List.Accumulate(a,Source,(x,y)=>Table.AddColumn(x,y,each List.PositionOf(a,y)+1))
1643382939379.png
 
Upvote 0
Power Query:
= let a={"A","B","C"} in #table(Table.ColumnNames(Source)&a,Table.ToList(Source,each _&List.Transform(a,(x)=>List.PositionOf(a,x)+1)))
1643383305705.png
 
Upvote 0
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="表1"]}[Content],
    a={"A","B","C"}
in
    Table.FromRecords(
                       Table.TransformRows(
                                           Source,
                                           each _&List.Accumulate(a,[],(x,y)=>Record.AddField(x,y,List.PositionOf(a,y)+1))
                                          )
                     )
1643383865640.png
 
Upvote 0
Many ways to add more than one columns in one step.
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="表1"]}[Content],
    a={"A","B","C"},
    res=Table.Group(
                    Source,
                    "Data",
                    List.Transform(
                                    a,
                                    each {_,(x)=>List.PositionOf(a,_)+1}
                                  )
                   )
in
    res
1643384295190.png
 
Upvote 0

Forum statistics

Threads
1,217,366
Messages
6,136,128
Members
449,993
Latest member
Sphere2215

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