GROUPBY and PIVOTBY: summaries without a PivotTable
=GROUPBY(A2:A8,C2:C8,SUM) lists each branch once, with its total sales and a Total at the bottom. GROUPBY and PIVOTBY are only in Excel for Microsoft 365 (Windows and Mac) and Excel for the web: Excel 2024, 2021 and older don't have them. =PIVOTBY(A2:A8,B2:B8,C2:C8,SUM) adds a second field across the top, like a PivotTable. Both are formulas, so they spill from one cell and update by themselves.
The formulas at a glance
| You want | Formula | What you get |
|---|---|---|
| A total for each group | =GROUPBY(A2:A8,C2:C8,SUM) | Each branch once, its total, and a Total row |
| Biggest first | =GROUPBY(A2:A8,C2:C8,SUM,,,-2) | The same, sorted by the totals |
| Only some rows | =GROUPBY(A2:A8,C2:C8,SUM,,,,B2:B8="Q1") | Totals for the first quarter only |
| Each group's share | =GROUPBY(A2:A8,C2:C8,PERCENTOF) | Each branch as a % of all sales |
| Average or count | =GROUPBY(A2:A8,C2:C8,COUNT) | How many sales each branch made |
| Rows and columns | =PIVOTBY(A2:A8,B2:B8,C2:C8,SUM) | Branches down the side, quarters across the top |
Every example below uses the same seven sales from three branches.
GROUPBY: a total for each group
Three branches have made seven sales between them. Nottingham's three sales are £120, £85 and £240, so its total is 120 + 85 + 240 = £445. Doing that by hand for every branch is slow, and has to be done again when a sale is added.
GROUPBY takes three parts: what to group by, what to add up, and the function to use.
=GROUPBY(A2:A8,C2:C8,SUM)
| A | B | C | D | E | F | |
|---|---|---|---|---|---|---|
| 1 | Branch | Quarter | Sales (£) | =GROUPBY(A2:A8,C2:C8,SUM) | £880.00 | |
| 2 | Nottingham | Q1 | £120.00 | Leicester | £455.00 | |
| 3 | Derby | Q1 | £600.00 | Nottingham | £445.00 | |
| 4 | Leicester | Q1 | £300.00 | Total | £1,780.00 | |
| 5 | Nottingham | Q2 | £85.00 | |||
| 6 | Derby | Q2 | £280.00 | |||
| 7 | Nottingham | Q2 | £240.00 | |||
| 8 | Leicester | Q2 | £155.00 |
The function is a name on its own: SUM, with no brackets after it. Swap it for AVERAGE, COUNT, MAX, MIN or MEDIAN. =GROUPBY(A2:A8,C2:C8,AVERAGE) gives Nottingham's average sale: £445 ÷ 3 = £148.33. =GROUPBY(A2:A8,C2:C8,COUNT) gives how many sales each branch made: Derby 2, Leicester 2, Nottingham 3.
Start the ranges at row 2, under the headings. If you start at row 1, GROUPBY spots the headings and leaves them out of the sums. Put a 3 in the 4th part, =GROUPBY(A1:A8,C1:C8,SUM,3), and it shows those headings above the answer too.
Biggest first
The 6th part sorts. Give it the column of the answer to sort by, with a minus sign for biggest first. The totals are the 2nd column, so:
=GROUPBY(A2:A8,C2:C8,SUM,,,-2)
| A | B | C | D | E | F | |
|---|---|---|---|---|---|---|
| 1 | Branch | Quarter | Sales (£) | Biggest first | ||
| 2 | Nottingham | Q1 | £120.00 | Derby | £880.00 | |
| 3 | Derby | Q1 | £600.00 | Leicester | £455.00 | |
| 4 | Leicester | Q1 | £300.00 | Nottingham | £445.00 | |
| 5 | Nottingham | Q2 | £85.00 | Total | £1,780.00 | |
| 6 | Derby | Q2 | £280.00 | |||
| 7 | Nottingham | Q2 | £240.00 | |||
| 8 | Leicester | Q2 | £155.00 |
That puts Derby (£880) first, then Leicester (£455), then Nottingham (£445). The empty parts between the commas are left at their usual settings: part 4 is the headings and part 5 the totals. Use 1 to sort by the branch names A to Z, which is what it does anyway, or -1 for Z to A.
Only some rows
The 7th part is a test, written as you would for FILTER (see UNIQUE and FILTER). Only the rows where it's TRUE are counted. In the first quarter Derby sold £600, Leicester £300 and Nottingham £120:
=GROUPBY(A2:A8,C2:C8,SUM,,,,B2:B8="Q1")
| A | B | C | D | E | F | |
|---|---|---|---|---|---|---|
| 1 | Branch | Quarter | Sales (£) | Q1 only | ||
| 2 | Nottingham | Q1 | £120.00 | Derby | £600.00 | |
| 3 | Derby | Q1 | £600.00 | Leicester | £300.00 | |
| 4 | Leicester | Q1 | £300.00 | Nottingham | £120.00 | |
| 5 | Nottingham | Q2 | £85.00 | Total | £1,020.00 | |
| 6 | Derby | Q2 | £280.00 | |||
| 7 | Nottingham | Q2 | £240.00 | |||
| 8 | Leicester | Q2 | £155.00 |
Better still, put Q1 in a cell such as H1 and use B2:B8=H1, so you can change the quarter without touching the formula. For two tests at once, multiply them in brackets: (B2:B8="Q1")*(C2:C8>100).
Each group's share with PERCENTOF
PERCENTOF gives each group's share of the whole. Nottingham's £445 out of £1,780 is a quarter, 25%:
=GROUPBY(A2:A8,C2:C8,PERCENTOF)
| A | B | C | D | E | F | |
|---|---|---|---|---|---|---|
| 1 | Branch | Quarter | Sales (£) | Share | ||
| 2 | Nottingham | Q1 | £120.00 | Derby | 49.4% | |
| 3 | Derby | Q1 | £600.00 | Leicester | 25.6% | |
| 4 | Leicester | Q1 | £300.00 | Nottingham | 25% | |
| 5 | Nottingham | Q2 | £85.00 | Total | 100% | |
| 6 | Derby | Q2 | £280.00 | |||
| 7 | Nottingham | Q2 | £240.00 | |||
| 8 | Leicester | Q2 | £155.00 |
Format the answer column as a percentage, or the shares show as decimals such as 0.25.
PIVOTBY: rows and columns
PIVOTBY takes what goes down the side, then what goes across the top, then the values and the function:
=PIVOTBY(A2:A8,B2:B8,C2:C8,SUM)
| A | B | C | D | E | F | G | H | |
|---|---|---|---|---|---|---|---|---|
| 1 | Branch | Quarter | Sales (£) | =PIVOTBY(A2:A8,B2:B8,C2:C8,SUM) | Q1 | Q2 | Total | |
| 2 | Nottingham | Q1 | £120.00 | Derby | £600.00 | £280.00 | £880.00 | |
| 3 | Derby | Q1 | £600.00 | Leicester | £300.00 | £155.00 | £455.00 | |
| 4 | Leicester | Q1 | £300.00 | Nottingham | £120.00 | £325.00 | £445.00 | |
| 5 | Nottingham | Q2 | £85.00 | Total | £1,020.00 | £760.00 | £1,780.00 | |
| 6 | Derby | Q2 | £280.00 | |||||
| 7 | Nottingham | Q2 | £240.00 | |||||
| 8 | Leicester | Q2 | £155.00 |
There's a Total column on the right and a Total row at the bottom. Where a branch made no sales in a quarter, the cell is left empty. PIVOTBY takes the same tests, sorting and PERCENTOF as GROUPBY, but in different places, because it has more parts: Excel's formula tips show which part is which as you type.
Formula or PivotTable?
Both give the same numbers. A formula updates by itself when the data changes, with no Refresh, and its answer can go straight into another formula, such as SORT or a chart. A PivotTable is better for trying different views by dragging fields about, for slicers, for grouping dates into months, and for anyone on an older Excel.
Our PivotTables section covers the PivotTable side step by step. To practise the formulas, try GROUPBY and PIVOTBY practice; for the PivotTable itself, PivotTable practice. In Excel for Mac, PivotTables use the same Insert › PivotTable button and the Pivot Table Analyse tab.
Common mistakes
Brackets after the function
=GROUPBY(A2:A8,C2:C8,SUM()) won't work. Give the name on its own: SUM.
Months in a strange order
GROUPBY sorts text A to Z, so months typed as words come out Apr, Aug, Dec and so on. Use month numbers, quarters, or a PivotTable, which knows the calendar order.
The Total row has gone
The 5th part sets the totals: 0 means none, 1 a Total at the bottom, and -1 at the top. Leave it empty to get the Total.
Grouping by the wrong column
The first part is what each row of the answer is named after. =GROUPBY(B2:B8,C2:C8,SUM) gives a total for each quarter, Q1 £1,020 and Q2 £760, because B holds the quarters.
#NAME? on a colleague's computer
Their Excel doesn't have GROUPBY. Send them a PivotTable instead, or use UNIQUE with SUMIFS (see SUMIFS): put =UNIQUE(A2:A8) in E2 to list the branches, and =SUMIFS(C2:C8,A2:A8,E2#) in F2 to add up each one.
#SPILL!
The summary needs empty cells below and to the right. Clear anything in the way (see the #SPILL! error).
Try it now
In E2, use GROUPBY to show the total cost for each department, with a Total at the bottom.
| A | B | C | D | E | F | |
|---|---|---|---|---|---|---|
| 1 | Department | Expense | Cost (£) | Total by department | ||
| 2 | Science | Lab kit | £240.00 | |||
| 3 | English | Books | £180.00 | |||
| 4 | Maths | Calculators | £150.00 | |||
| 5 | Science | Microscope slides | £65.00 | |||
| 6 | English | Theatre trip | £420.00 | |||
| 7 | Maths | Graph paper | £35.00 | |||
| 8 | Science | Safety goggles | £90.00 | |||
| 9 | Maths | Software | £120.00 | |||
| 10 | ||||||
| 11 |
Open this in Try a formula: the same sheet with the answer filled in, to change or share by link.
Part of PivotTable practice: every lesson on the topic, with challenges in Excel.
Where it works
GROUPBY, PIVOTBY and PERCENTOF are in Excel for Microsoft 365 on Windows and Mac, and in Excel for the web. They aren't in Excel 2024, 2021 or older, which show #NAME?. If you have Microsoft 365 and don't see them, update Excel.
Questions
Why don't I have GROUPBY in Excel?
GROUPBY and PIVOTBY are only in Excel for Microsoft 365, on Windows and Mac, and in Excel for the web. Excel 2024, 2021 and older don't have them, and show #NAME?. If you pay for Microsoft 365 and still can't see them, update Excel. On a work computer, your IT team may hold updates back. A PivotTable, or UNIQUE with SUMIFS, gives the same totals in older versions.
How do I sort GROUPBY by the total?
Use the 6th part, with the column of the answer to sort by. The totals are the 2nd column, so =GROUPBY(A2:A8,C2:C8,SUM,,,-2) sorts them biggest first, and 2 sorts them smallest first. Leave the 4th and 5th parts empty, with just their commas, to keep the usual headings and Total row. The Total row stays at the bottom however you sort.
How do I count with GROUPBY?
Use COUNTA as the function to count the rows in each group: =GROUPBY(A2:A8,A2:A8,COUNTA) counts how many times each branch appears. COUNT works too when the values are numbers, such as sales. Give the function name on its own, with no brackets after it. For a count of different values in each group, such as different customers, PIVOTBY or a PivotTable is clearer.
Should I use GROUPBY or a PivotTable?
Use GROUPBY when the summary has to keep up by itself, or feed another formula, and everyone who opens the file has Microsoft 365. Use a PivotTable when you want to try different layouts by dragging fields, add slicers, group dates into months, or share the file with people on Excel 2024 or older. A PivotTable needs Refresh after the data changes; GROUPBY doesn't.