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.
| Version | Has it |
|---|---|
| Microsoft 365 (Windows and Mac) | Yes |
| Excel for the web | Yes |
| Excel 2024 | No |
| Excel 2021 | No |
| Excel 2019 | No |
| Excel 2016 | No |
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.
| Row | A | B | C | D | E |
|---|---|---|---|---|---|
| 1 | Order ID | Rep | Region | Product | Amount |
| 2 | 1001 | Ana | North | Laptop | 1200 |
| 3 | 1002 | Ben | South | Monitor | 300 |
| 4 | 1003 | Ana | North | Monitor | 350 |
| 5 | 1004 | Cara | East | Laptop | 1150 |
| 6 | 1005 | Ben | South | Laptop | 1250 |
| 7 | 1006 | Dev | West | Keyboard | 80 |
| 8 | 1007 | Cara | East | Monitor | 320 |
| 9 | 1008 | Ana | South | Keyboard | 90 |
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
empty Keyboard Laptop Monitor Total East empty 1150 320 1470 North empty 1200 350 1550 South 90 1250 300 1640 West 80 empty empty 80 Total 170 3600 970 4740 Regions down the side, products across the top, and a grand total of 4,740 in the corner.
2.Without totals
=PIVOTBY(B2:B9, D2:D9, E2:E9, SUM, , 0, , 0)Result: Spills reps by products, no Total row or column
empty Keyboard Laptop Monitor Ana 90 1200 350 Ben empty 1250 300 Cara empty 1150 320 Dev 80 empty empty row_total_depth and col_total_depth are both 0.
3.Count orders instead of summing
=PIVOTBY(C2:C9, D2:D9, E2:E9, COUNT)Result: Spills order counts; the grand total is 8
empty Keyboard Laptop Monitor Total East empty 1 1 2 North empty 1 1 2 South 1 1 1 3 West 1 empty empty 1 Total 2 3 3 8 COUNT counts the amounts in each region and product pair.
Common errors and fixes
| Error | Why it happens | Fix |
|---|---|---|
| #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.
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.
- Written for your platform and version
- Checked against your actual data
- Newest functions looked up in official docs
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