Sub topic 10
Add KPI cells that follow the slicers
A KPI cell is a single number at the top of the dashboard, such as total revenue or order count. A slicer filters the source data, and a plain SUM ignores that filter. In this chapter you build KPI cells that recalculate when a viewer changes a slicer.
This chapter assumes you can enter formulas such as SUM, and that you have a flat Data sheet like the one in the earlier chapters. It also assumes at least one slicer on the dashboard tab whose data range is that table. Columns are Order ID, Order Date, Region, Category, Units, and Revenue, in A through F.
Why a plain SUM does not follow a slicer
A slicer applied to a data range hides the rows that do not match. SUM adds every cell in its range, hidden or not. If you put =SUM(Data!F2:F1000) on the dashboard, the result stays the same whatever the viewer selects.
SUBTOTAL is the function that skips hidden rows. It takes a function code first and then a range:
=SUBTOTAL(function_code, range)The codes come in two families.
| Code | Function | Hidden rows |
|---|---|---|
| 9 | SUM | Skips rows hidden by a filter, but still counts rows you hid by hand |
| 109 | SUM | Skips all hidden rows, filtered or hidden by hand |
| 3 | COUNTA | Skips filtered rows, but counts rows hidden by hand |
| 103 | COUNTA | Skips all hidden rows |
| 101 | AVERAGE | Skips all hidden rows |
| 104 | MAX | Skips all hidden rows |
| 105 | MIN | Skips all hidden rows |
Use the 100-series codes (101 to 111) for dashboards. They give the same answer whether a row is hidden by a slicer or by hand, so the KPI cell is never inconsistent with what the viewer sees.
Build the core KPI cells
Put each KPI in its own cell, with a label above or beside it. Start with total revenue and order count:
Total revenue (visible rows):
=SUBTOTAL(109, Data!F2:F1000)
Order count (visible rows):
=SUBTOTAL(103, Data!A2:A1000)109 sums only visible Revenue values. 103 counts the visible non-empty Order ID cells. Because each row is one order, that count is the number of visible orders. Count a column that is never blank, such as Order ID. If you count a column with gaps, the KPI undercounts.
Add a units KPI the same way:
Units sold (visible rows):
=SUBTOTAL(109, Data!E2:E1000)Derived KPIs
A derived KPI divides or combines other KPIs. Average order value is visible revenue divided by visible orders:
=IFERROR(SUBTOTAL(109, Data!F2:F1000) / SUBTOTAL(103, Data!A2:A1000), 0)If the viewer picks a combination with no rows, the count is 0 and the division fails with #DIV/0!. IFERROR replaces the error with 0.
IFERROR also hides every other error, including a typo in a sheet name. A stricter version tests only the case you expect:
=IF(SUBTOTAL(103, Data!A2:A1000) = 0, 0, SUBTOTAL(109, Data!F2:F1000) / SUBTOTAL(103, Data!A2:A1000))With this version a real error such as #REF! still shows up. During development, prefer it over IFERROR.
A share-of-total KPI compares the visible rows to the whole table. The unfiltered total uses SUM, because here you want hidden rows included:
=IF(SUM(Data!F2:F1000) = 0, 0, SUBTOTAL(109, Data!F2:F1000) / SUM(Data!F2:F1000))Format the result as a percent. When no slicer is active, it shows 100%. When the viewer selects West, it shows West's share of all revenue.
How the pieces connect
The chain from a click to a changed KPI has four steps. The KPI formula never reads the slicer. It only reads the visibility of rows.
This explains why the slicer's data range matters. SUBTOTAL follows the slicer only if the slicer filters the same range the formula reads. If the slicer filters a different table, no rows in your formula's range are hidden, and the KPI never moves.
Choose the range size on purpose
The examples use Data!F2:F1000. This range starts below the header, so the header text is never included. It stops at row 1000, so rows added past that point are missed. You have three options:
- Fixed generous range, such as
F2:F5000. This is simple, and blank cells cost nothing inSUBTOTAL. Pick a limit above any realistic row count and document it. - Open-ended range, such as
Data!F2:F. It grows with the data and needs no maintenance. If the sheet has a header row and nothing else below the table, this is the easiest option. - Whole column including the header, such as
Data!F:F. Avoid this.SUBTOTAL(109, ...)ignores the header text, butSUBTOTAL(103, Data!A:A)counts the header as a non-empty cell and returns one too many.
Whichever you choose, use the same range in every KPI. Mixed ranges produce quiet mismatches, such as revenue that includes row 1001 while the count does not.
Things SUBTOTAL does not follow
Some dashboard numbers will not respond to the slicer. Knowing which ones saves debugging time.
QUERY and other value-reading formulas
A QUERY formula reads cell values and does not care whether rows are hidden. This summary builds a revenue-by-region table that can feed a chart:
=QUERY(Data!A1:F, "select C, sum(F) where C is not null group by C label sum(F) 'Revenue'", 1)It always shows all regions. Slicers hide rows, but QUERY still reads them. Use QUERY for summaries that should stay fixed, such as an all-time comparison next to the filtered KPIs. Use a pivot table for summaries that should respond to slicers.
Slicers attached to a pivot table
A slicer whose data range is a pivot table filters the pivot table's output. It does not hide rows in Data. A SUBTOTAL over Data stays unchanged. To make a KPI follow that kind of slicer, point the formula at the pivot table's output cells instead. Pivot output cells are hidden or shown by the slicer, so SUBTOTAL can read them. Leave out the pivot table's Grand Total row, or SUBTOTAL will double count.
Text that looks like a number
If Revenue values were pasted as text, shown left-aligned, SUBTOTAL(109, ...) treats them as zero. Convert the column to a number format, or re-enter the values, before you build KPIs on it.
A KPI block you can reuse
Lay the KPIs in a row near the top of the dashboard, with labels in row 2 and values in row 3:
A2: Revenue B2: Orders C2: Avg order value D2: Share of total
A3: =SUBTOTAL(109, Data!F2:F5000)
B3: =SUBTOTAL(103, Data!A2:A5000)
C3: =IF(B3 = 0, 0, A3 / B3)
D3: =IF(SUM(Data!F2:F5000) = 0, 0, A3 / SUM(Data!F2:F5000))C3 divides the other KPI cells, so the logic is written once and SUBTOTAL appears only in A3 and B3. If you later change the data range, you edit only those two cells and the share formula. Use number formats instead of rounding in the formula. Currency for A3 and C3, a plain integer for B3, and percent for D3 keep the underlying values exact.
Test that the KPIs follow the slicer
Check this on the sample table before you trust it:
- With no slicer selection, confirm
A3equals=SUM(Data!F2:F5000). - Select
Westin aRegionslicer. For the three sample rows,A3should become 450 andB3should become 1. - Select a value that has no matching rows, or clear all values in a condition filter.
B3should show 0 andC3should show 0 rather than an error. - Clear the slicer. The numbers should return to their unfiltered values.
If step 2 does nothing, the slicer is not filtering the range the formula reads. The troubleshooting chapter covers that diagnosis in more detail.
Exercise
The dashboard has a KPI =SUM(Data!F2:F1000) and a Region slicer on the Data range. A viewer selects East, and the KPI does not change. Rewrite the formula so it follows the slicer, and explain which code you chose and why.
Show answer
Use =SUBTOTAL(109, Data!F2:F1000). Code 109 is SUM that ignores all hidden rows, including rows filtered out by the slicer. Code 9 would also work for slicer-hidden rows, but 109 gives the same answer when rows are hidden by hand. A plain SUM reads hidden rows, so it never changes.
Exercise
You add a "Revenue by Region" table with QUERY and place it next to the KPI cells. After the viewer selects East in the slicer, the KPIs show East only, but the QUERY table still lists every region. Why, and how would you make the table follow the slicer?
Show answer
QUERY reads cell values whether or not the rows are hidden, so slicer filtering does not affect it. To get a table that responds to the slicer, build a pivot table on the Data range and connect the slicer to that pivot table, or point SUBTOTAL formulas at the Data columns for each region.
Exercise
Write a formula for "orders per unit sold" using the visible rows, which returns 0 when no rows are visible and does not hide errors other than an empty selection.
Show answer
=IF(SUBTOTAL(109, Data!E2:E5000) = 0, 0, SUBTOTAL(103, Data!A2:A5000) / SUBTOTAL(109, Data!E2:E5000)). The IF tests only for zero visible units, so other errors such as #REF! still appear.