Skip to content

GROUPBY

Groups rows by one or more fields and summarises the values with a function such as SUM. It is a pivot table built from one formula.

Syntax

=GROUPBY(, , , , , , , )

Dynamic arraysStatic examples

Excel for Microsoft 365 only, in recent builds. Not available in Excel 2024, Excel 2021 or earlier, where the formula returns #NAME?.

How GROUPBY works

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.

Arguments

row_fields
The column (or columns) to group by, such as A2:A9. Each distinct value becomes one row of the result.
values
The numbers to summarise, such as B2:B9. It must have the same number of rows as row_fields. Give several columns to summarise each one.
function
The calculation to apply to each group. Give a function name without brackets, such as SUM, AVERAGE, COUNT, MAX or MIN, or a LAMBDA.
field_headersoptional
Whether the ranges you passed include header rows. 0 no, 1 yes but do not show them, 2 no but generate them, 3 yes and show them. Leave it out and Excel decides from the data.
total_depthoptional
Which totals to add. 0 none, 1 a grand total, 2 a grand total and subtotals. A negative number puts the totals above the data instead of below. Left out, GROUPBY adds a grand total, and subtotals when there are several row fields.
sort_orderoptional
How to sort the groups. Give the number of the column to sort by, counting the row fields first and then the values. A negative number sorts descending, so -2 sorts by the first value column, largest first. The default sorts by the first row field, ascending.
filter_arrayoptional
A column of TRUE and FALSE values, one per row of data. Only the rows with TRUE are included in the groups.
field_relationshipoptional
How multiple row fields relate. 0, the default, treats them as a hierarchy, and 1 as a table.

Examples

Example 1

Total sales for each region

D2 =GROUPBY(A2:A9,B2:B9,SUM)

Result: East, 1840; North, 2820; South, 1210; West, 2100; Total, 7970

Groups come back in alphabetical order, with a grand total row added below. Excel decides there are no headers because the first value is a number.

Shown as a static example: our practice sheet doesn't calculate this function yet.

Example 1: Total sales for each region
RowABCDEF
1RegionSales
2North1,200East1840
3South450North2820
4North980South1210
5East1,530West2100
6South760Total7970
7North640
8East310
9West2,100
10

Example 2

Rank the regions: largest first, no total

D2 =GROUPBY(A2:A9,B2:B9,SUM,0,0,-2)

Result: North, 2820; West, 2100; East, 1840; South, 1210

The 0s switch off headers and totals. The -2 sorts by the second column, the totals, in descending order.

Shown as a static example: our practice sheet doesn't calculate this function yet.

Example 2: Rank the regions: largest first, no total
RowABCDEF
1RegionSales
2North1200North2820
3South450West2100
4North980East1840
5East1530South1210
6South760
7North640
8East310
9West2100
10

Example 3

A different function: the average sale per region

D2 =GROUPBY(A2:A9,B2:B9,AVERAGE,0,0)

Result: East, 920; North, 940; South, 605; West, 2100

The function argument decides the calculation. North has three sales totalling 2,820, so the average is 940.

Shown as a static example: our practice sheet doesn't calculate this function yet.

Example 3: A different function: the average sale per region
RowABCDEF
1RegionSales
2North1200East920
3South450North940
4North980South605
5East1530West2100
6South760
7North640
8East310
9West2100
10

Example 4

Leave out small sales with a filter

D2 =GROUPBY(A2:A9,B2:B9,SUM,0,0,1,B2:B9>500)

Result: East, 1530; North, 2820; South, 760; West, 2100

Only rows where the sale is above 500 are grouped. That drops South's 450 and East's 310. The sort order of 1 is the default, ascending by region.

Shown as a static example: our practice sheet doesn't calculate this function yet.

Example 4: Leave out small sales with a filter
RowABCDEF
1RegionSales
2North1200East1530
3South450North2820
4North980South760
5East1530West2100
6South760
7North640
8East310
9West2100
10

Your turn.

Solve it in the sheet. Every answer is checked, with hints if you need them.

GROUPBY needs a recent Microsoft 365 build, so first build its parts with functions that work everywhere. In D2, list each region once, in alphabetical order.

=SORT(UNIQUE(A2:A9))

In E2, return the total sales for each region, in the same alphabetical order, with a single SUMIFS formula that takes the region list from the data.

=SUMIFS(B2:B9,A2:A9,SORT(UNIQUE(A2:A9)))

In G2, return the region list and its totals side by side as one two-column result, using LET to name the region list once.

=LET(r,SORT(UNIQUE(A2:A9)),HSTACK(r,SUMIFS(B2:B9,A2:A9,r)))

Common mistakes

  • The function is a name, not a call. Write SUM, not SUM(B2:B9) or SUM(). If you pass a calculated result instead of a function, the formula is invalid.
  • #NAME? means your version of Excel does not have GROUPBY. It is only in recent Microsoft 365 builds. Use UNIQUE and SUMIFS in files that have to open everywhere.
  • Include header rows in both row_fields and values or in neither. If one has a header and the other does not, the sizes differ and the result is an error.
  • Row fields and values must have the same number of rows, such as A2:A9 and B2:B9. A range that is a row too short or long returns #VALUE!.
  • Sorting by a column number counts the row fields first. With one row field and one value, the value is column 2, so -2 sorts by it, descending.
  • #SPILL! means something is in the cells the result needs.

Questions people ask

What does GROUPBY do in Excel?

It groups rows of data by a field, such as region, and summarises the values in each group with a function such as SUM, then returns the result as a table that updates with the data.

What is the difference between GROUPBY and a PivotTable?

A PivotTable is a separate object that you refresh and lay out with a field list. GROUPBY is a formula, so it recalculates automatically, can be used inside other formulas and spills like any dynamic array.

Why does GROUPBY return #NAME?

Your version of Excel does not have it. GROUPBY is only available in recent Microsoft 365 builds. In Excel 2024 and earlier use UNIQUE with SUMIFS, or a PivotTable.

How do I sort GROUPBY results by the totals?

Use a negative column number in the sort_order argument. With one row field and one value column, `-2` sorts by the values, largest first: `=GROUPBY(A2:A9,B2:B9,SUM,0,0,-2)`.

How do I group by two columns?

Give row_fields a range of two columns, such as `A2:B9`. The result shows both fields, and by default it adds subtotals for the first.

Learn GROUPBY in a real sheet.

Tracks take you from the basics to modelling, one graded exercise at a time.