Excel functions · Summaries
GROUPBY function in Excel
GROUPBY groups the rows of your data by one or more columns and aggregates a value column for each group, such as total sales by region, in a single formula that spills a summary table with a total row. It's a formula alternative to a PivotTable that updates as soon as the data changes.
Last reviewed
Syntax
=GROUPBY(row_fields, values, function, [field_headers], [total_depth], [sort_order], [filter_array], [field_relationship])- row_fieldsrequired
- The column (or columns) to group by, such as Region.
- valuesrequired
- The column (or columns) to aggregate, such as Amount.
- functionrequired
- How to aggregate: SUM, AVERAGE, COUNT, COUNTA, MAX, MIN, PERCENTOF and others, or a LAMBDA.
- field_headersoptional
- Whether your ranges include headers and whether to show them: 0 no, 1 yes but don't show, 2 no but generate, 3 yes and show. Left out, Excel guesses.
- total_depthoptional
- 0 no totals, 1 grand total, 2 grand total and subtotals; negative values put them at the top. Left out, Excel shows a grand total (and subtotals where it can).
- sort_orderoptional
- Which output column to sort by: 1 is the first group column, then the value columns follow. Negative sorts descending. Groups sort A–Z by default.
- filter_arrayoptional
- A TRUE/FALSE test with one entry per row, such as D2:D9="Laptop", to include only some rows.
- field_relationshipoptional
- With several group columns: 0 treats them as a hierarchy (the default), 1 as independent table columns.
- The function argument is the function's name without brackets: SUM, not SUM().
- Include the header row and set field_headers to 3 to label the output: =GROUPBY(C1:C9, E1:E9, SUM, 3).
- Leave optional arguments empty with commas to reach a later one.
Which versions have GROUPBY
Microsoft 365 only. Not in Excel 2021, 2019 or 2016.
GROUPBY 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 GROUPBY or PIVOTBY. Use a pivot table, or QUERY with a GROUP BY or PIVOT clause, such as =QUERY(A1:E9, "select C, sum(E) group by C", 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.Total sales by region
=GROUPBY(C2:C9, E2:E9, SUM)Result: Spills 4 regions and a Total row
East 1470 North 1550 South 1640 West 80 Total 4740 Regions are sorted A–Z and the grand total is added by default.
2.Sort by the totals, largest first
=GROUPBY(B2:B9, E2:E9, SUM, , , -2)Result: Spills 4 reps and a Total row
Ana 1640 Ben 1550 Cara 1470 Dev 80 Total 4740 -2 sorts by the 2nd output column, the totals, descending.
3.Only some rows, no totals
=GROUPBY(C2:C9, E2:E9, SUM, , 0, , D2:D9="Laptop")Result: Spills 3 regions
East 1150 North 1200 South 1250 filter_array keeps the laptop orders only, and total_depth 0 drops the Total row. West sold no laptops, so it's not listed.
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, values and filter_array cover different numbers of rows. | Make every range cover the same rows. |
Do it with GPTExcel
Skip writing GROUPBY 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.
Summarize my sales by region and rep, and turn it into a native pivot table with a chart.
Done. I used GROUPBY 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
GROUPBY questions
- Is GROUPBY better than a PivotTable?
- It's different. GROUPBY updates the moment the data changes and can feed other formulas, but it has no slicers or drag-and-drop layout. A PivotTable needs a refresh but is easier to rearrange and works in every Excel version.
- Which Excel versions have GROUPBY?
- Microsoft 365 only. Excel 2021, 2019 and 2016 don't have it, and neither does Google Sheets, where QUERY with GROUP BY does a similar job.
- How do I count rows per group with GROUPBY?
- Use COUNTA as the function: =GROUPBY(D2:D9, D2:D9, COUNTA) returns Keyboard 2, Laptop 3, Monitor 3 and a Total of 8 on the sample data.
Skip the formula hunt
Upload a spreadsheet and ask in plain English. Free plan, no credit card.
Start free