Asked by:
Reorder Columns selectively

General discussion
-
Hello,
I'm looking to reorder columns in Power Query but I want the column order to be ascending and other select columns to be at the left most side of the table.
The table will have the Columns of : "Item Name", "Units", "Cost", "ratio", "source", (the rest of the column order will be ascending).
In terms of M-Functions advanced editor it might look like this.
"Reordered Columns" = Table.ReorderColumns("Prior Step",{"Item Name", "Units", "Cost", "ratio", "source"} & List.Sort(Table.ColumnNames("Prior Step"),Order.Ascending))
but the above does not work.
- Edited by SSibbett Thursday, October 24, 2019 10:52 PM 4th time is a charm
All replies
-
Hi
With the bolded part in List.Sort(Table.ColumnNames("Prior Step"),Order.Ascending) your sort ALL your column names, inc. those you want to keep on the left (Item name, Units...source)
So you should exclude them from the List.Sort. One way:
StepBefore_LeftColumns = ... LeftColumns = {"item Name", "units", "cost", "ratio", "source"}, ReorderList = LeftColumns & List.Sort(List.Difference(Table.ColumnNames(<StepBefore_LeftColumns>),LeftColumns),Order.Ascending), Reorder = Table.ReorderColumns(<StepBefore_LeftColumns>, ReorderList)
- Edited by Lz._ Friday, October 25, 2019 10:46 AM
-
-
-