Smart Systems for Home

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

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

· 7 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 multiple columns in Excel Power Query, you add a series of sort steps in the order that matters: first sort by the primary column, then by the secondary, and so on. Power Query applies each sort step to the table as it exists at that point, so the last sort you add becomes the primary sort key. The practical way to get the order you want is to add the sorts in reverse priority (least important first, most important last), or use the built-in Sort dialog to define all levels at once. This guide covers both methods, plus how to fix the most common problems.

What You'll Need

  • Excel with Power Query (Excel 2016 or later, or Microsoft 365). The steps below use the Power Query Editor on Windows; Mac and web versions have the same core buttons but may label them slightly differently.
  • Your data loaded into Power Query, either via Data > Get Data or by selecting a table and using Data > From Table/Range.
  • At least two columns you want to sort by, and a clear idea of which one is the primary sort key.

Step-by-Step

Menu labels can differ slightly by Excel version, device and region, but the Power Query Editor layout is consistent enough that you can follow these steps on any recent desktop build.

  1. Open the Power Query Editor. If your data is already loaded, go to Data > Queries & Connections, right-click the query and choose Edit. If you are starting fresh, use Data > Get Data > From File or From Table/Range, then click Transform Data to open the editor.
  2. Decide your sort priority. For example, if you want rows sorted by Region first, then by Sales descending, Region is the primary key and Sales is the secondary key. Write this down before clicking anything, because the order of steps matters.
  3. Add the first sort step using the column header menu. Click the drop-down arrow on the column header of the least important sort column (in the example, Sales) and choose Sort Ascending or Sort Descending. Power Query inserts a "Sorted Rows" step in the Applied Steps pane on the right.
  4. Add the next sort step. Click the drop-down on the next column up in priority (Region) and choose its sort direction. A second "Sorted Rows" step appears. Because this step runs after the first one, it becomes the dominant sort, and the earlier sort acts as a tie-breaker within each Region.
  5. Repeat for any additional columns. Continue working from least important to most important. Each new sort step is applied on top of the previous ones, so the final step in the Applied Steps list should be your primary sort key.
  6. Alternatively, use the Sort dialog for all levels at once. With the table selected, open the Home tab in the Power Query Editor and click the Sort button's drop-down, then choose Sort. In the dialog that opens, add each column, choose Ascending or Descending, and use the up and down arrows to set priority. The top entry is the primary sort. Click OK and Power Query generates the equivalent sort steps for you.
  7. Check the Applied Steps order. Look at the Applied Steps pane. The sort steps should appear in the reverse of your intended priority: least important first, most important last. If they are the other way round, drag steps to reorder them or delete and re-add them.
  8. Verify the result. Scroll through the preview. Within each value of the primary column, the secondary column should be in the direction you chose. If not, the step order is wrong.
  9. Handle nulls and blanks. Power Query sorts null values together, usually at the top for ascending and at the bottom for descending. If you need blanks last regardless of direction, add a conditional column or use Table.Sort with a custom comparer in the formula bar, or filter out nulls before sorting if they are not needed.
  10. Close and load. Click Close & Load (or Close & Load To to choose a destination). The sorted order is now part of the query and will be reapplied every time the query refreshes.

Common Mistakes

  • Sorting in the wrong order. The most frequent error is sorting by the primary key first and the secondary key second. That makes the secondary sort dominant, so the primary column ends up only partly ordered. Always add sorts from least to most important.
  • Sorting in Excel instead of Power Query. Sorting the loaded table in the worksheet does not change the query. On refresh, the original order returns. Do the sorting inside the Power Query Editor.
  • Assuming the Sort dialog and manual steps are identical. They produce the same result, but the dialog writes a single combined sort step in some versions, while manual header sorts create separate steps. Both work; just be consistent when editing later.
  • Ignoring data types. A column stored as text sorts alphabetically (so "10" comes before "2"). Set the correct data type before sorting, especially for numbers and dates.
  • Forgetting case sensitivity. Text sorts are case-sensitive in some contexts and not others. If case matters, use a custom comparer or add a helper column with Text.Lower or Text.Upper and sort on that.

Tips

  • Name your sort steps clearly by double-clicking the step name in Applied Steps, so you can find them later in a long query.
  • If you need to sort by a custom list (for example, regions in a specific business order), add a conditional column that assigns a number to each region, then sort by that number.
  • Use the formula bar to inspect the generated Table.Sort step. It shows the exact column names and directions, which is useful when a column is renamed later and the step breaks.
  • If a column name changes upstream, the sort step will fail. Rename columns before adding sorts, or update the step manually.
  • For very large tables, sorting adds processing time on every refresh. Sort only when the order is actually needed for the output, not as a habit.

Troubleshooting

The sort does not stick after refresh. Check that the sort steps are inside the query, not applied to the worksheet. Open the editor and confirm the Applied Steps list contains the sort steps.

The order looks wrong. Inspect the Applied Steps sequence. The last sort step wins. Reorder the steps so the primary key is last, or use the Sort dialog and set priority explicitly.

Numbers sort as text. Change the column data type to Whole Number, Decimal Number or Date before sorting. Use Transform > Data Type and pick the right type.

A sort step shows an error after a column rename. Click the step and look at the formula bar. Replace the old column name with the new one, or delete the step and re-add the sort.

Blanks appear where you do not want them. Filter out null rows before sorting, or add a helper column that flags blanks and sort by that flag first.

The Sort dialog is greyed out. Make sure the table preview has focus and that no step is currently in an error state. Fix any error steps first, then try again.

Frequently Asked Questions

Can I sort by more than two columns in Power Query?

Yes. Add as many sort steps as you need, or use the Sort dialog to define multiple levels. Each additional level acts as a tie-breaker for the previous ones.

Does the order of sort steps matter?

Yes. Power Query applies steps sequentially, so the last sort step becomes the primary sort. Add sorts from least important to most important.

Why does my sort revert after refreshing?

The sort was probably applied in the worksheet rather than in the query. Add the sort inside the Power Query Editor so it is saved as part of the query.

How do I sort blanks to the bottom?

Filter out nulls before sorting, or add a helper column that marks blanks and sort by that column first. Power Query's default null placement depends on the sort direction.

Can I sort case-sensitively?

Not with the standard header sort. Add a helper column using Text.Lower or Text.Upper and sort on that, or use a custom comparer in the Table.Sort formula.

Does sorting in Power Query affect the original source data?

No. Sorting changes only the query output. The source file or database is untouched unless you write the sorted result back.