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.
Syntax =GROUPBY(row_fields, values, function, [field_headers], [total_depth], [sort_order], [filter_array], [field_relationship])
=GROUPBY(B2:B10,D2:D10,SUM,0,1)Arguments
| Name | Required | What to pass |
|---|---|---|
| row_fields | Required | The column or columns to group by. Two columns give a second level, which is what subtotals need. |
| values | Required | The numbers to aggregate. One column per value. It must have the same number of rows as row_fields. |
| function | Required | SUM, 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_headers | Optional | 0 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_depth | Optional | Omit 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_order | Optional | Which 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_array | Optional | TRUE 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_relationship | Optional | 0, 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
| A | B | C | D | E | F | G | |
|---|---|---|---|---|---|---|---|
| 1 | Date | State | Channel | Amount | State | Total | |
| 2 | 3/2/2026 | Texas | Retail | 2500 | California | 3800 | |
| 3 | 3/4/2026 | Texas | Retail | 700 | Ohio | 2000 | |
| 4 | 3/6/2026 | California | Retail | 1800 | Texas | 4200 | |
| 5 | 3/9/2026 | California | Retail | 1000 | Total | 10000 | |
| 6 | 3/11/2026 | Ohio | Retail | 1500 | |||
| 7 | 3/14/2026 | California | Web | 1000 | |||
| 8 | 3/18/2026 | Ohio | Web | 200 | |||
| 9 | 3/18/2026 | Ohio | Web | 300 | |||
| 10 | 3/21/2026 | Texas | Web | 1000 |
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.