1. Home
  2. Functions
  3. Array
  4. GROUPBY

GROUPBY function

Groups rows by the values of one or more columns and aggregates the values of each group.

Syntax

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

Arguments

ArgumentRequiredDescription
row_fieldsRequiredThe column(s) to group by
valuesRequiredThe column(s) to aggregate
functionRequiredA function name such as SUM, or a LAMBDA
field_headersOptional0 no headers, 1 headers not shown, 2 no headers but generate them, 3 headers shown
total_depthOptional0 no totals, 1 grand total (default), 2 subtotals too; negative puts them on top
sort_orderOptionalColumn number to sort by, negative for descending
filter_arrayOptionalTRUE/FALSE per row to include
field_relationshipOptional0 hierarchy (default), 1 table

How to use GROUPBY

When GROUPBY returns more than one value, the result spills into the cells below and to the right. Keep those cells empty, or you get #SPILL!.

GROUPBY takes 3 required arguments and 5 optional arguments. Type =GROUPBY( in a cell and SheetsBat shows the arguments as you go. Optional arguments are shown in square brackets; leave them out to use their default.

Examples

Each result below was calculated by the SheetsBat formula engine.

=GROUPBY({"a";"b";"a"}, {1;2;3}, SUM)Result 3 by 2 array
Spilled result
AB
1a4
2b2
3Total6
=GROUPBY({"x";"y";"x"}, {5;7;1}, LAMBDA(v, MAX(v)), 0, 0, -2)Result 2 by 2 array
Spilled result
AB
1y7
2x5

Every formula, right in your browser

SheetsBat calculates 536 functions, with charts, pivot tables and live collaboration. Free to start.