Function · Dynamic arrays

Excel GROUPBY function

The Excel GROUPBY function sums rows by a field and spills the groups. Nine March 2026 orders total $10,000, with Texas at $4,200.

Microsoft 365 and Excel for the web. Excel 2024, Excel 2021, and Excel 2019 do not have it.

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

GROUPBY
=GROUPBY(B2:B10,D2:D10,SUM,0,1)

Arguments

NameRequiredWhat to pass
row_fieldsRequiredThe column or columns to group by. Two columns give a second level, which is what subtotals need.
valuesRequiredThe numbers to aggregate. One column per value. It must have the same number of rows as row_fields.
functionRequiredSUM, AVERAGE, COUNTA, PERCENTOF, or a LAMBDA that returns one value for the group. Pass the name SUM, with no parentheses. HSTACK(SUM, AVERAGE) returns two result columns.
field_headersOptional0 means the ranges have no headers. 1 means they do, and the result should hide them. 2 generates headers. 3 means the ranges include headers and the result should show them. Omit it and Excel looks at values. Text in the first cell and a number in the second is treated as a header. Headers show on their own when there is more than one group level.
total_depthOptionalOmit it for a grand total, plus subtotals when row_fields has two or more columns. 0 hides totals. 1 is the grand total. 2 adds subtotals. -1 and -2 put those totals at the top. Subtotals with a single column are
sort_orderOptionalWhich result column to sort by, starting at 1 for the first row field. A negative number sorts descending. Omit it and the groups sort A to Z. A list such as {-1, 2} sorts by more than one row field.
filter_arrayOptionalTRUE or FALSE for each row of row_fields, the same height. Rows with FALSE stay out of every group and out of the total.
field_relationshipOptional0, the default, keeps later columns inside the earlier ones, which is what subtotals use. 1 sorts each column on its own. Subtotals need the hierarchy, so 1 does not show them.

Examples

Nine March 2026 orders, summed by state

=GROUPBY(B2:B10,D2:D10,SUM,0,1)

Result California $3,800, Ohio $2,000, Texas $4,200, total $10,000

Average order by channel

=GROUPBY(C2:C10,D2:D10,AVERAGE,0,0)

Result Retail $1,500 and web $625

Each state's share of the $10,000

=GROUPBY(B2:B10,D2:D10,PERCENTOF,0,0)

Result 38%, 20%, and 42%

The gap between the largest and smallest order in each state

=GROUPBY(B2:B10,D2:D10,LAMBDA(orders,MAX(orders)-MIN(orders)),0,0)

Result California $800, Ohio $1,300, Texas $1,800

=GROUPBY(B2:B10,D2:D10,SUM,0,1)F2
Orders
ABCDEFG
1DateStateChannelAmountStateTotal
23/2/2026TexasRetail2500California3800
33/4/2026TexasRetail700Ohio2000
43/6/2026CaliforniaRetail1800Texas4200
53/9/2026CaliforniaRetail1000Total10000
63/11/2026OhioRetail1500
73/14/2026CaliforniaWeb1000
83/18/2026OhioWeb200
93/18/2026OhioWeb300
103/21/2026TexasWeb1000
A picture of the sheet, not a live calculator. F2 holds the formula. G2:G5 is the spill. The dates are March 2026 and are not part of the formula.

GROUPBY spills one row per state. The nine March 2026 orders are not in alphabetical order. The result is. California is $3,800, Ohio is $2,000, Texas is $4,200, and the total is $10,000. field_headers 0 means row 2 is data. The first amount is a number, which is the same guess Excel makes when that argument is left out. total_depth -1 puts the $10,000 row above the states. A filter of C2:C10="Web" leaves California $1,000, Ohio $500, Texas $1,000, and a $2,500 total.

Excel 2024, Excel 2021, and Excel 2019 return #NAME?. A PivotTable still groups the rows. Excel 2021 and Excel 2024 can put UNIQUE beside SUMIFS. The group sum LAMBDA is that pair for one field, and it needs Excel 2024 or Microsoft 365, because Excel 2021 has no LAMBDA. Excel 2019 can use the PivotTable, or SUMIFS beside a list of the states. PIVOTBY adds the second field across the columns. The Sales Commission Calculator totals each rep, the Sales Pipeline Tracker totals each stage with SUMIFS, and the Annual Budget Planner totals the January categories with SUMIFS. All three open in Excel 2010.