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.

Beginner SUMIF SUMIFS GROUPBY UNIQUE HSTACK SORT

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:

D2fx=GROUPBY(A2:A9,B2:B9,SUM,0,0)
ABCDE
1CategoryAmount
2Travel320Meals83
3Software99Office62
4Meals45Software148
5Travel180Travel595
6Office62
7Meals38
8Software49
9Travel95

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:

Formula
=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:

E2fx=SUMIF($A$2:$A$9,D2,$B$2:$B$9)
ABCDE
1CategoryAmountCategoryTotal
2Travel320Travel595
3Software99Software
4Meals45Meals
5Travel180Office
6Office62
7Meals38
8Software49
9Travel95

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

  1. Click inside the data and choose Insert > PivotTable > OK.
  2. Drag Category to Rows and Amount to Values. The Values field shows Sum of Amount.
  3. Right-click a number > Sort > Largest to Smallest if you want the biggest category first.
  4. 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, travel and Travel (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 named R&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.
Tip. For totals per month instead of per category, see How to sum values by month in Excel. For a percentage share of each category, see How to calculate percentage of total in Excel. Conditional sums are covered fully in Counting and summing with criteria: COUNTIFS, SUMIFS, SUMPRODUCT.
⬇Practice workbookExamples with live formulas + practice sheets · .xlsx · 15 KB

More how-to recipes