Lookup & reference function

PIVOTBY function

Summarises data in a cross-tab with groups down the rows and across the columns, like a PivotTable built from one formula.

Available in Microsoft 365 and Excel for the web (added in 2024); not in Excel 2024 or older perpetual versions

Microsoft 365 onlyNot in Google Sheets

Syntax

PIVOTBY(row_fields, col_fields, values, function, [field_headers], [row_total_depth], [row_sort_order], [col_total_depth], [col_sort_order], [filter_array], [relative_to])
ArgumentWhat it means
row_fieldsRequiredThe column(s) whose items become row labels, e.g. Region.
col_fieldsRequiredThe column(s) whose items become column headings, e.g. Product.
valuesRequiredThe numbers (or items) to aggregate.
functionRequiredThe aggregation: SUM, AVERAGE, COUNTA, MAX, PERCENTOF, a LAMBDA, etc.
field_headersOptional0-3, as in GROUPBY: whether the data has headers and whether to show them.
row_total_depthOptional0 no row totals, 1 grand total row (default), 2 with subtotals.
row_sort_orderOptionalColumn to sort rows by; negative for descending.
col_total_depthOptional0 no total column, 1 grand total column (default), 2 with subtotals.
col_sort_orderOptionalRow to sort columns by; negative for descending.
filter_arrayOptionalTRUE/FALSE per data row to include or exclude rows.
relative_toOptionalFor PERCENTOF: what each percentage is relative to - 0 column totals (default), 1 row totals, 2 grand total, 3 parent column total, 4 parent row total.

Returns: A spilled cross-tab: row labels down the left, column labels across the top, aggregated values at each intersection, plus totals.

How PIVOTBY works

PIVOTBY creates a two-way summary - regions down the side, products across the top, totals in the margins - from a single formula. It does what a PivotTable does for everyday reports, but the result is live: change a number in the data and the cross-tab updates immediately, with no Refresh. And because it is a formula, other formulas can read from it, and you can copy it to build a variant in seconds.

The first four arguments are all you need: row field, column field, values and the aggregation function. Groups are sorted ascending in both directions, and a Total row and a Total column are added. Intersections with no data stay empty. The optional arguments switch totals off (0), sort rows or columns by their values, filter the data, or, with PERCENTOF, express each cell as a share of its row, column or grand total.

If you only need groups in one direction, GROUPBY is simpler. Both functions are part of Microsoft 365 only.

Examples

1Revenue by region and product

G2fx=PIVOTBY(B2:B8, C2:C8, E2:E8, SUM)
ABCDEFGHIJK
1RepRegionProductUnitsRevenue
2AnaNorthLaptop43600LaptopMonitorMouseTotal
3BenSouthMonitor101490East18003602160
4ChloeNorthMonitor6894North36008941444638
5AnaEastLaptop21800South450014905990
6DevSouthLaptop54500Total9900238450412788
7BenEastMouse30360
8ChloeNorthMouse12144

Regions run down the side and products across the top. Empty cells mean that region sold none of that product. The last row and column hold the totals.

2Count of sales, no totals

G2fx=PIVOTBY(A2:A8, B2:B8, C2:C8, COUNTA, , 0, , 0)
ABCDEFGHIJ
1RepRegionProductUnitsRevenue
2AnaNorthLaptop43600EastNorthSouth
3BenSouthMonitor101490Ana11
4ChloeNorthMonitor6894Ben11
5AnaEastLaptop21800Chloe2
6DevSouthLaptop54500Dev1
7BenEastMouse30360
8ChloeNorthMouse12144

COUNTA counts how many sales lines each rep has in each region. Row and column totals are switched off with the two zeros.

3Units by product and region

G2fx=PIVOTBY(C2:C8, B2:B8, D2:D8, SUM, , 0, , 0)
ABCDEFGHIJ
1RepRegionProductUnitsRevenue
2AnaNorthLaptop43600EastNorthSouth
3BenSouthMonitor101490Laptop245
4ChloeNorthMonitor6894Monitor610
5AnaEastLaptop21800Mouse3012
6DevSouthLaptop54500
7BenEastMouse30360
8ChloeNorthMouse12144

Swapping the fields puts products down the side and regions across, summing the Units column.

⬇Practice workbookExamples with live formulas + practice sheets · .xlsx · 18 KB

Common errors and how to fix them

ErrorWhy it happensFix
#VALUE!The field, value and filter ranges have different heights.Make every range cover the same data rows.
#SPILL!Not enough empty space for the cross-tab.Clear the cells to the right and below.
#NAME?The Excel version has no PIVOTBY.Use a PivotTable or SUMIFS with UNIQUE labels.

Tips

  • Use PERCENTOF with relative_to 1 to show each product's share of a region's total.
  • Put the field ranges in a formatted table so new rows are included automatically.
  • Add filter_array, e.g. A2:A8<>"Dev", to exclude a rep without deleting data.

Questions

Can PIVOTBY replace PivotTables?

For many regular reports, yes. PivotTables remain better for exploring data interactively, very large tables, slicers and Data Model sources.

How do I show percentages instead of totals?

Use PERCENTOF as the function and choose relative_to - for example 1 for the share of each row's total.

Why do some cells stay empty?

No data rows exist for that row/column combination, so there is nothing to aggregate.

Google Sheets

Use QUERY with GROUP BY and PIVOT clauses, or insert a pivot table.

PIVOTBY in other languages

In a non-English version of Excel the function has a local name. Excel translates formulas automatically when a file is opened in another language.

LanguageFunction name
FrenchPIVOTER.PAR
RussianСВОДПО
SpanishPIVOTARPOR