Power Query Sort: How to Sort and Keep Rows in Order

Formulas & functions · Updated November 11, 2026

To sort data in Power Query in Excel for Microsoft 365 on Windows, open the query, select the dropdown on a column header, and choose Sort Ascending or Sort Descending. Power Query records the sort as a query step, so it is reapplied when the query refreshes; to sort by more than one field, apply the sorts in priority order.

How sorting works in Power Query

Power Query is Excel’s data-import and transformation tool. A query contains a sequence of steps, such as changing a column’s data type, filtering rows, and sorting records. Sorting is therefore not the same as rearranging cells in the original worksheet: it defines an operation in the query that produces an ordered result when the query runs.

In the Power Query Editor, a sort usually appears as a step named Sorted Rows in the Query Settings pane under Applied Steps. The step can be selected, edited, or removed. If the Applied Steps pane is hidden, use View > Query Settings to show it. The formula bar may also show the operation in Power Query M, but you do not need to write code to sort through the interface.

Sorting is useful when you want records in a predictable order every time a report updates: for example, sales from largest to smallest, dates from oldest to newest, or customer names alphabetically. The source data itself is not necessarily changed; the query output is what gets ordered.

Sort a single column

To sort a query by one field in Excel for Microsoft 365 on Windows:

  1. Select a cell in the query output and choose Data > From Table/Range to open the Power Query Editor. If the data is already a query, select a cell in its loaded table and choose Data > Get Data > Launch Power Query Editor, or open the query through Data > Queries & Connections.
  2. Find the column you want to use and select its dropdown arrow in the header.
  3. Choose Sort Ascending or Sort Descending. For text, ascending is generally A to Z; for numbers, it is smallest to largest; and for dates, it is earliest to latest.
  4. Check the preview and the Applied Steps list. When the result is ready, choose Home > Close & Load, or use Home > Close & Load To to choose where to place it.

You can also select a column and use the sort controls on the Home or Transform tab, depending on the Editor layout. The header dropdown is often the quickest way to see the available direction. Sorting a column does not remove records; it changes their order in the query result.

Sort by multiple columns

Multi-column sorting creates a hierarchy. The first field is the primary sort; the next field orders records that tie on the first; a third field breaks ties left by the first two. For instance, a sales table can be ordered first by Region from A to Z, then by Order Date from oldest to newest, and then by Sales descending. Within each region, dates are ordered; for orders on the same date, the largest sale appears first.

In the Editor, apply a sort to the primary column first. Then use the header dropdown on the secondary column to set its direction, and repeat for any further tie-breakers. Inspect the preview after each change. The Applied Steps list may show multiple sorted-row steps, while the resulting sort can be represented as a list of sort keys. If the order is not what you intended, remove or edit the relevant step and apply the keys again in the desired priority order.

Choose keys that make the desired order explicit. If two rows have the same Region, Date, and Sales value, their relative order may not be meaningful or guaranteed. Add a unique identifier, such as an Order ID, as a final key when you need a reproducible order for every row. That is especially helpful before adding an index column or comparing results between refreshes.

Check data types before sorting

A sort can look wrong when a column’s type does not match the values it represents. A column containing numbers stored as text may sort alphabetically rather than numerically. For example, the text values 1, 10, and 2 can appear in that sequence because text is compared character by character; numeric values sort as 1, 2, and 10. Dates stored as text can likewise sort according to their text representation rather than chronological date value.

Look at the small type icon at the left of each column header in the Power Query Editor. It indicates whether the column is typed as text, a whole number, a decimal number, a date, or another data type. If the type is wrong, select the column and choose the appropriate type from Transform > Data Type or the type icon. Confirm that the conversion is valid before sorting, since invalid values may produce errors or require cleanup.

For dates or numbers written in a regional format, a direct type change may interpret the text incorrectly. Use Change Type > Using Locale when available, then select the intended data type and locale. For example, a day/month/year string can be ambiguous if interpreted using a month/day/year locale. Check several converted values against the source before relying on the resulting order.

Where to place the sort step

The position of a sort in Applied Steps can affect what the final query returns. A filter after a sort removes rows but usually leaves the remaining rows in their prior relative order. Other operations, however, can change the sequence or make ordering irrelevant. Grouping, merging, expanding, appending, and later sorting steps may all affect the order visible in the final preview. If the output must have a specific final order, add or verify a sort step after the transformations that can change it.

Suppose a query combines monthly files, removes unwanted rows, groups sales by representative, and then produces a summary. Sorting the source rows before grouping does not necessarily sort the grouped summary. Apply the sort to the summary’s relevant output columns after the grouping step. Similarly, if a merge introduces a new date field that should control the final order, sort after the merge and expansion.

When you add an Index Column, place it after the sort if the index is meant to represent the ordered position. In the Editor, use Add Column > Index Column after establishing the intended sort. If you add the index first, it records the earlier row sequence; sorting later moves those existing index values along with their records instead of numbering the new order from top to bottom.

Power Query sort versus sorting an Excel worksheet

Excel’s worksheet sort and Power Query’s sort solve related but different problems. A worksheet sort, accessed from Data > Sort or the sort controls on the Data tab, rearranges the rows in the selected range or table. It is convenient for a one-time analysis or for sorting data directly in a workbook.

A Power Query sort is part of a repeatable transformation. When the query refreshes, Power Query reruns its steps against the current source data. This makes it useful for recurring reports and imported data that should be cleaned and ordered consistently. But sorting the loaded table manually in Excel does not add a sort step to the query. A later refresh can replace the table with query results in the order specified by the query instead.

Choose the worksheet sort when you want to rearrange a range for immediate work and do not need the action built into an import process. Choose Power Query when the ordering should be repeated as part of data preparation. You can still apply a worksheet sort to a loaded query table for temporary viewing, but treat the query steps as the durable instructions for the next refresh.

Refresh behavior and source ordering

After a sort has been added in the Power Query Editor, it normally remains in the query and is applied during refresh. Use Data > Refresh All to refresh workbook queries and connections, or right-click a query table and choose Refresh. New source records are included according to the query’s other steps, and the sort is recalculated for the rows that remain.

Do not assume a source is already in a permanent order just because it appears ordered in its original file or database. Row order can depend on the source and how data is retrieved. If order matters to the result, include a sort in the query rather than relying on the source’s current presentation. For queries connected to databases or other systems, Power Query may pass some operations back to the source through query folding. The visible result should still be checked, especially after adding later steps or changing the source.

Sorting does not protect a query from changes in its inputs. If refreshed data contains new values, those values will take their appropriate place under the selected sort, but errors, missing values, or changed data types may affect the outcome. Check the refreshed result when the source structure or format changes.

Common sorting problems

  • Numbers appear out of order: Check whether the column is typed as text. Convert it to a numeric type, resolve conversion errors, and sort again.
  • Dates are not chronological: Confirm that the column is a date or date/time value rather than text, and use the correct locale when converting regional date strings.
  • The wrong field seems to control the order: Review the order of sort keys and the sequence of Applied Steps. A later sort can establish a new primary order.
  • The order changes after grouping or combining data: Add a final sort after the operation that changes the rows or output columns.
  • Rows with equal values change relative position: Add a further tie-breaker such as a unique ID if each row needs a defined position.
  • Manual Excel sorting disappears after refresh: Add the durable sort in the Power Query Editor, rather than relying only on a worksheet sort of the loaded results.

One related misconception is that sorting before removing duplicates always determines which duplicate is kept. Do not rely on that assumption as a general rule: transformations, source behavior, and query folding can affect how operations are executed. If a particular record must be retained, define a clear rule for selecting it and verify the query result rather than treating a preceding sort as a guarantee.

Frequently Asked Questions

Does Power Query keep the sort after refresh?
Yes, if the sort is saved as a step in the query. Refresh reruns that step against the current data. A manual sort applied only to the worksheet output is not the same as a saved query sort.

Can I sort a Power Query table by two columns?
Yes. Apply the primary sort first, then add secondary and further sorts for tie-breaking. For example, sort by Department, then by Last Name, and finally by Employee ID if a fully defined order is needed.

Why does Power Query sort 1, 10, 2 instead of 1, 2, 10?
The values may be stored as text. Change the column to a numeric data type, check the conversion, and sort it again. Text sorting compares characters rather than numeric magnitudes.

How do I sort dates from newest to oldest?
Make sure the column has a date or date/time type, then select its header dropdown and choose Sort Descending. Descending date order places the latest dates first.

Is Power Query sorting the original source file?
No. A query sort orders the query’s result; it does not generally rearrange the original workbook, CSV, or database records. Use a worksheet sort or the relevant source application if you need to change the source itself.

Is Power Query available on Mac?
Power Query is available in supported Microsoft 365 versions of Excel for Mac, but available connectors and features can differ from Windows and may depend on the update channel. Menu locations can also vary by build. The sorting idea is the same: edit the query, sort a column, and load the result; consult the current Excel for Mac ribbon for the corresponding Get Data and query-editing commands.

Related

Try it: the Formula Explainer: paste any formula and see each function and reference in plain English.

More in Formulas & functions

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.