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])| Argument | What it means | |
|---|---|---|
row_fields | Required | The column(s) whose items become row labels, e.g. Region. |
col_fields | Required | The column(s) whose items become column headings, e.g. Product. |
values | Required | The numbers (or items) to aggregate. |
function | Required | The aggregation: SUM, AVERAGE, COUNTA, MAX, PERCENTOF, a LAMBDA, etc. |
field_headers | Optional | 0-3, as in GROUPBY: whether the data has headers and whether to show them. |
row_total_depth | Optional | 0 no row totals, 1 grand total row (default), 2 with subtotals. |
row_sort_order | Optional | Column to sort rows by; negative for descending. |
col_total_depth | Optional | 0 no total column, 1 grand total column (default), 2 with subtotals. |
col_sort_order | Optional | Row to sort columns by; negative for descending. |
filter_array | Optional | TRUE/FALSE per data row to include or exclude rows. |
relative_to | Optional | For 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
| A | B | C | D | E | F | G | H | I | J | K | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | Rep | Region | Product | Units | Revenue | ||||||
| 2 | Ana | North | Laptop | 4 | 3600 | Laptop | Monitor | Mouse | Total | ||
| 3 | Ben | South | Monitor | 10 | 1490 | East | 1800 | 360 | 2160 | ||
| 4 | Chloe | North | Monitor | 6 | 894 | North | 3600 | 894 | 144 | 4638 | |
| 5 | Ana | East | Laptop | 2 | 1800 | South | 4500 | 1490 | 5990 | ||
| 6 | Dev | South | Laptop | 5 | 4500 | Total | 9900 | 2384 | 504 | 12788 | |
| 7 | Ben | East | Mouse | 30 | 360 | ||||||
| 8 | Chloe | North | Mouse | 12 | 144 |
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
| A | B | C | D | E | F | G | H | I | J | |
|---|---|---|---|---|---|---|---|---|---|---|
| 1 | Rep | Region | Product | Units | Revenue | |||||
| 2 | Ana | North | Laptop | 4 | 3600 | East | North | South | ||
| 3 | Ben | South | Monitor | 10 | 1490 | Ana | 1 | 1 | ||
| 4 | Chloe | North | Monitor | 6 | 894 | Ben | 1 | 1 | ||
| 5 | Ana | East | Laptop | 2 | 1800 | Chloe | 2 | |||
| 6 | Dev | South | Laptop | 5 | 4500 | Dev | 1 | |||
| 7 | Ben | East | Mouse | 30 | 360 | |||||
| 8 | Chloe | North | Mouse | 12 | 144 |
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
| A | B | C | D | E | F | G | H | I | J | |
|---|---|---|---|---|---|---|---|---|---|---|
| 1 | Rep | Region | Product | Units | Revenue | |||||
| 2 | Ana | North | Laptop | 4 | 3600 | East | North | South | ||
| 3 | Ben | South | Monitor | 10 | 1490 | Laptop | 2 | 4 | 5 | |
| 4 | Chloe | North | Monitor | 6 | 894 | Monitor | 6 | 10 | ||
| 5 | Ana | East | Laptop | 2 | 1800 | Mouse | 30 | 12 | ||
| 6 | Dev | South | Laptop | 5 | 4500 | |||||
| 7 | Ben | East | Mouse | 30 | 360 | |||||
| 8 | Chloe | North | Mouse | 12 | 144 |
Swapping the fields puts products down the side and regions across, summing the Units column.
Common errors and how to fix them
| Error | Why it happens | Fix |
|---|---|---|
#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
PERCENTOFwithrelative_to1 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.