Table.MaxN

Table

Returns the top N rows with the highest values according to the given comparison criteria.

Examples on this page use shared sample tables. View them to understand the input data before reading the examples below.

Syntax

Table.MaxN(table as table, comparisonCriteria as any, countOrCondition as any) as table

Parameters

NameTypeRequiredDescription
tabletableYesThe table to search.
comparisonCriteriaanyYesA column name, list of column names, or comparer function defining the sort order.
countOrConditionanyYesA number specifying how many top rows to return, or a condition function.

Return Value

tableA 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)
Result
OrderID
CustomerName
Product
UnitPrice
Quantity
16BobThingamajig E1201
24AliceGadget D751
32BobGadget B502

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)
Result
OrderID
CustomerName
Product
UnitPrice
16BobThingamajig E120
24AliceGadget D75
32BobGadget B50

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)
Result
EmployeeID
FullName
Department
Salary
1E002BOB JONESEngineering95,000
2E005Eve MartinezSales88,000

Compatibility

Power BI Desktop Power BI Service Excel Desktop Excel Online Dataflows Fabric Notebooks