GROUPBY
Short answer
A PivotTable in one formula that never needs a refresh
GROUPBY(row_fields, values, function, [field_headers], [total_depth], [sort_order], [filter_array], [field_relationship])
- Category
- Conditional Aggregation
- Excel version
- 365
- Also searched as
- group by formula excel, pivot table formula, summarize without a pivot table, sum by vendor, dynamic trial balance
What it returns
GROUPBY takes a column of labels and a column of numbers, groups the rows by label, and aggregates each group. The result spills: one row per distinct label, the aggregate beside it, and a grand total at the bottom by default.
That is the output of a PivotTable with one field in Rows and one in Values. The difference is that GROUPBY is a formula. Add a row to the source Table and the summary changes on the next recalculation. There is no refresh button, no pivot cache, and no field list for someone to rearrange.
The function argument takes a function name with no parentheses, such as SUM, AVERAGE, COUNTA, MAX or PERCENTOF, or a LAMBDA you write. Groups come back sorted ascending by label unless you say otherwise.
Arguments that matter
| Argument | Required | What it does |
|---|---|---|
row_fields | Yes | The column (or adjacent columns) to group by. AP[Vendor]. Can be a calculated array of the same height. |
values | Yes | The column or columns to aggregate. AP[Amount]. Must be the same height as row_fields. |
function | Yes | The aggregation. SUM, AVERAGE, COUNTA, PERCENTOF, a LAMBDA, or several stacked with HSTACK(SUM, PERCENTOF). |
field_headers | No | 0 no headers, 1 headers present but hidden, 2 no headers but generate them, 3 headers present and shown. Omit it and Excel guesses. |
total_depth | No | 0 no totals, 1 grand total (default), 2 grand total and subtotals. A negative value puts totals at the top. |
sort_order | No | The output column to sort by. 2 sorts by the first values column. Negative sorts descending. |
filter_array | No | A TRUE/FALSE array the same height as the data. Only TRUE rows are grouped. |
field_relationship | No | With several row fields: 0 treats them as a hierarchy (default), 1 as independent columns. |
Three of these carry most of the weight.
sort_order decides whether the summary reads like a report. -2 puts the largest group first, which is what a vendor spend review wants. The grand total stays at the bottom.
filter_array replaces the criteria you would otherwise write into SUMIFS. AP[Entity]=$C$3 limits the summary to one entity, driven from an input cell.
field_headers is worth setting explicitly every time. Table column references carry no header row, so 0 is the right value for them. Leaving it to the guess is how a header ends up treated as a group.
In a financial model
GROUPBY earns its place where the list of groups is not known in advance. A statement grid has fixed line items, and SUMIFS fills it. A vendor list, a customer list or a set of GL accounts changes every month, and a SUMIFS grid would need someone to add the new rows by hand.
The most useful single instance is a self-updating trial balance. One formula on a review tab, grouping the ledger extract by account, spills every account with a balance. The grand total row is a free control: a trial balance sums to zero, so that cell belongs in the check row, as in build a check row and make it loud.
The source stays untouched on its own tab, per raw data stays raw. GROUPBY reads it. Nothing is pasted, so nothing goes stale.
Use it on review tabs. Keep SUMIFS for the reporting grid that other formulas point at, because that grid has to hold its shape.
Worked example
An accounts payable extract on tab AP, formatted as a Table with columns InvoiceDate, Entity, Vendor, CostCenter, Amount. The review tab needs year-to-date spend by vendor for the entity in input cell C3, largest first.
=GROUPBY(AP[Vendor], AP[Amount], SUM, 0, 1, -2, AP[Entity]=$C$3)
In order: group by vendor, sum the amount, no headers, grand total only, sort descending on the second column, and keep the selected entity.
| Vendor | Amount |
|---|---|
| Northwind Logistics | 412,300 |
| Contoso Cloud | 268,950 |
| Fabrikam Facilities | 97,400 |
| Litware Legal | 41,200 |
| Total | 819,850 |
Change C3 to another entity and the list rebuilds, with a different set of vendors and a different length.
Add the share of total by stacking two functions:
=GROUPBY(AP[Vendor], AP[Amount], HSTACK(SUM, PERCENTOF), 0, 1, -2, AP[Entity]=$C$3)
A third column spills with each vendor’s share of spend, and the total row reads 100%.
Old way vs new way
The pre-GROUPBY formula approach needed two spilling formulas side by side:
=UNIQUE(AP[Vendor])
=SUMIFS(AP[Amount], AP[Vendor], A5#)
GROUPBY does it in one:
=GROUPBY(AP[Vendor], AP[Amount], SUM)
The old pair had no total, no sort, and no filter without extra wrapping. GROUPBY carries all three as arguments, and the result cannot drift out of step because there is only one formula.
Where it breaks
GROUPBY returns #NAME?. The file is open in Excel 2021, Excel 2024 or an older version. GROUPBY ships only in Microsoft 365, and the perpetual 2024 release does not include it. Fix: if the model travels to colleagues on perpetual licenses, build the summary with UNIQUE and SUMIFS, or keep a PivotTable for that audience.
GROUPBY returns #SPILL!. Something sits in the range the result needs, or the formula was entered inside an Excel Table. Tables do not allow spills. Fix: put the formula on a clear area of a normal sheet and leave room below it for the list to grow.
GROUPBY returns #VALUE!. row_fields and values are different heights. Mixing A:A with AP[Amount] is the usual cause. Fix: reference Table columns for both, so they grow together.
One row per day instead of one per month. The raw date column went into row_fields, so every posting date is its own group. Fix: group by EOMONTH(AP[InvoiceDate], 0), which buckets each date to its month end.
The same vendor appears twice. Northwind Logistics and Northwind Logistics with a trailing space are two different labels. GROUPBY groups exact text. Fix: clean the vendor name where the data lands, in Power Query, rather than wrapping the formula in TRIM.
A formula pointing into the output shows the wrong vendor. Someone wrote =B7 to pick up one vendor’s spend, then a new vendor arrived and every row shifted. Fix: never reference a spilled row by position. Look it up with XLOOKUP against the spill, or read the source directly with SUMIFS.
Related functions
- PIVOTBY. GROUPBY with a second dimension across the columns. Reach for it when the output needs months across the top.
- PERCENTOF. Pass it as the
functionargument, alone or inHSTACK(SUM, PERCENTOF), to get mix and share of total in the same spill. - SUMIFS. For a fixed reporting grid that other formulas consume. GROUPBY output changes shape, a SUMIFS grid does not.
- UNIQUE. The label list on its own. Useful when you need the distinct accounts without aggregating anything.
- The play build a check row and make it loud is where the trial balance total belongs.
