Placing a new custom column

DrDebit

Board Regular
Joined
May 20, 2013
Messages
123
Office Version
  1. 365
Platform
  1. Windows
In Power Query, is there an easy way to add a custom column so that the placement is not always at the very end? for example, if I have 20 columns and I want to place the new column after column 5... Thankyou in advance!
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Not currently, as far as I know. You have to add it, then move it.
 
Upvote 0
Solution
If you have the need to do that a lot then you can write a custom function. Say you had a table like this:

Book1
ABCDEF
1Col1Col2Col4Col5Col6
212456
312456
412456
512456
612456
7
Sheet2


and you wanted to add Col3 at column position 2 (since PQ is zero based). With a custom function query like the one below (name it Table_AddColumnAtPosition):

Power Query:
(tbl as table, ColName as text, fnAdd as function, pos as number) =>
        let
            tbl1 = Table.AddColumn(tbl, ColName, fnAdd),
            reorder = let tcn = Table.ColumnNames(tbl1) in List.FirstN(tcn,pos) & {List.Last(tcn)} & List.Range(tcn, pos, List.Count(tcn) - pos -1),
            Result = Table.ReorderColumns(tbl1, reorder)
        in  
            Result

then write your query concisely using the function

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
    Result = Table_AddColumnAtPosition(Source,"Col3", each 3, 2)
in
    Result

Book1
ABCDEFGHIJKLM
1Col1Col2Col4Col5Col6Col1Col2Col3Col4Col5Col6
212456123456
312456123456
412456123456
512456123456
612456123456
7
Sheet2
 
Upvote 0

Forum statistics

Threads
1,223,677
Messages
6,173,794
Members
452,534
Latest member
autodiscreet

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