Power Query - Add multiple blank columns

borkybork

New Member
Joined
Aug 5, 2022
Messages
18
Office Version
  1. 365
Platform
  1. Windows
Hi! I’m trying to add blank columns to my table, but when I do it creates a query with hundreds of repeated rows of the same data. So for example if I have 5 rows of data, it then creates the query and repeats that same data set a hundred times. Is there a better way of adding blank columns without this happening? I’m still learning power query so I’m not sure what I’m doing wrong. Below is the code I have. Thank you in advance!

=Table.AddColumn(# “Changed Type”, “ColumnName1”, each null)
& Table.AddColumn(# “Changed Type”, “ColumnName2”, each null)
& Table.AddColumn(# “Changed Type”, “ColumnName3”, each null)

…etc.
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    Cols = {"DOB", "Address", "City", "State", "Zip"},
    AddEmptyColumns = List.Accumulate(Cols, Source, (s,c)=> Table.AddColumn(s, c, each null))
in
    AddEmptyColumns

Book1
ABCDEFGHIJK
1Table2Query Output
2NameValueNameValueDOBAddressCityStateZip
3A8A8
4B5B5
5C9C9
6D3D3
7E2E2
8F4F4
9G1G1
10H7H7
11I6I6
12J10J10
13
Sheet2
 
Upvote 1
Solution
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    Cols = {"DOB", "Address", "City", "State", "Zip"},
    AddEmptyColumns = List.Accumulate(Cols, Source, (s,c)=> Table.AddColumn(s, c, each null))
in
    AddEmptyColumns

Book1
ABCDEFGHIJK
1Table2Query Output
2NameValueNameValueDOBAddressCityStateZip
3A8A8
4B5B5
5C9C9
6D3D3
7E2E2
8F4F4
9G1G1
10H7H7
11I6I6
12J10J10
13
Sheet2
Thank you!! Would you mind explaining what the 2nd part of the formula is doing?
AddEmptyColumns = List.Accumulate(Cols, Source, (s,c)=> Table.AddColumn(s, c, each null))
 
Upvote 0
List.Accumulate goes through each item in a list, in this case the list named Cols.

It takes an initial value in this case the table named Source.

Then it applies a function to the current state represented by s in the function (s,c)=>. The current state is initially the initial value and can change with each iteration as it steps through the list.

So first step it takes the table Source and adds a column named c="DOB" filling each row with null. Then it takes that table and adds a column "Address", etc ... until the last item in the list.

It is a very useful function to allow looping in M code.
 
Upvote 0
List.Accumulate goes through each item in a list, in this case the list named Cols.

It takes an initial value in this case the table named Source.

Then it applies a function to the current state represented by s in the function (s,c)=>. The current state is initially the initial value and can change with each iteration as it steps through the list.

So first step it takes the table Source and adds a column named c="DOB" filling each row with null. Then it takes that table and adds a column "Address", etc ... until the last item in the list.

It is a very useful function to allow looping in M code.
Great, thanks so much! This is very helpful.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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