Table.MaxN
TableReturns the top N rows with the highest values according to the given comparison criteria.
Syntax
Table.MaxN(table as table, comparisonCriteria as any, countOrCondition as any) as tableParameters
| Name | Type | Required | Description |
|---|---|---|---|
table | table | Yes | The table to search. |
comparisonCriteria | any | Yes | A column name, list of column names, or comparer function defining the sort order. |
countOrCondition | any | Yes | A number specifying how many top rows to return, or a condition function. |
Return Value
table — A table containing the rows with the N highest values.
Remarks
Table.MaxN returns the top N rows by the specified comparison criteria, ordered from highest to lowest. When countOrCondition is a number, it returns exactly that many rows. When it is a function (predicate), it returns rows from the top of the sorted order while the predicate is true — stopping as soon as the first non-matching row is found.
Table.MaxN is useful for top-N analysis — finding the top 3 customers by revenue, the 5 most expensive products, or all orders above a threshold. It is equivalent to Table.FirstN(Table.Sort(table, {criteria, Order.Descending}), N) but expresses the intent more directly.
Note that when using a condition function as countOrCondition, the condition is applied to the rows in the already-sorted (descending) order. A row that fails the condition stops the scan — any rows after it are not considered even if they would satisfy the predicate. This is a "leading contiguous" behavior, not a filter.
Examples
Example 1: Top 3 orders by unit price
let
Sales = #table(
{"OrderID", "CustomerName", "Product", "UnitPrice", "Quantity"},
{{1,"Alice","Widget A",25.00,4},{2,"Bob","Gadget B",50.00,2},
{3,"Charlie","Widget C",15.00,10},{4,"Alice","Gadget D",75.00,1},
{6,"Bob","Thingamajig E",120.00,1}}
)
in
Table.MaxN(Sales, "UnitPrice", 3)OrderID | CustomerName | Product | UnitPrice | Quantity | |
|---|---|---|---|---|---|
| 1 | 6 | Bob | Thingamajig E | 120 | 1 |
| 2 | 4 | Alice | Gadget D | 75 | 1 |
| 3 | 2 | Bob | Gadget B | 50 | 2 |
Example 2: All orders with unit price at or above 50
let
Sales = #table(
{"OrderID", "CustomerName", "Product", "UnitPrice"},
{{1,"Alice","Widget A",25.00},{2,"Bob","Gadget B",50.00},
{4,"Alice","Gadget D",75.00},{6,"Bob","Thingamajig E",120.00}}
)
in
Table.MaxN(Sales, "UnitPrice", each [UnitPrice] >= 50)OrderID | CustomerName | Product | UnitPrice | |
|---|---|---|---|---|
| 1 | 6 | Bob | Thingamajig E | 120 |
| 2 | 4 | Alice | Gadget D | 75 |
| 3 | 2 | Bob | Gadget B | 50 |
Example 3: Top 2 highest-paid employees
let
Employees = #table(
type table [EmployeeID = text, FullName = text, Department = text, Salary = number],
{{"E001","alice smith","Sales",55000},{"E002","BOB JONES","Engineering",95000},
{"E003","Charlie Brown","Marketing",72000},{"E005","Eve Martinez","Sales",88000}}
)
in
Table.MaxN(Employees, "Salary", 2)EmployeeID | FullName | Department | Salary | |
|---|---|---|---|---|
| 1 | E002 | BOB JONES | Engineering | 95,000 |
| 2 | E005 | Eve Martinez | Sales | 88,000 |