GROUPBY builds a summary table with a formula. It groups the rows of your data by a field, such as region, applies a function to the values in each group, sorts the groups, adds a total, and spills the result. It does what a simple PivotTable does, but live, with no refresh and no pivot cache.
The arguments
- row_fields is what to group by. Each distinct value becomes a row in the result.
- values is what to summarise. It must be the same height as
row_fields. - function is the calculation applied to each group. Name it without brackets:
SUM, AVERAGE, COUNT, MAX, MIN. A LAMBDA works too, for custom calculations.
=GROUPBY(A2:A9,B2:B9,SUM) totals sales for each region. The result has the regions in the first column, in alphabetical order, the totals in the second column, and a grand total row at the bottom.
The optional arguments
- field_headers tells GROUPBY whether your ranges include a header row. Include the headers in the ranges and set this to
3 to show them in the result. - total_depth controls the totals.
0 removes them. A negative number moves them above the data. - sort_order is a column number.
-2 sorts by the second column of the result, descending, which ranks the groups by their value. - filter_array limits the rows that are counted:
B2:B9>500 leaves out any sale of 500 or less before the grouping.
Compared with a PivotTable
A PivotTable is a separate object that you refresh, and its layout is edited with a field list. GROUPBY is an ordinary formula that recalculates with your data, can sit inside other formulas, and can be sorted, filtered and formatted like any other result. A PivotTable is still the better choice for exploratory work with many fields, drag-and-drop layout and slicers. GROUPBY suits a report that has to update itself.
For a two-way summary, with one field down the side and another across the top, use PIVOTBY.
Without GROUPBY
If you need to share a workbook with people on older versions, build the same summary from functions that work everywhere: list the groups with SORT(UNIQUE(A2:A9)), and total each with SUMIFS. The exercise below does exactly that. See UNIQUE and SUMIFS.
Notes
The result spills, so #SPILL! means the cells it needs aren't empty. GROUPBY is a recent addition and is not in every Microsoft 365 build yet. If it returns #NAME?, your version does not have it. The examples below show the results Excel returns, and the exercise builds the same summary with UNIQUE and SUMIFS.