Practice free

GROUPBY function

GROUPBY summarizes data by one or more fields with a single formula and spills a table with one row per group, like a PivotTable that recalculates live. It adds a grand total row by default.

Example

Read it as a sentence: group the rows by Category in C2:C7 and add up the Qty from D2:D7 for each group, which starts with Household.

Practice this in the app

Syntax

GROUPBY(row_fields, values, function, [field_headers], [total_depth], [sort_order], [filter_array], [field_relationship])

Arguments in [square brackets] are optional.

row_fields (required)
The column(s) to group by.
values (required)
The column(s) to aggregate.
function (required)
The aggregation, such as SUM, AVERAGE, COUNT, or a LAMBDA.
field_headers (optional)
Whether the inputs include header rows; 0 none, 1 yes and hide, 2 generate, 3 yes and show.
total_depth (optional)
0 no totals, 1 grand total (default), 2 grand and subtotals.
sort_order (optional)
Columns to sort by; negative numbers sort descending.
filter_array (optional)
TRUE/FALSE array to include only some rows.
field_relationship (optional)
How multiple row fields relate: 0 hierarchy (default), 1 table.

Returns: A spilled summary table with one row per group.

One GROUPBY formula can fill more than one cell. When the result has several values, they spill into the cells below or to the right of the formula.

What Excel actually returns

Each formula below was run in desktop Excel. The result is exactly what the cell showed, errors included.

FormulaExcel showsCase
=GROUPBY(Items!C2:C7,Items!D2:D7,SUM) Household Sum by region
=GROUPBY(Items!C1:C7,Items!D1:D7,SUM,3) Category With headers shown
=GROUPBY(Items!C2:C7,Items!D2:D7,COUNT,0,0) Household Count, no totals
=GROUPBY(Items!C2:C7,Items!D2:D7,SUM,,,-2) Smoking Sort by value descending
=GROUPBY(Items!C2:C7,Items!D2:E7,AVERAGE) Household Two value columns
=GROUPBY(Items!C2:C7,Items!D2:D7,LAMBDA(x,MAX(x))) Household LAMBDA aggregate
=GROUPBY(Items!C2:C7,Items!D2:D3,SUM) #VALUE! Mismatched sizes
=GROUPBY(Items!C2:C7,Items!D2:D7,"SUM") #VALUE! Function as text

The Items sheet these formulas read

Watch out for

  • Includes a grand total row by default; set total_depth to 0 to hide it.
  • Rows are sorted by the group field unless you change sort_order.
  • Pass the function name bare (SUM), without parentheses or arguments.

Where it works

Excel
GROUPBY is only in Excel for Microsoft 365 and Excel for the web. Recent 365 addition; argument list written of the rollout, so verify against your build.
Google Sheets
No direct equivalent: `=QUERY(range,"select Col1, sum(Col2) group by Col1")`. Sheets uses QUERY with GROUP BY or a pivot table. Newer Sheets versions may have added a similar function; unsure.

Part of Lookup and reference functions. Every result on this page was measured in desktop Excel, build 16.0.20527. Last updated .