Power Query List.Sort: Step-by-Step Guide
In this article
The List.Sort function in Power Query sorts a list of values and returns a new list in ascending or descending order. It works in the Power Query Editor found in Excel and Power BI, and you need a query that already contains a list — either created with a list literal, produced by another function, or extracted from a table column. The function does not change the original list; it returns a new sorted list, so you must reference or output the result.
What You'll Need
- Excel (Microsoft 365 or a version that includes Power Query) or Power BI Desktop.
- A query open in the Power Query Editor. You can start from Get Data or from an existing query.
- A list to sort. This can be a list literal such as
{3, 1, 2}, a list from a table column usingTable.Column, or a list produced by another function. - Basic familiarity with the formula bar and the Advanced Editor, since List.Sort is written as an M expression.
Step-by-Step
- Open the Power Query Editor. In Excel, go to the Data tab and choose Get Data > Launch Power Query Editor (labels can differ slightly by version, device or region). In Power BI Desktop, choose Transform data from the Home ribbon.
- Create or select the query that contains your list. If you are starting from scratch, use Home > New Source > Blank Query, then open the Advanced Editor from the Home tab to type M code.
- Write a list literal or reference an existing list. For example, in the formula bar or Advanced Editor, enter:
let Source = {5, 2, 8, 1} in Source
This creates a list of four numbers. - Apply List.Sort to the list. Change the expression to:
let Source = {5, 2, 8, 1}, Sorted = List.Sort(Source) in Sorted
The result is a new list in ascending order:{1, 2, 5, 8}. - Choose ascending or descending order. List.Sort takes an optional second argument. Use
Order.AscendingorOrder.Descending. For example:List.Sort(Source, Order.Descending)
This returns{8, 5, 2, 1}. - Sort text values. List.Sort works on text as well. For example,
List.Sort({"banana", "apple", "cherry"})returns{"apple", "banana", "cherry"}. Text sorting is case-sensitive by default in some contexts, so check the output if case matters. - Sort a list from a table column. If your query has a table named
Saleswith a columnAmount, you can extract the column as a list and sort it:List.Sort(Table.Column(Sales, "Amount"))
This returns a sorted list of the amounts, not a sorted table. To sort the table itself, use Table.Sort instead. - Use the sorted list in a subsequent step. You can convert the sorted list back to a table with
Table.FromList, or use it in functions such asList.FirstorList.Lastto get the smallest or largest value. - Preview the result. After entering the expression, click outside the formula bar or press Enter. The Power Query Editor shows a preview of the list. If you see an error, check the syntax and the data types.
- Load or close & load the query. When the result is correct, use Home > Close & Load (Excel) or Home > Close & Apply (Power BI) to bring the sorted list into your workbook or data model.
Common Mistakes
- Trying to sort a table with List.Sort. List.Sort only works on lists. For tables, use Table.Sort with the column name and order.
- Forgetting that List.Sort returns a new list and does not modify the original. You must reference the new list in your output or next step.
- Using the wrong order argument. The second argument must be
Order.AscendingorOrder.Descending, not a string like "asc". - Assuming List.Sort changes the order of rows in a table. It does not; it only sorts the list you pass to it.
- Mixing data types in a list. If a list contains numbers and text, sorting may produce unexpected results or an error. Ensure the list is homogeneous.
- Expecting case-insensitive sorting for text without checking. The default comparison may treat uppercase and lowercase differently; test with your data.
Tips
- Use List.Sort together with List.Distinct to get a sorted list of unique values:
List.Sort(List.Distinct(Source)). - To sort by a custom comparison, List.Sort does not directly support a comparer function in the same way as some other languages. For complex sorting, consider adding an index column and using Table.Sort on the table instead.
- When working with dates, List.Sort sorts chronologically if the values are of type date. Ensure the list elements are actually date values, not text.
- Use the formula bar to experiment quickly; the Advanced Editor is better for multi-step queries.
- Remember that List.Sort is case-sensitive for text in many cases. If you need case-insensitive sorting, you may need to standardize the case first using Text.Lower or Text.Upper.
Troubleshooting
If List.Sort does not work as expected, check these common issues:
- Error: "We cannot convert a value of type Table to type List." You passed a table instead of a list. Use Table.Column to extract a column as a list, or use Table.Sort for tables.
- Error: "The field 'Order.Ascending' was not found." Ensure you have spelled the order constant correctly and that it is available in your version. Use
Order.AscendingorOrder.Descendingexactly. - Unexpected sort order for text. Text sorting may be case-sensitive. Convert all text to the same case with Text.Lower or Text.Upper before sorting if you need case-insensitive order.
- List contains nulls. List.Sort places nulls at the beginning for ascending order and at the end for descending order in many cases. If you need to exclude nulls, filter them out with List.Select or List.RemoveNulls before sorting.
- List contains mixed types. Sorting a list with both numbers and text may cause an error or unexpected order. Ensure all elements are of the same type, or convert them to a common type first.
- The sorted list does not appear in the output. Make sure the final step of your query returns the sorted list, not the original list. Check the
inclause of your let expression.
If you still have problems, simplify the query to a minimal example with a list literal and test List.Sort there. Then add complexity step by step to isolate the issue.
Frequently Asked Questions
Can List.Sort sort a table?
No. List.Sort works only on lists. To sort a table, use Table.Sort with the column name and order.
How do I sort a list in descending order?
Pass Order.Descending as the second argument: List.Sort(Source, Order.Descending).
Does List.Sort remove duplicates?
No. List.Sort only changes the order. To remove duplicates, wrap the list in List.Distinct before or after sorting.
Can I sort a list of records?
List.Sort can sort a list of records if you provide a comparison function, but the default behavior may not be what you expect. For sorting records by a field, it is often easier to convert the list to a table and use Table.Sort.
Is List.Sort case-sensitive?
Text sorting may be case-sensitive depending on the context. If you need case-insensitive sorting, standardize the case with Text.Lower or Text.Upper first.
What happens to null values when sorting?
Null values are typically placed at the beginning for ascending order and at the end for descending order. To exclude nulls, use List.RemoveNulls before sorting.