Sub topic 16
Charts that follow filtered or formula-driven data
This chapter assumes you can insert a chart, open the chart editor, and set a data range (covered earlier). It also assumes you know basic formulas such as SUM. Here you will make a chart update when rows are filtered, hidden, or recalculated, without rebuilding it each time.
How a chart reads its range
A chart does not store numbers. It stores a reference to a range and redraws whenever the cells in that range change. That gives two ways to make a chart dynamic:
- Change which rows of the source range are visible, using a filter or by hiding rows.
- Point the chart at a range that a formula fills in, so the chart follows the formula's output.
Charts and filtered or hidden rows
By default, a chart ignores rows and columns that are hidden. A basic filter hides the rows that do not match, so the chart drops those rows too. If you filter a sales table to one region, the chart shows only that region.
The Setup tab of the chart editor has a checkbox named Include hidden/filtered data. Behaviour by setting:
| Setting | Hidden or filtered rows | Typical use |
|---|---|---|
| Unchecked (default) | Left out of the chart | Chart should match what you see on screen |
| Checked | Plotted anyway | Chart should always show the full data, whatever the filter |
Two points to watch:
- A filter changes the chart for everyone who opens the sheet, because a basic filter changes the shared view. If readers should see a fixed chart, uncheck the filter or use a formula-driven helper range instead.
- Hiding rows to tidy the sheet can silently remove data from the chart. If a chart is missing a category, check for hidden rows before suspecting the chart settings.
Charting formula output
Filters are quick but blunt. A formula gives you more control: you can group, sort, limit, and rename in one step, and the result sits in a visible helper range that the chart reads. This is usually the better choice for a dashboard.
QUERY as a chart source
QUERY runs a small SQL-like statement over a range. This formula builds a summary table of total sales per value in column A, for the North region only:
=QUERY(A1:C100, "select A, sum(C) where B = 'North' group by A label sum(C) 'Sales'", 1)Enter it in an empty cell, for example F1. It fills a two-column table with a header row. Select that table as the chart's data range. The last argument, 1, tells QUERY the source has one header row. The label clause gives the aggregated column a readable header, which becomes the series name in the legend. Without it, the header would read "sum Sales".
For a ranked chart, sort and limit in the same statement:
=QUERY(A1:C100, "select A, sum(C) group by A order by sum(C) desc limit 5 label sum(C) 'Sales'", 1)This returns the top five values of column A by total sales, already sorted. A bar chart built on it shows the top five and updates as the data changes.
FILTER, SORT, and UNIQUE
When you do not need grouping, simpler functions work:
=FILTER(A2:C100, B2:B100 = "North")returns only matching rows. It does not return a header, so type your own headers in the row above.=SORT(A2:C100, 3, FALSE)sorts by the third column, descending.=UNIQUE(A2:A100)lists each distinct value once, which is useful as a category list.
These functions return an array that spills into the cells below and to the right. Anything already in that area causes a #REF! error, so leave it empty.
Driving a chart from a dropdown
You can let the reader choose what the chart shows. Put a dropdown (Data > Data validation) in cell E1 listing the regions. Then build the QUERY statement from that cell:
=QUERY(A1:C100, "select A, sum(C) where B = '" & E1 & "' group by A label sum(C) 'Sales'", 1)The text between the double quotes is a fixed string, and & E1 & inserts the dropdown's value into it. The single quotes around the inserted value are part of the query syntax, since the region is text. When the reader picks "South", the formula recalculates and the chart redraws with South data. Give the chart a title that matches. Chart titles cannot hold formulas, so state the dependence in an adjacent cell or in a generic title such as "Sales by product (selected region)".
Keeping the range correct as data grows
A chart reads a fixed range, and a formula's output can change size. If the QUERY result grows from 5 rows to 12, a chart pointing at F1:G6 still shows only the first 5. Options:
- Set the range to an open-ended reference such as
F1:G, which includes everything below row 1 in those columns. Blank cells at the end are ignored by most chart types, but check the result after setting it. - Use
limitin QUERY so the output never exceeds a size you chose, and point the chart at exactly that size. - Place the helper table where nothing else sits below it, so growth cannot collide with other content.
If the source table itself grows, extend the formula's range too. A1:C100 stops at row 100. Using A1:C takes all rows in the columns.
Common errors with dynamic charts
| Symptom | Likely cause | Fix |
|---|---|---|
| Chart is empty after filtering | The filter matches no rows, or all matching rows are hidden | Clear the filter, or check the **Include hidden/filtered data** setting |
| Header row appears as a data point | The helper range has no header, or QUERY's third argument is 0 | Add a header row, or set the argument to 1 |
| Numbers plotted as zeros or ignored | The formula returns text, for example numbers joined with `&` | Return real numbers; remove `""` placeholders from number columns |
| `#REF!` in the helper range | Cells below or beside the formula are not empty | Clear the blocked cells |
| Chart shows old categories | The chart range ends before the new output | Extend the range or use an open-ended one |
| Dropdown has no effect | The formula does not reference the dropdown cell | Check the cell reference inside the query string |
If you want a line chart to leave a gap instead of dropping to zero for missing values, have the formula return NA() for them. Line charts skip #N/A cells.
Exercise
You have sales data in A1:C100 (A = product, B = region, C = amount, with headers in row 1). Write a formula that sums the amount per product for the region typed in cell E1, and explain which two things you must do so a column chart based on it stays correct when new products appear.
Show answer
=QUERY(A1:C100, "select A, sum(C) where B = '" & E1 & "' group by A label sum(C) 'Sales'", 1). Point the chart at the formula's output using an open-ended range such as F1:G, so extra rows are included. Keep the cells below and beside the formula empty so the result is not blocked by a #REF! error. To pick up new source rows, the range A1:C100 should be extended too, for example to A1:C.