GROUPBY and PIVOTBY: practice exercises
GROUPBY, PIVOTBY, PERCENTOF, sort biggest first, filter, subtotals: summaries as formulas. 8 exercises on real-looking data, each marked the moment you press Check. The first exercise is below to try now; the full lesson is part of Pro.
In F2, use GROUPBY to show the total sales for each branch, with a Total at the bottom.
| A | B | C | D | E | F | G | H | I | J | K | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | Branch | Team | Quarter | Sales (£) | Sales by branch | ||||||
| 2 | Loughborough | Support | Q4 | £40.00 | |||||||
| 3 | Lincoln | Support | Q2 | £75.00 | |||||||
| 4 | Loughborough | Support | Q3 | £50.00 | |||||||
| 5 | Lincoln | Support | Q2 | £205.00 | |||||||
| 6 | Leicester | Finance | Q4 | £300.00 | |||||||
| 7 | Loughborough | Support | Q4 | £170.00 | |||||||
| 8 | Lincoln | Sales | Q2 | £230.00 | |||||||
| 9 | Loughborough | Support | Q2 | £110.00 | |||||||
| 10 | Lincoln | Sales | Q1 | £215.00 | |||||||
| 11 | Leicester | Sales | Q1 | £110.00 | |||||||
| 12 | Loughborough | Sales | Q1 | £160.00 | |||||||
| 13 | Leicester | Sales | Q3 | £305.00 | |||||||
| 14 | Loughborough | Support | Q4 | £135.00 | |||||||
| 15 | Loughborough | Support | Q1 | £300.00 | |||||||
| 16 | Leicester | Finance | Q4 | £250.00 | |||||||
| 17 | Leicester | Sales | Q1 | £45.00 | |||||||
| 18 | Lincoln | Finance | Q2 | £135.00 | |||||||
| 19 | Leicester | Sales | Q1 | £95.00 |
How it works
GROUPBY and PIVOTBY make a summary like a PivotTable's with a formula. You type it in one cell and the answer spills: each branch once, its total, and a Total row at the bottom.
=GROUPBY(A2:A8,C2:C8,SUM): what to group by, what to add up, and the function's name on its own, with no brackets. =PIVOTBY(A2:A8,B2:B8,C2:C8,SUM) puts a second field across the top. They're in Microsoft 365 and Excel for the web only.
=GROUPBY(A2:A8,C2:C8,SUM) =GROUPBY(A2:A8,C2:C8,SUM,,,-2) =GROUPBY(A2:A8,C2:C8,SUM,,,,B2:B8="Q1") =GROUPBY(A2:A8,C2:C8,PERCENTOF) =PIVOTBY(A2:A8,B2:B8,C2:C8,SUM)
Before you start: which Excel?
GROUPBY and PIVOTBY are new. They're in Microsoft 365 (the Excel that updates itself) and Excel for the web. The one-off versions you buy once, such as Excel 2024 or 2021, don't have them, and nor does anything older: they show #NAME? instead.
If your work uses an older Excel, make the same summary with a PivotTable (the first lessons in this section), or list each branch with UNIQUE and total it with SUMIFS. Check before you share a workbook: anyone opening it in an older Excel sees #NAME? too.
GROUPBY: a total for each branch
| A | B | C | D | E | F | G | H | |
|---|---|---|---|---|---|---|---|---|
| 1 | Branch | Quarter | Sales (£) | |||||
| 2 | Nottingham | Q1 | £120.00 | |||||
| 3 | Derby | Q1 | £600.00 | |||||
| 4 | Leicester | Q1 | £300.00 | |||||
| 5 | Nottingham | Q2 | £85.00 | |||||
| 6 | Derby | Q2 | £280.00 | |||||
| 7 | Nottingham | Q2 | £240.00 | |||||
| 8 | Leicester | Q2 | £155.00 |
=GROUPBY(A2:A8,C2:C8,SUM)in E2. Nottingham's three sales are £120 + £85 + £240 = £445. GROUPBY does that for every branch, lists them A to Z, and adds a Total row: £1,780.=GROUPBY(A2:A8,C2:C8,AVERAGE)in E2. The third part is the function, as a name on its own: SUM, not SUM(). Swap it for AVERAGE, COUNT, MAX or MIN. Nottingham's average sale is £445 ÷ 3 = £148.33.
Biggest first, and only some rows
| A | B | C | D | E | F | G | H | |
|---|---|---|---|---|---|---|---|---|
| 1 | Branch | Quarter | Sales (£) | |||||
| 2 | Nottingham | Q1 | £120.00 | |||||
| 3 | Derby | Q1 | £600.00 | |||||
| 4 | Leicester | Q1 | £300.00 | |||||
| 5 | Nottingham | Q2 | £85.00 | |||||
| 6 | Derby | Q2 | £280.00 | |||||
| 7 | Nottingham | Q2 | £240.00 | |||||
| 8 | Leicester | Q2 | £155.00 |
=GROUPBY(A2:A8,C2:C8,SUM,,,-2)in E2. The 6th part sorts. 2 means the 2nd column of the answer (the totals) and the minus puts the biggest first: Derby £880, Leicester £455, Nottingham £445. The two commas in a row skip parts 4 and 5.=GROUPBY(A2:A8,C2:C8,SUM,,,,B2:B8="Q1")in E2. The 7th part keeps only the rows where the test is TRUE, like FILTER. Just Q1: Derby £600, Leicester £300, Nottingham £120.
Shares with PERCENTOF
| A | B | C | D | E | F | G | H | |
|---|---|---|---|---|---|---|---|---|
| 1 | Branch | Quarter | Sales (£) | |||||
| 2 | Nottingham | Q1 | £120.00 | |||||
| 3 | Derby | Q1 | £600.00 | |||||
| 4 | Leicester | Q1 | £300.00 | |||||
| 5 | Nottingham | Q2 | £85.00 | |||||
| 6 | Derby | Q2 | £280.00 | |||||
| 7 | Nottingham | Q2 | £240.00 | |||||
| 8 | Leicester | Q2 | £155.00 |
=GROUPBY(A2:A8,C2:C8,PERCENTOF)in E2. PERCENTOF gives each branch's share of all the sales: Nottingham's £445 out of £1,780 is 25%. The Total is 100%. Format the column as a percentage.
PIVOTBY: a second field across the top
| A | B | C | D | E | F | G | H | |
|---|---|---|---|---|---|---|---|---|
| 1 | Branch | Quarter | Sales (£) | |||||
| 2 | Nottingham | Q1 | £120.00 | |||||
| 3 | Derby | Q1 | £600.00 | |||||
| 4 | Leicester | Q1 | £300.00 | |||||
| 5 | Nottingham | Q2 | £85.00 | |||||
| 6 | Derby | Q2 | £280.00 | |||||
| 7 | Nottingham | Q2 | £240.00 | |||||
| 8 | Leicester | Q2 | £155.00 |
=PIVOTBY(A2:A8,B2:B8,C2:C8,SUM)in E2. The branches go down the side, the quarters across the top, and each sale is added in where they meet. Nottingham in Q2 is £85 + £240 = £325. There's a Total row and a Total column.
Formula or PivotTable?
Both give the same numbers. Pick by what you need:
- A formula (GROUPBY or PIVOTBY) updates by itself when the data changes, with no Refresh. It can feed other formulas, and it's easy to copy into another workbook.
- A PivotTable lets you drag fields about to try different views, filter with slicers, group dates into months and quarters, and draw a PivotChart. It also works in older Excel.
One catch: GROUPBY sorts text A to Z, so months typed as words come out Apr, Aug, Dec…. Use quarters, month numbers or a PivotTable, which knows the calendar order.
What you'll practise
- In F2, use GROUPBY to show the total sales for each branch, with a Total at the bottom.
- In F2, show the average sale for each team.
- In F2, show how many sales each branch made (not how much they came to).
- In F2, show the total sales for each branch with the biggest total first.
- In F2, show the total sales for each branch, counting only the quarter in G1. Use G1, not the quarter typed in, so the summary changes when G1 does.
- In F2, show each branch's share of all the sales, as a percentage, using PERCENTOF.
- In F2, use PIVOTBY to show total sales with the branches down the side and the quarters across the top.
- In F2, show total sales for each team within each branch, with a subtotal for each branch. (Give GROUPBY two columns to group by.)
Guide with worked examples: GROUPBY and PIVOTBY: summaries without a PivotTable