Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Power Query Sort by Multiple Columns: Step-by-Step Guide

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To sort by multiple columns in Power Query, you add one Sort step per column in reverse priority order: sort the least important column first, then the next, and finish with the column that should be the primary sort. Power Query records each sort as a separate step and applies them in sequence, so the last sort you add becomes the primary order while the earlier sorts act as tie-breakers. This works the same in Excel (Data > Get Data / Power Query Editor) and in Power BI Desktop (Transform Data). You only need a table already loaded into the Power Query Editor and a clear idea of which column should sort first, second and so on.

What You'll Need

  • A table or range already loaded into the Power Query Editor in Excel or Power BI Desktop.
  • Column headers that are unique and meaningful (Power Query uses the header text in each step).
  • A decision on sort priority: which column is primary, which is secondary, and which direction (ascending or descending) each should use.
  • Optional: a backup copy of the workbook or report file, since sort steps change the query output.

Step-by-Step

  1. Open the Query Editor. In Excel, select any cell in your table and go to Data > From Table/Range (or open an existing query via Data > Queries & Connections and double-click it). In Power BI Desktop, select Home > Transform Data. Menu labels can differ slightly by version and region.
  2. Identify your sort priority. For example, if you want rows grouped by Region, then by Product, then by Date, your primary column is Region, secondary is Product, and tertiary is Date. Write this order down before clicking anything.
  3. Sort the least important column first. Click the filter/sort arrow in the header of the tertiary column (Date in the example) and choose Sort Ascending or Sort Descending. A new step named "Sorted Rows" appears in the Applied Steps pane on the right.
  4. Sort the next column up. Click the arrow on the secondary column (Product) and choose its sort direction. Another "Sorted Rows" step is added.
  5. Sort the primary column last. Click the arrow on the primary column (Region) and choose ascending or descending. This final sort overrides the row order for Region while preserving the previous sorts inside each Region group.
  6. Verify the result. Scroll through the preview. Rows should be grouped by Region, then by Product within each region, then by Date within each product. If not, check the order of steps in Applied Steps.
  7. Rename the steps (optional but recommended). Right-click each "Sorted Rows" step in Applied Steps and choose Rename so you can tell which column it sorts. This makes later edits much easier.
  8. Apply and close. In Excel, select Home > Close & Load > Close & Load. In Power BI Desktop, select Home > Close & Apply. The sorted order is now part of the query and refreshes with the data.

Common Mistakes

  • Sorting in the wrong order. The most frequent error is sorting the primary column first. Each new sort replaces the row order established by the previous step for the rows it touches, so the primary column must be sorted last.
  • Using the worksheet sort instead of Power Query. Sorting the loaded table in Excel or the visual in Power BI does not change the query; the next refresh restores the query's order. Always sort inside the Query Editor if you need the order to persist.
  • Sorting a column that is later removed. If you delete a column after sorting by it, the step may error or the sort may be dropped. Sort after you have finished removing and renaming columns where possible.
  • Expecting text sorts to behave like numbers. Text columns sort alphabetically, so "10" can appear before "2". Change the column type to Whole Number or Decimal Number before sorting if the values are numeric.
  • Forgetting that nulls have a position. Blank or null values sort to the top in ascending order and to the bottom in descending order. If that is not what you want, filter or replace nulls first.
  • Reordering steps by dragging. Moving a sort step above a column-removal or type-change step can break the query. Keep sort steps near the end of the Applied Steps list.

Tips

  • Name your sort steps clearly (for example "Sort by Date", "Sort by Product", "Sort by Region") so the priority is obvious at a glance.
  • If you need to change the sort direction later, click the step in Applied Steps and use the arrow in the column header; Power Query updates that step rather than adding a new one.
  • For a clean, repeatable pattern, add all sort steps at the very end of the query, after filters, type changes and column removals.
  • Use the Advanced Editor if you prefer to see the M code. Each sort appears as a Table.Sort line, and you can confirm the order of operations there.
  • When sorting by date or time, make sure the column type is Date, Date/Time or Time, not Text; otherwise the sort is alphabetical and will look wrong.
  • If the query is used by a PivotTable or a Power BI visual, the sort order in the query does not automatically set the visual's sort. Set the visual sort separately in the report.

Troubleshooting

If the preview does not show the order you expect, work through these checks in order:

  1. Check Applied Steps order. Open the Applied Steps pane and confirm the primary-column sort is the last sort step. If it is not, drag it down or delete and re-add the sorts in the correct sequence.
  2. Check column data types. Select the column, look at the icon in its header, and use Transform > Data Type to set it correctly. Text-typed numbers and dates are the usual cause of odd ordering.
  3. Look for a later step that re-sorts or reorders. A step such as "Removed Duplicates" or a merge can change row order. Move the sort steps after those steps.
  4. Refresh the query. In Excel use Data > Refresh All; in Power BI use Home > Refresh. If the order reverts, the sort step is missing or was overwritten.
  5. Check for errors in the step. A red error indicator next to a step means the column it references no longer exists or was renamed. Delete the step and re-add the sort on the current column name.
  6. If the query still will not hold the order, add an explicit index column (Add Column > Index Column) before sorting, then sort by your columns and finally by the index if you need to restore the original order. This is a safe way to test what each sort is doing.

If none of that works, close without saving, reopen the original file, and rebuild the query with the sort steps added last. Avoid deleting the query or clearing the workbook until you have a backup, since that removes the steps you have built.

Frequently Asked Questions

Can I sort by more than two columns?

Yes. Add one sort step per column, in reverse priority order. There is no fixed limit, though very long chains of sort steps become harder to maintain, so rename each step.

Does sorting in Power Query change the order in my worksheet or report?

It changes the order of rows loaded by the query. A PivotTable or a Power BI visual may apply its own sort, so set the visual sort separately if needed.

Why does my date column sort as text?

The column type is probably Text. Change it to Date or Date/Time with Transform > Data Type, then re-apply the sort.

Can I sort by a column and then remove that column?

You can, but the sort step may error or be dropped. Sort after removing columns, or keep the column until the sort is complete.

Where do blank values go when sorting?

Nulls sort to the top in ascending order and to the bottom in descending order. Filter or replace nulls before sorting if you need a different placement.

Is the sort order saved when I refresh the data?

Yes, because the sort is stored as a step in the query. Each refresh re-applies the same sequence of sorts to the new data.