PIVOTBY Function

Excel Functions › Lookup & Reference

Excel 365 only Dynamic Array

The Excel PIVOTBY function builds a complete cross-tab — row groups down the side, column groups across the top, aggregated values in the grid, totals included — from a single formula. It’s GROUPBY’s two-dimensional sibling and the closest Excel has come to a PivotTable that recalculates live, never needs refreshing, and can’t go stale.


Quick answer: regions down the side, quarters across the top, sales summed in the middle:
=PIVOTBY(A2:A100, B2:B100, C2:C100, SUM)
Four arguments: row labels, column labels, the numbers, and the aggregation function — written bare, no parentheses, no quotes.

Syntax

=PIVOTBY(row_fields, col_fields, values, function, [field_headers], [row_total_depth], [row_sort_order], [col_total_depth], [col_sort_order], [filter_array])
ArgumentDescription
row_fieldsRequiredColumn(s) whose unique values become the row labels. Two adjacent columns give two grouping levels.
col_fieldsRequiredColumn(s) whose unique values run across the top.
valuesRequiredThe numbers (or text) to aggregate.
functionRequiredThe aggregation: SUM, AVERAGE, COUNT, COUNTA, MAX, MIN, MEDIAN, ARRAYTOTEXT, a custom LAMBDA, and more — written without parentheses.
field_headersOptional0–3: whether your ranges include headers and whether to show them in the result.
row_total_depth / col_total_depthOptionalHow many levels of totals to show (0 = none, 1 = grand totals, 2 = grand + subtotals). Negative values put totals first.
row_sort_order / col_sort_orderOptionalWhich column/row to sort the groups by; negative = descending.
filter_arrayOptionalTRUE/FALSE array — rows where it’s FALSE are excluded before pivoting.

Available in: Excel for Microsoft 365 only (Windows, Mac, and the web). Perpetual versions through Excel 2024 show #NAME?. The result is a dynamic array that spills.

A pivot table in one formula

Sales data sits in three columns: Region (A), Quarter (B), Amount (C). One formula in F1 produces the whole cross-tab below — labels, grid, and a totals row and column:

=PIVOTBY(A2:A100, B2:B100, C2:C100, SUM)
FGHI
1Q1Q2Total
2East3,4004,1007,500
3North5,9006,30012,200
4South4,7005,2009,900
5Total14,00015,60029,600

Note SUM is passed as a bare function name — this is an eta-reduced lambda, the same convention GROUPBY and BYROW use. Add a new sale to the source and the pivot updates on the spot; there is no Refresh button because none is needed.

=PIVOTBY(A2:A100, B2:B100, C2:C100, AVERAGE, , 0, , 0) // averages, with row and column totals switched off

Try it: interactive PIVOTBY demo

Live demo

A 10-row sales table is pivoted live below. Switch the aggregation and the totals setting and watch the cross-tab rebuild.

Sorting, filtering, percentages, and custom lambdas

The optional arguments cover most PivotTable conveniences. Sort regions by their totals, descending, and pivot only the rows that pass a filter:

=PIVOTBY(A2:A100, B2:B100, C2:C100, SUM, , 1, -2, 1, , D2:D100="Online") // online sales only, rows sorted by total descending

The function argument accepts PERCENTOF for share-of-total views, and full LAMBDAs when the built-ins aren’t enough:

=PIVOTBY(A2:A100, B2:B100, C2:C100, PERCENTOF) // each cell as a share of the grand total
=PIVOTBY(A2:A100, B2:B100, C2:C100, LAMBDA(x, MAX(x)-MIN(x))) // custom: range of values per cell

Need just one dimension — row groups with no columns across the top? That’s GROUPBY, which shares every argument except col_fields.

PIVOTBY or a real PivotTable? PIVOTBY wins for live-updating reports that feed other formulas. Classic PivotTables still win for drag-and-drop exploration, drill-down, and very large source data.

Errors & common pitfalls

#NAME? — not on Microsoft 365. PIVOTBY ships only in Microsoft 365 (and Excel for the web). Excel 2024 and every earlier perpetual release don’t have it.

#VALUE! — mismatched range lengths. row_fields, col_fields, values, and filter_array must all cover the same number of rows. A range that’s one row short is the usual culprit.

#SPILL! — the landing zone isn’t empty. A pivot output is wide and tall, and it grows as new groups appear in the data. Give it generous empty space.

Pitfall: writing the function with parentheses. It’s SUM, not SUM() and not "SUM". You’re passing the function itself, not calling it.

Pitfall: headers swallowed into the groups. If your ranges include header cells, tell PIVOTBY via field_headers (3 = has headers, show them) — otherwise “Region” turns up as a group label.

Practice workbook

📊
Download the free PIVOTBY practice workbook
Every example on this page, ready to open in Excel — plus practice challenges with answers on a separate tab. No sign-up required.

Frequently asked questions

What's the difference between PIVOTBY and GROUPBY?
One dimension. GROUPBY aggregates into row groups only; PIVOTBY adds col_fields, spreading a second field across the top to form a true cross-tab. The other arguments are essentially the same.
Does PIVOTBY replace PivotTables?
For live formula-driven reports, often yes - it recalculates automatically and its output feeds other formulas. PivotTables still win for interactive exploration, drill-down, grouping dates by month/year on the fly, and huge data sets.
Why do I write SUM without parentheses in PIVOTBY?
Because you're passing the function itself as a value (an eta-reduced lambda), not calling it. PIVOTBY applies it to each group internally. SUM() or "SUM" both fail.
How do I turn off the totals row in PIVOTBY?
Set row_total_depth (argument 6) to 0 for no row totals, and col_total_depth (argument 8) to 0 for no column totals. Use -1 to place grand totals before the data instead of after.
Which Excel versions have PIVOTBY?
Microsoft 365 subscriptions only (Windows, Mac, web). It is not in Excel 2024 or any perpetual-license version - those show #NAME?.
Can I filter the data before pivoting?
Yes, two ways: the built-in filter_array argument (a TRUE/FALSE column), or wrap the source ranges in FILTER before they reach PIVOTBY.

Master functions like this in one day

This page covers one function. Our Excel Formulas and Functions class covers the 30 that matter most — live, hands-on, taught by professionals in Dallas–Fort Worth, Houston, Austin, Oklahoma City, Denver, or online.

See the Formulas & Functions Class

Related functions: GROUPBY · GETPIVOTDATA · FILTER · UNIQUE · TRANSPOSE