How-to recipes · Counting and summing
How to sum by category in Excel
Total amounts per category, product or person with GROUPBY or UNIQUE + SUMIF in Microsoft 365, SUMIF in any version, or a PivotTable.
An expense list has a category on every row: Travel, Software, Meals, Office. At the end of the month you need one total per category, both to check the budget and to fill in a report. Sorting the list and adding with a calculator is slow and breaks the moment a new expense arrives. This recipe shows how to get the totals with one formula in Microsoft 365, the classic SUMIF approach that works everywhere, and the PivotTable route.
The example data
Eight expenses in A1:B9 across four categories. Travel appears three times: 320 + 180 + 95 = 595.
Microsoft 365: one formula for the whole summary
GROUPBY takes the column to group by, the values, and the function to apply:
| A | B | C | D | E | |
|---|---|---|---|---|---|
| 1 | Category | Amount | |||
| 2 | Travel | 320 | Meals | 83 | |
| 3 | Software | 99 | Office | 62 | |
| 4 | Meals | 45 | Software | 148 | |
| 5 | Travel | 180 | Travel | 595 | |
| 6 | Office | 62 | |||
| 7 | Meals | 38 | |||
| 8 | Software | 49 | |||
| 9 | Travel | 95 |
Categories come out sorted A to Z. The final 0,0 hide the header and grand-total rows.
Without the last two arguments GROUPBY adds a Total row underneath. You can sort by the amounts instead with the sort_order argument: =GROUPBY(A2:A9,B2:B9,SUM,0,0,-2) sorts by the second column, largest first.
If you do not have GROUPBY
Older Microsoft 365 builds and Excel 2021 have UNIQUE but not GROUPBY. Combine UNIQUE with SUMIF, which accepts a whole array of criteria and returns one total per item:
=HSTACK(SORT(UNIQUE(A2:A9)),SUMIF(A2:A9,SORT(UNIQUE(A2:A9)),B2:B9))HSTACK places the categories and their totals side by side. In Excel 2021 (no HSTACK), put =SORT(UNIQUE(A2:A9)) in D2 and =SUMIF(A2:A9,D2#,B2:B9) in E2; the D2# reference follows the spilled list as it grows.
Excel 2016 and 2019: SUMIF per category
Type the categories in a column (or copy them with Data > Remove Duplicates on a copy), then put a SUMIF next to the first one and copy it down:
| A | B | C | D | E | |
|---|---|---|---|---|---|
| 1 | Category | Amount | Category | Total | |
| 2 | Travel | 320 | Travel | 595 | |
| 3 | Software | 99 | Software | ||
| 4 | Meals | 45 | Meals | ||
| 5 | Travel | 180 | Office | ||
| 6 | Office | 62 | |||
| 7 | Meals | 38 | |||
| 8 | Software | 49 | |||
| 9 | Travel | 95 |
E2 adds every amount whose category equals Travel. Copy it down to E5 for Software, Meals and Office.
Result: 595
$A$2:$A$9: where to look for the category. The$signs keep the range fixed while copying.D2: the category for this row of the summary (relative, so it moves down).$B$2:$B$9: the numbers to add.
The other totals are Software 148, Meals 83 and Office 62. A quick check: the category totals together must equal the grand total =SUM(B2:B9) (888). If they do not, a category is missing from your list or spelled differently.
More than one condition
Use SUMIFS when the total depends on two things, for example Travel in March: =SUMIFS($B$2:$B$9,$A$2:$A$9,D2,$C$2:$C$9,"March"). Note the different argument order: in SUMIFS the sum range comes first, in SUMIF it comes last.
PivotTable method
- Click inside the data and choose Insert > PivotTable > OK.
- Drag Category to Rows and Amount to Values. The Values field shows Sum of Amount.
- Right-click a number > Sort > Largest to Smallest if you want the biggest category first.
- After adding new rows, right-click the PivotTable and choose Refresh.
PivotTables are best when you want to slice by several fields at once; formulas are best when the totals feed other calculations. See PivotTables: summarise thousands of rows in seconds.
Pitfalls
- Spelling variants.
Travel,travelandTravel(with a trailing space) look like one category but the space creates a separate group. Case does not matter; spaces do. Clean the column with TRIM or use a drop-down list for data entry (How to create a drop-down list in Excel). - Numbers stored as text in the Amount column are ignored by SUMIF and GROUPBY's SUM. If a total looks too low, check with
=ISTEXT(B2). - Wildcards. SUMIF treats
*and?in the criteria as wildcards, so a category namedR&D*would match more than itself. - Range sizes. In SUMIF the criteria range and sum range should be the same size; otherwise Excel silently resizes the sum range, which can add the wrong cells.