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.

GROUPBY 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 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.

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

    =GROUPBY(C2:C9, E2:E9, SUM)

    Result: Spills 4 regions and a Total row

    East1470
    North1550
    South1640
    West80
    Total4740

    Regions are sorted A–Z and the grand total is added by default.

  2. 2.Sort by the totals, largest first

    =GROUPBY(B2:B9, E2:E9, SUM, , , -2)

    Result: Spills 4 reps and a Total row

    Ana1640
    Ben1550
    Cara1470
    Dev80
    Total4740

    -2 sorts by the 2nd output column, the totals, descending.

  3. 3.Only some rows, no totals

    =GROUPBY(C2:C9, E2:E9, SUM, , 0, , D2:D9="Laptop")

    Result: Spills 3 regions

    East1150
    North1200
    South1250

    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

GROUPBY 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, 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.

your-file.xlsx

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.

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.

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