GROUPBY function
Groups rows by one or more fields and aggregates values for each group with a formula, like a mini PivotTable that updates itself.
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
GROUPBY(row_fields, values, function, [field_headers], [total_depth], [sort_order], [filter_array], [field_relationship])| Argument | What it means | |
|---|---|---|
row_fields | Required | The column(s) to group by, e.g. Region. Several adjacent columns create nested groups. |
values | Required | The column(s) to aggregate, e.g. Revenue. |
function | Required | How to aggregate: a function name such as SUM, AVERAGE, COUNTA, MAX, PERCENTOF, or a LAMBDA. |
field_headers | Optional | 0 no headers, 1 headers in data but not shown, 2 no headers but generate them, 3 headers in data and show them. If omitted Excel guesses from the first row. |
total_depth | Optional | 0 no totals, 1 grand total (default), 2 grand and subtotals; negative values put totals at the top. |
sort_order | Optional | Column number to sort by (1 = first row field). Negative numbers sort descending. Default sorts ascending by the row field. |
filter_array | Optional | A TRUE/FALSE array, one per row, to include only some rows. |
field_relationship | Optional | 0 hierarchy (default) or 1 table, for several row fields. |
Returns: A spilled summary table: one row per group with its aggregated value, plus totals according to total_depth.
How GROUPBY works
GROUPBY answers "what is the total per category?" with one formula. Give it the column to group by, the numbers to summarise and the function to use, and it returns a summary table with one row per group and a grand total - the same thing a simple PivotTable does, but as a formula that recalculates instantly and can be referenced by other formulas, without any Refresh button.
By default the groups are sorted A-Z and a Total row is added at the bottom. sort_order sorts by any output column (use a negative number for descending, e.g. -2 to put the biggest totals first), total_depth 0 removes the totals, and filter_array lets you leave rows out before grouping. The function argument accepts any function that aggregates - SUM, AVERAGE, COUNT, COUNTA, MIN, MAX, MEDIAN, PERCENTOF - or your own LAMBDA.
When your ranges include the header row, set field_headers to 3 so the headers are treated as labels and shown above the result. For a two-dimensional summary with groups across the top too, use PIVOTBY.
Examples
1Revenue by region with a total
| A | B | C | D | E | F | G | H | |
|---|---|---|---|---|---|---|---|---|
| 1 | Rep | Region | Product | Units | Revenue | |||
| 2 | Ana | North | Laptop | 4 | 3600 | East | 2160 | |
| 3 | Ben | South | Monitor | 10 | 1490 | North | 4638 | |
| 4 | Chloe | North | Monitor | 6 | 894 | South | 5990 | |
| 5 | Ana | East | Laptop | 2 | 1800 | Total | 12788 | |
| 6 | Dev | South | Laptop | 5 | 4500 | |||
| 7 | Ben | East | Mouse | 30 | 360 | |||
| 8 | Chloe | North | Mouse | 12 | 144 |
Each region's revenue is added up; groups are sorted A-Z and a Total row (12,788) is added at the end.
2Include and show the headers
| A | B | C | D | E | F | G | H | |
|---|---|---|---|---|---|---|---|---|
| 1 | Rep | Region | Product | Units | Revenue | |||
| 2 | Ana | North | Laptop | 4 | 3600 | Region | Revenue | |
| 3 | Ben | South | Monitor | 10 | 1490 | East | 2160 | |
| 4 | Chloe | North | Monitor | 6 | 894 | North | 4638 | |
| 5 | Ana | East | Laptop | 2 | 1800 | South | 5990 | |
| 6 | Dev | South | Laptop | 5 | 4500 | Total | 12788 | |
| 7 | Ben | East | Mouse | 30 | 360 | |||
| 8 | Chloe | North | Mouse | 12 | 144 |
The ranges now start in row 1. field_headers 3 tells Excel the first row is headers and to display them above the summary.
3Biggest region first
| A | B | C | D | E | F | G | H | |
|---|---|---|---|---|---|---|---|---|
| 1 | Rep | Region | Product | Units | Revenue | |||
| 2 | Ana | North | Laptop | 4 | 3600 | South | 5990 | |
| 3 | Ben | South | Monitor | 10 | 1490 | North | 4638 | |
| 4 | Chloe | North | Monitor | 6 | 894 | East | 2160 | |
| 5 | Ana | East | Laptop | 2 | 1800 | Total | 12788 | |
| 6 | Dev | South | Laptop | 5 | 4500 | |||
| 7 | Ben | East | Mouse | 30 | 360 | |||
| 8 | Chloe | North | Mouse | 12 | 144 |
sort_order -2 sorts by the second output column (the sums) in descending order. The Total row stays at the bottom.
4Leave some rows out
| A | B | C | D | E | F | G | H | |
|---|---|---|---|---|---|---|---|---|
| 1 | Rep | Region | Product | Units | Revenue | |||
| 2 | Ana | North | Laptop | 4 | 3600 | East | 1800 | |
| 3 | Ben | South | Monitor | 10 | 1490 | North | 4494 | |
| 4 | Chloe | North | Monitor | 6 | 894 | South | 5990 | |
| 5 | Ana | East | Laptop | 2 | 1800 | |||
| 6 | Dev | South | Laptop | 5 | 4500 | |||
| 7 | Ben | East | Mouse | 30 | 360 | |||
| 8 | Chloe | North | Mouse | 12 | 144 |
filter_array drops the mouse sales before grouping, and total_depth 0 removes the Total row. East is left with only Ana's laptop sale.
Common errors and how to fix them
| Error | Why it happens | Fix |
|---|---|---|
#VALUE! | row_fields, values and filter_array have different heights. | Make all ranges cover the same rows. |
#SPILL! | The summary needs more room than is free. | Clear the area to the right and below the formula. |
#NAME? | The Excel version does not have GROUPBY (Excel 2024 or older). | Use a PivotTable, or UNIQUE with SUMIFS. |
Tips
=GROUPBY(C2:C8, E2:E8, AVERAGE, , 0)gives average revenue per product: Laptop 3300, Monitor 1192, Mouse 252.- Pass several value columns side by side, e.g.
D2:E8, to aggregate units and revenue together. - Use
PERCENTOFas the function to show each group's share of the total. - If you need compatibility with older Excel,
=SUMIFS(E2:E8, B2:B8, G2)next to a UNIQUE list gives the same numbers.
Questions
Should I use GROUPBY or a PivotTable?
GROUPBY updates instantly and can feed other formulas; PivotTables are better for drag-and-drop exploration, very large data and Data Model sources.
How do I remove the Total row?
Set the fifth argument, total_depth, to 0: =GROUPBY(B2:B8, E2:E8, SUM, , 0).
Why does Excel show #NAME? for GROUPBY?
It is only in Microsoft 365 and Excel for the web. Excel 2021 and Excel 2024 do not have it.
Google Sheets
Use QUERY with GROUP BY (for example QUERY(A:C, "select A, sum(C) group by A")) or a pivot table.
GROUPBY 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.
| Language | Function name |
|---|---|
| French | GROUPER.PAR |
| Portuguese (Brazil) | AGRUPARPOR |
| Spanish | AGRUPARPOR |