PIVOTBY in Excel: a two-way summary in one formula
PIVOTBY summarises a table by rows and columns in one formula that updates by itself. How to build one, add totals, and when a PivotTable is still better.
PIVOTBY builds a two-way summary from one formula. One field goes down the side, another goes across the top, and each cell holds a total. When the data changes, the summary changes too.
PIVOTBY is in Microsoft 365 only. Excel 2021 and Excel 2024 do not have it. There, the formula
shows #NAME?.
A gym has two branches, Harbour and Park. The booking system exports one row per class session. The manager wants bookings by class and by branch.
| Row | A | B | C |
|---|---|---|---|
| 1 | Gym bookings | ||
| 2 | Class | Branch | Bookings |
| 3 | Pilates | Harbour | 18 |
| 4 | Spin | Harbour | 24 |
| 5 | Pilates | Park | 12 |
| 6 | Yoga | Harbour | 20 |
| 7 | Spin | Park | 30 |
| 8 | Yoga | Park | 15 |
| 9 | Pilates | Harbour | 16 |
| 10 | Spin | Park | 22 |
| 11 | Yoga | Harbour | 11 |
Build it
In E3, type =PIVOTBY(A3:A11,B3:B11,C3:C11,SUM). It fills E3 to
H7:
| Harbour | Park | Total | |
| Pilates | 34 | 12 | 46 |
| Spin | 24 | 52 | 76 |
| Yoga | 31 | 15 | 46 |
| Total | 89 | 79 | 168 |
The formula has 4 parts, in this order:
- A3:A11 goes down the side. Each class gets a row.
- B3:B11 goes across the top. Each branch gets a column.
- C3:C11 is the numbers to add.
- SUM is how to add them. Type the name only, with no brackets and no quote marks.
The top-left cell, E3, stays empty. It sits above the class names and left of the branch names, so it names neither.
Check it before you send it
Two quick checks catch most mistakes. First, the corner. H7 shows 168.
=SUM(C3:C11) also gives 168, so no booking was lost.
Second, check one cell a different way. Spin at Park shows 52.
=SUMIFS(C3:C11,A3:A11,"Spin",B3:B11,"Park") also gives 52. That
is 30 plus 22, the two Spin sessions at Park.
Turn the totals off
Both kinds of total are on unless you turn them off. The 6th part controls the Total row. The 8th part controls the Total column. A 0 turns each one off.
=PIVOTBY(A3:A11,B3:B11,C3:C11,SUM,,0,,0) fills E3 to G6:
| Harbour | Park | |
| Pilates | 34 | 12 |
| Spin | 24 | 52 |
| Yoga | 31 | 15 |
Each pair of commas skips a part you do not need. Count them carefully. One comma too few, and the 0 lands in the wrong part.
To keep one kind, leave out its 0. =PIVOTBY(A3:A11,B3:B11,C3:C11,SUM,,0)
has a Total column but no Total row.
Count instead of add
Change SUM to COUNT, and the table counts sessions instead of bookings.
=PIVOTBY(A3:A11,B3:B11,C3:C11,COUNT) fills E3 to H7:
| Harbour | Park | Total | |
| Pilates | 2 | 1 | 3 |
| Spin | 1 | 2 | 3 |
| Yoga | 2 | 1 | 3 |
| Total | 5 | 4 | 9 |
The corner is 9, one for each row of data. MAX and AVERAGE work the same way. With MAX, Spin at Park shows 30, its busiest session.
Turn it on its side
Swap the first two parts, and the branches go down the side instead.
=PIVOTBY(B3:B11,A3:A11,C3:C11,SUM) fills E3 to I6:
| Pilates | Spin | Yoga | Total | |
| Harbour | 34 | 24 | 31 | 89 |
| Park | 12 | 52 | 15 | 79 |
| Total | 46 | 76 | 46 | 168 |
The numbers are the same. Pick the layout that fits the page: fewer columns is easier to read.
When a PivotTable is still better
| PIVOTBY | PivotTable | |
|---|---|---|
| Updates when data changes | Yes | After Refresh |
| Works in Excel 2021 or 2024 | No | Yes |
| Try a new layout by dragging | No | Yes |
| Groups dates into months | No | Yes |
| Other formulas can read it | Yes | Harder |
Use a PivotTable when the file goes to somebody without Microsoft 365. Also use one to look through a new export. You can drag fields in and out and try a new layout in seconds.
A PivotTable is also better with dates. It can group a date column into months or years for you. PIVOTBY needs a month column first.
Use PIVOTBY for a report that must stay right when nobody clicks Refresh. For a table with only rows, see the GROUPBY article.
What goes wrong
Something is in the way. The table needs empty cells below and to the right.
One value in that space turns the whole result into #SPILL!. A
new class makes the table one row taller, so leave room.
A formula reads one cell by its position.
=INDEX(PIVOTBY(A3:A11,B3:B11,C3:C11,SUM),3,3) gives 52 today. Add
a new class or branch and the rows or columns can move. Then the same formula reads a different
cell. Read one number by name with SUMIFS instead.
New rows are outside the range. A session typed in row 12 is not in A3:A11. Make the data an Excel table with Ctrl+T, and use its columns in the formula.
PIVOTBY has more optional parts, for sorting and filtering. You can build most reports without them.
The same table without PIVOTBY
If the file goes to Excel 2021, build the table with SUMIFS. Type the class names in E4 to E6. Type Harbour in F3 and Park in G3. Then, in F4, type:
=SUMIFS($C$3:$C$11,$A$3:$A$11,$E4,$B$3:$B$11,F$3)
Fill it right to G4, then down to row 6. F4 gives 34 and G5 gives 52, the same as PIVOTBY.
$E4 always reads column E. F$3
always reads row 3. The
SUMIFS exercises explain the argument
order.
The cost is the list of names. A new class in the data does not appear in column E by itself.
Check that the table adds up to the same total as =SUM(C3:C11).
You can practise PIVOTBY and GROUPBY in the lessons below. Each lesson is a real sheet you type into, and each answer is checked when you press Enter.