Smart Systems for Home

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

Power Query List.MaxN Function: How to Use It

· 5 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

List.MaxN is a Power Query (M language) function that returns the N largest values from a list. You feed it a list and a count - for example List.MaxN({3, 9, 1, 7}, 2) - and it hands back {9, 7}, the top two values sorted descending. It is not exposed as a button anywhere in the Power Query ribbon, so you use it by typing M code: in a custom column, in the Advanced Editor, or in the formula bar. It works in Excel Power Query, Power BI Desktop's Power Query Editor, and anywhere else M is available. There is no built-in prerequisite beyond having a query that contains the values you want ranked.

What You'll Need

  • Excel with Power Query built in (any modern Excel version) or Power BI Desktop
  • A query that loads or contains the list of values you want the top N from
  • Basic familiarity with the Power Query Editor window and the formula bar

Step-by-Step

  1. Open your workbook in Excel and load your source data into Power Query: select the data, then go to Data > From Table/Range. (In Power BI, use Home > Transform Data. Menu labels can differ slightly between versions and apps.)
  2. Once the Power Query Editor opens, find the column holding the values you want to rank - for example a Sales Amount column.
  3. To get the top N values of the whole column in one step, add a custom step: click the fx button in the formula bar (or right-click the last applied step and choose Insert Step After).
  4. In the formula bar, type a formula such as = List.MaxN(Source[Sales Amount], 5), replacing Source with the actual name of your previous step and Sales Amount with your column name. Press Enter.
  5. Power Query shows a list preview with the 5 largest values, sorted from largest to smallest. If you want the result as its own table, wrap it: = Table.FromRecords(List.MaxN(Source[Sales Amount], 5), {"TopValues"}) is not valid syntax - instead use = Table.FromList(List.MaxN(Source[Sales Amount], 5), Splitter.SplitByNothing(), {"TopValues"}).
  6. To get the top N per group (for example the top 3 sales per region), use Group By: select the grouping column (Region), then Transform > Group By, and choose Operation = All Rows, name the new column Data.
  7. Add a custom column on the grouped table: Add Column > Custom Column, name it Top3, and enter List.MaxN([Data][Sales Amount], 3). Each row now contains a list of the top 3 values for that region.
  8. Click the expand icon (two arrows) in the header of the Top3 column to split the list into rows, then remove the Data column. Click Close & Load to send the result back to your workbook.
  9. Optionally add a comparison criterion as a third argument: List.MaxN(list, 2, (x, y) => Value.Compare(x, y)) is the default, but you can supply a custom comparer if you need special ordering rules.

Common Mistakes

  • Confusing List.MaxN with List.Max: List.Max returns a single value, while List.MaxN returns a list of N values. Trying to use the result of List.MaxN as a number causes an error.
  • Forgetting the column reference syntax: Source[Sales Amount] with square brackets returns the column as a list. Using parentheses or quoting the column name fails.
  • Typing List.MaxN(Sales Amount, 5) without the step name. You must reference the previous step's name, not just the column.
  • Passing a count larger than the list length - this simply returns the whole list sorted, which may not be what you expect, so check your N.
  • Expecting null handling: nulls in the list can produce unexpected results or errors depending on comparison, so filter or replace nulls first.
  • Mixing data types in the list (text and numbers) will throw a comparison error. Set the column type before calling the function.

Tips

  • The returned list is already sorted descending, so there is no need for a separate List.Sort afterwards.
  • To return the top N rows instead of just values, sort the table descending on your column, then keep the first N rows with Table.FirstN - often more useful than MaxN alone.
  • List.MinN is the mirror function for the smallest N values; the syntax is identical.
  • You can make N dynamic with a parameter: create a blank query with a number (call it TopN), then use List.MaxN(Source[Sales Amount], TopN) so you can change it in one place.
  • If you only need one maximum per group, Group By with the Max operation is simpler than MaxN.

Troubleshooting

If you see a formula error, first check the spelling and capitalization of the function name - it must be exactly List.MaxN. Next, verify the column reference includes the step name and square brackets. An error like "We cannot convert the value to type List" usually means you referenced the wrong step or the column name has a typo. If the result shows an error inside cells, check for mixed data types or nulls in the source column and set the column type explicitly. If the Group By approach produces errors, confirm you used the All Rows aggregation - the Max operation gives a single value, not a nested table you can drill into. Finally, if the Expand step fails, make sure the custom column result is a list and not a table or record.

Frequently Asked Questions

What is the difference between List.Max and List.MaxN?

List.Max returns the single largest value in a list. List.MaxN returns a new list containing the N largest values, sorted from largest to smallest.

Is the result of List.MaxN sorted?

Yes. The returned list is ordered descending, so the first item is the largest of the N values.

Can I use List.MaxN with dates or text?

Yes. Any comparable values work - dates compare chronologically and text compares alphabetically - as long as all items in the list are the same type.

What happens if N is larger than the number of items in the list?

No error occurs. List.MaxN simply returns all the items in the list, sorted descending.

How do I get the top N rows instead of just the values?

Sort your table descending on the value column, then use Table.FirstN with the same count, or combine it with Table.MaxN, which does exactly this on a table.

Does List.MaxN work in both Excel and Power BI?

Yes. It is part of the M language, so it behaves identically in Excel's Power Query and the Power Query Editor in Power BI Desktop.