Skip to content

Re-ordering columns

Moving a column by dragging it in the Power Query Editor writes a Table.ReorderColumns step that names every column in the table. That is convenient, but it makes the query fragile, because the step fails on refresh as soon as any of those columns is renamed or removed in the source.

Two habits reduce the risk:

  • Reorder only after removing the columns the report does not need, so that fewer names are involved.
  • Name only the columns whose position matters, and let the others keep their order.

This page shows how to do the second.

The data has six columns:

An Excel table with columns Date, Color, Supplier, City, Category and Revenue, holding five rows of car sales

The aim is to make City and Revenue the second and third columns, while leaving the order of the rest alone.

The table in the Power Query Editor with the columns in their original order, from Date to Revenue

Click the fx button beside the formula bar to add a step, and enter this formula:

= Table.ReorderColumns(
Source, List.InsertRange(
List.Difference(
Table.ColumnNames(Source),
{"City", "Revenue"} ),
1,
{"City", "Revenue"}
)
)

The formula builds the new column order as a list, in two stages:

  1. List.Difference takes the table’s column names and removes City and Revenue, which leaves {Date, Color, Supplier, Category}.
  2. List.InsertRange puts City and Revenue back into that list at position 1, which is the second place, because positions count from 0.

The result is {Date, City, Revenue, Color, Supplier, Category}, and Table.ReorderColumns arranges the table in that order. Only City and Revenue are named, so the step keeps working if the other columns change.

The technique on this page follows one in Gil Raviv’s Collect, Combine, and Transform Data Using Power Query in Excel and Power BI (Microsoft Press).