Ravindra BagaleCourses & study guides मराठी Track your progress

6. Tables, Sorting and Filtering

6.7 SUBTOTAL and AGGREGATE

SUBTOTAL for filtered rows with example — in 30 seconds

What: SUBTOTAL with 109 (sum) or 103 (count) uses only the rows you can see after filtering.

Why: A normal SUM keeps showing the total of all rows, even when the filter shows only Pune. People then quote the wrong number.

Tiny example: Filter City = Pune. SUM still shows 2870. =SUBTOTAL(109,tblOrders[Amount]) shows 544.

First look at the data (before → after):

SUBTOTAL for filtered rows with example: before and after tables, fictional FreshGo Maharashtra data

Expected result — What each formula shows while the filter is on

Formula Shows
=SUM(tblOrders[Amount]) 2870
=SUBTOTAL(109,tblOrders[Amount]) 544
=SUBTOTAL(103,tblOrders[Order ID]) 4

About this data: Teaching sample for a fictional shop “GreenField Dairy”. Figures and names (e.g. Sana Sheikh) are made up for this lesson on subtotal and aggregate.

Before table and formula as text

Before — Filter on City = Pune (4 rows visible)

Order ID City Amount
BLK-1001 Pune 64
AMN-1003 Pune 110
BLK-1006 Pune 150
BLK-1009 Pune 220

About this data: Demo table only — not real customer or company data. Built to practise subtotal and aggregate.

Formula / click path

=SUM(tblOrders[Amount])
=SUBTOTAL(109,tblOrders[Amount])
=SUBTOTAL(103,tblOrders[Order ID])

Do it step by step:

  1. Above the table, type =SUBTOTAL(109,tblOrders[Amount]).
  2. Next to it, type =SUBTOTAL(103,tblOrders[Order ID]) for the visible order count.
  3. Filter the City column to Pune.
  4. The two cells change to 544 and 4.
  5. Clear the filter. They go back to the full totals.

=SUBTOTAL(function_num, ref1, …) – calculates only visible rows after filtering.

function_num (includes manually hidden rows) function_num (ignores manually hidden rows) Function
1 101 AVERAGE
2 102 COUNT
3 103 COUNTA
4 104 MAX
5 105 MIN
9 109 SUM

About this data: Made-up rows so you can copy the pattern for subtotal and aggregate. Currencies may use ₹, £, € or $.

Filtered-out rows are always ignored; the 100-series also ignores rows you hid manually.

=AGGREGATE(function_num, options, ref1, …) – like SUBTOTAL with more functions (19, e.g. 14 = LARGE, 15 = SMALL) and options to ignore errors:

options Ignores
5 hidden rows
6 error values
7 hidden rows and error values

About this data: Fictional worksheet used in class. Safe to edit; nothing here is live production data.

Formula Result
=SUBTOTAL(109,tblOrders[Amount]) Sum of visible orders
=SUBTOTAL(103,tblOrders[Order ID]) Count of visible orders
=AGGREGATE(9,6,D2:D100) Sum ignoring #N/A/#DIV/0!
=AGGREGATE(14,6,tblOrders[Amount],2) 2nd largest amount, ignoring errors

About this data: Teaching sample for a fictional shop “GreenField Dairy”. Figures and names (e.g. Sana Sheikh) are made up for this lesson on subtotal and aggregate.

Worked example – dynamic heading. ="Showing "&SUBTOTAL(103,tblOrders[Order ID])&" orders worth ₹"&TEXT(SUBTOTAL(109,tblOrders[Amount]),"#,##0") updates as the user filters.

Ravindra Bagale's Tip

If you calculate the total of filtered data with =SUM(), the hidden rows are included too – many students report the wrong total. If there is a filter, use SUBTOTAL(109,…). And if the column has errors, SUM gives #N/A – use AGGREGATE(9,6,…).

Practice task

Build a filter-aware summary box above tblOrders: visible order count, visible sales and visible average delivery time. Add an AGGREGATE total for a column that contains errors.

Loading practice tasks...

  1. Total cost (Easy): Total of E2:E8.
  2. Average weight (Easy): Average of D2:D8.
  3. Heaviest (Easy): Biggest weight in D2:D8.
  4. Lightest (Easy): Smallest weight in D2:D8.
  5. Count costs (numbers) (Medium): Count numbers in E2:E8.
  6. Count all cells (Medium): Count filled cells in E2:E8.
  7. Pending costs (Medium): How many costs are not numbers?
  8. Average cost (Medium): Average of E2:E8.
  9. Cost per kg (Hard): Visible total of E2 to E8 divided by (visible total of D2 to D8 minus D3).
  10. Hidden-row version (Hard): Count shipments in A2:A8 with code 103.
  11. Gap between heaviest and lightest (Hard): Heaviest minus lightest in D2:D8 with 104 and 105.

Quick revision cards

Three standalone cards. Each one explains the idea with its own example. Scan the QR to open this lesson.

Revision card 1: What is Subtotal And Aggregate?
Card 1: What is Subtotal And Aggregate? · Download PNG
Revision card 2: Tiny example — Subtotal And Aggregate
Card 2: Tiny example — Subtotal And Aggregate · Download PNG
Revision card 3: Checklist — Subtotal And Aggregate
Card 3: Checklist — Subtotal And Aggregate · Download PNG