Power Query Record.ReorderFields Function Guide
In this article
The Record.ReorderFields function in Power Query lets you rearrange the order of fields (columns) in a record—typically used when transforming tables to ensure fields appear in your desired sequence. It requires a record or table (which is internally a list of records) and a list of field names specifying the new order. No special setup is needed beyond having Power Query available in Excel or Power BI.
What You'll Need
- Excel (2016 or later) or Power BI Desktop
- A table or record already loaded or created in Power Query Editor
- A list of field names in the order you want them to appear
Step-by-Step
- Open Power Query Editor: In Excel, go to the Data tab and click Get Data Launch Power Query Editor. In Power BI, import your data and click Transform data in the Home tab to open the editor.
- Locate or create your table: Ensure your query returns a table (not just a record or scalar value). If you’re working with a single record (e.g., from an API response), convert it to a table first: right-click the record in the preview, choose Convert to Table, then expand it if needed.
- Open the Advanced Editor: In the Power Query Editor, select your query from the left pane, then click Home > Advanced Editor (or View > Advanced Editor in some versions).
- Identify the source step: Find the step that produces the table you want to reorder—usually named something like
Source,Changed Type, or your custom step. You’ll applyRecord.ReorderFieldsto this step’s output. - Add a new custom step: After the source step, insert a new step. You can do this by clicking the fx icon next to the formula bar above the step list, or by typing directly in the Advanced Editor after the last step. For example, if your last step is named
PreviousStep, create a new step like:Reordered = Record.ReorderFields(PreviousStep, {"Field3", "Field1", "Field2"}). - Specify field names correctly: Replace
"Field3", "Field1", "Field2"with the *exact* column names from your table (case-sensitive in some environments, but generally not in Power Query). Use double quotes and separate names with commas inside curly braces. Omit fields not in the table, or the function will throw an error. - Handle missing or extra fields: If you include a field name in the list that doesn’t exist in the record, Power Query raises an error. To avoid this, verify the field names match by checking the column headers in the preview or using
Table.ColumnNames(PreviousStep)in a temporary step to list current column names. - Apply and close: Once satisfied, click Close & Load (or Close & Load To) to load the reordered table back into Excel or Power BI. Confirm the column order has changed in the output.
Common Mistakes
- Using field indices instead of names:
Record.ReorderFieldsexpects a list of *names*, not numbers. Passing{2, 1, 3}will fail. - Mismatched or misspelled field names: Even a trailing space (e.g.,
"CustomerID ") will cause an error. Always copy-paste names directly from the column header or fromTable.ColumnNames(). - Applying to non-record types: If the input is a list, number, or null, the function throws
Expression.Error. Ensure the previous step returns a record or table (tables work because they’re lists of records, andRecord.ReorderFieldsoperates on each record). - Assuming it reorders the entire table globally: This function reorders fields *within each record*, but if the table has duplicate column names (rare, but possible in some transformations), behavior may be unpredictable. Avoid duplicate column names before reordering.
Tips
- Combine with
Table.SelectColumns: UseTable.SelectColumnsfirst if you only want to keep specific columns *and* reorder them—e.g.,Table.SelectColumns(Source, {"Name", "Date", "Amount"})—and then reorder only if needed. - Use for API or JSON data: When consuming flat JSON objects, fields often arrive in arbitrary order.
Record.ReorderFieldshelps normalize the output for reports. - Test with a small dataset: Before applying to a large query, test the step on a filtered subset (e.g.,
Table.FirstN(Source, 5)) to catch errors quickly. - Preserve type information: Reordering doesn’t change data types. Keep type conversions (e.g.,
Table.TransformColumnTypes) in separate steps after reordering to avoid type errors. - Dynamic field lists with
List.Transform: If field names are stored in a parameter or list (e.g., from Excel cells), useRecord.ReorderFields(#"PreviousStep", ParameterName)whereParameterNameis a list like{"A", "B", "C"}.
Troubleshooting
If Record.ReorderFields doesn’t work as expected, follow these steps:
- Check the error message: Hover over the red exclamation mark in the step list to see the full error. Common errors include
Expression.Error: The field 'XYZ' does not exist in the record—confirm the field name exists in your table. - Verify the input type: Right-click the previous step’s output in the preview pane and choose View Native Query (if available) or Diagnose to inspect the data structure. If the output is a list of records, ensure each record has the fields you’re referencing.
- Isolate the step: Temporarily delete all steps after the source and re-add them one by one. If reordering fails only after adding a specific step, the issue likely stems from that prior transformation (e.g., a column was renamed or removed).
- Check for hidden characters: Copy a column name from the header, paste it into Notepad, then copy from Notepad into your
Record.ReorderFieldslist to eliminate invisible spaces or encoding issues. - Reset to a working state: If stuck, click the Undo button (or press Ctrl+Z) repeatedly to revert to a working step, then reapply your reorder logic carefully.
Frequently Asked Questions
Does Record.ReorderFields change the underlying data?
No—it only changes the display order of fields. All values, data types, and row content remain identical.
Can I reorder fields without using the Advanced Editor?
Yes. In Power Query Editor, drag and drop column headers to reorder them visually. This updates the query behind the scenes, but using Record.ReorderFields explicitly gives you precise control and makes the logic visible in code.
What happens if I include a field name twice in the list?
Power Query will include the field twice in the output, potentially creating duplicate columns. Avoid this unless intentional.
Does this work with nested records?
No—Record.ReorderFields only affects the top-level fields of each record. To reorder nested fields, you must first expand the nested record into separate columns, reorder those, and optionally re-nest if needed.
Can I use wildcards or patterns in the field list?
No—field names must be exact strings. Use List.Select or List.Transform with Table.ColumnNames to programmatically build the list if needed (e.g., List.Select(Table.ColumnNames(Source), each Text.StartsWith(_, "Date"))).
Why does my reordered table still show columns in the original order in the output?
Ensure you’ve applied the step and reloaded the data. Sometimes Excel or Power BI caches the previous view—try refreshing the query (Data > Refresh All) or closing and reopening the file.