cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
GulianiG
Helper I
Helper I

Can a custom column be added at a given location in table?

Hi All,

When I add a custom table, it gets added at the end of the table.

Is there a way for us to define in M-Code, where we want the column to be inserted instead of end?

1 ACCEPTED SOLUTION
Vijay_A_Verma
Super User
Super User

Yes, custom column is always added at the end and then you will have to reorder it. Below is a formula which combines these. Here I am inserting custom column at index position of 2.

= Table.ReorderColumns(Table.AddColumn(#"Changed Type", "Custom", each List.Sum(List.LastN(Record.ToList(_),3))),List.InsertRange(Table.ColumnNames(#"Changed Type"),2,{"Custom"}))

 See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test 

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Q", type text}, {"A", Int64.Type}, {"B", Int64.Type}, {"C", Int64.Type}, {"D", Int64.Type}, {"E", Int64.Type}, {"F", Int64.Type}, {"G", Int64.Type}}),
    Result = Table.ReorderColumns(Table.AddColumn(#"Changed Type", "Custom", each List.Sum(List.LastN(Record.ToList(_),3))),List.InsertRange(Table.ColumnNames(#"Changed Type"),2,{"Custom"}))
in
    Result

View solution in original post

2 REPLIES 2
v-jingzhang
Community Support
Community Support

Hi @GulianiG 

 

You can use Transform > Move to move the custom column to another position after creating it. This would be easier. https://docs.microsoft.com/power-query/move-columns 

 

After you move a column, you will see the code of this step. The columns display in the order of the column name list. You can also change the item order in the list to change their display order.

vjingzhang_0-1654755783185.png

 

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.

Vijay_A_Verma
Super User
Super User

Yes, custom column is always added at the end and then you will have to reorder it. Below is a formula which combines these. Here I am inserting custom column at index position of 2.

= Table.ReorderColumns(Table.AddColumn(#"Changed Type", "Custom", each List.Sum(List.LastN(Record.ToList(_),3))),List.InsertRange(Table.ColumnNames(#"Changed Type"),2,{"Custom"}))

 See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test 

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Q", type text}, {"A", Int64.Type}, {"B", Int64.Type}, {"C", Int64.Type}, {"D", Int64.Type}, {"E", Int64.Type}, {"F", Int64.Type}, {"G", Int64.Type}}),
    Result = Table.ReorderColumns(Table.AddColumn(#"Changed Type", "Custom", each List.Sum(List.LastN(Record.ToList(_),3))),List.InsertRange(Table.ColumnNames(#"Changed Type"),2,{"Custom"}))
in
    Result

Helpful resources

Announcements
PBI Sept Update Carousel

Power BI September 2023 Update

Take a look at the September 2023 Power BI update to learn more.

Learn Live

Learn Live: Event Series

Join Microsoft Reactor and learn from developers.

Dashboard in a day with date

Exclusive opportunity for Women!

Join us for a free, hands-on Microsoft workshop led by women trainers for women where you will learn how to build a Dashboard in a Day!

MPPC 2023 PBI Carousel

Power Platform Conference-Power BI and Fabric Sessions

Join us Oct 1 - 6 in Las Vegas for the Microsoft Power Platform Conference.

Top Solution Authors
Top Kudoed Authors