Excel functions · Summaries

PIVOTBY function in Excel

PIVOTBY builds a cross-tab summary from one formula, grouping rows by one field and columns by another and aggregating a value where they meet, such as sales by region and product. It works like a PivotTable with row and column fields, spills its result with totals, and updates when the data changes.

Last reviewed

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])
row_fieldsrequired
The column (or columns) whose values become row labels.
col_fieldsrequired
The column (or columns) whose values become column headers.
valuesrequired
The column (or columns) to aggregate.
functionrequired
How to aggregate: SUM, AVERAGE, COUNT, PERCENTOF and others, or a LAMBDA.
field_headersoptional
Whether your ranges include headers and whether to show them, as in GROUPBY (0 to 3).
row_total_depthoptional
Totals for rows: 0 none, 1 grand total, 2 with subtotals; negative puts them at the top.
row_sort_orderoptional
How to sort the rows; negative is descending.
col_total_depthoptional
Totals for columns, with the same values as row_total_depth.
col_sort_orderoptional
How to sort the columns; negative is descending.
filter_arrayoptional
A TRUE/FALSE test with one entry per row to include only some rows.
relative_tooptional
For PERCENTOF: what each value is a share of, such as column totals (the default) or row totals.
  • Rows and columns sort A–Z by default, and both get a Total unless you set the total depths to 0.
  • Combinations with no data show as empty cells.

Which versions have PIVOTBY

Microsoft 365 only. Not in Excel 2021, 2019 or 2016.

PIVOTBY is in Microsoft 365 only. It became generally available in the Current Channel in 2024, so an older Microsoft 365 build may not have it yet.

PIVOTBY availability by Excel version
VersionHas it
Microsoft 365 (Windows and Mac)Yes
Excel for the webYes
Excel 2024No
Excel 2021No
Excel 2019No
Excel 2016No

Google Sheets

Google Sheets has no PIVOTBY or GROUPBY. Use a pivot table, or QUERY with GROUP BY and PIVOT clauses, such as =QUERY(A1:E9, "select C, sum(E) group by C pivot D", 1).

Examples

The examples use this sample sheet.

Orders (A1:E9)
RowABCDE
1Order IDRepRegionProductAmount
21001AnaNorthLaptop1200
31002BenSouthMonitor300
41003AnaNorthMonitor350
51004CaraEastLaptop1150
61005BenSouthLaptop1250
71006DevWestKeyboard80
81007CaraEastMonitor320
91008AnaSouthKeyboard90
  1. 1.Sales by region and product

    =PIVOTBY(C2:C9, D2:D9, E2:E9, SUM)

    Result: Spills a 5-row by 5-column grid with totals

    emptyKeyboardLaptopMonitorTotal
    Eastempty11503201470
    Northempty12003501550
    South9012503001640
    West80emptyempty80
    Total17036009704740

    Regions down the side, products across the top, and a grand total of 4,740 in the corner.

  2. 2.Without totals

    =PIVOTBY(B2:B9, D2:D9, E2:E9, SUM, , 0, , 0)

    Result: Spills reps by products, no Total row or column

    emptyKeyboardLaptopMonitor
    Ana901200350
    Benempty1250300
    Caraempty1150320
    Dev80emptyempty

    row_total_depth and col_total_depth are both 0.

  3. 3.Count orders instead of summing

    =PIVOTBY(C2:C9, D2:D9, E2:E9, COUNT)

    Result: Spills order counts; the grand total is 8

    emptyKeyboardLaptopMonitorTotal
    Eastempty112
    Northempty112
    South1113
    West1emptyempty1
    Total2338

    COUNT counts the amounts in each region and product pair.

Common errors and fixes

PIVOTBY errors, causes and fixes
ErrorWhy it happensFix
#NAME?Your Excel doesn't have the function: it's in Microsoft 365 only, not in Excel 2021, 2019 or 2016.Open the file in Microsoft 365, or build a PivotTable instead.
#SPILL!The summary needs more cells than are free, or the formula is inside an Excel table.Clear the cells below and to the right, or move the formula outside the table.
#VALUE!row_fields, col_fields, values and filter_array cover different numbers of rows.Make every range cover the same rows.

Do it with GPTExcel

Skip writing PIVOTBY by hand

Upload your Excel or CSV file to Chat and ask in plain English. GPTExcel writes the formula for your file and your version of Excel or Google Sheets.

your-file.xlsx

Build a pivot of sales by region and product with a slicer for rep.

Done. I used PIVOTBY on your sheet and checked the results against your data.

your-file-updated.xlsxYour original stays unchanged
  • Written for your platform and version
  • Checked against your actual data
  • Newest functions looked up in official docs
Try it free Formula Generator

Free plan, no credit card.

PIVOTBY questions

What's the difference between GROUPBY and PIVOTBY?
GROUPBY groups rows only, giving a list with one line per group. PIVOTBY groups rows and columns, giving a cross-tab grid like a PivotTable with both row and column fields.
Can I use PIVOTBY in Google Sheets?
No. Google Sheets doesn't have PIVOTBY; use a pivot table or QUERY with a PIVOT clause instead.

Skip the formula hunt

Upload a spreadsheet and ask in plain English. Free plan, no credit card.

Start free