Skip to content

How filters work ​

Filters let people narrow down a dashboard without editing any queries. You write a placeholder in curly braces in a chart's query. Diver then shows a matching control at the top of the dashboard and fills in the placeholder with whatever the viewer picks.

sql
SELECT country, count(*) AS signups
FROM users
WHERE {created_at=timerange}
  AND {country=countries}
GROUP BY country

When a viewer picks "Last 7 days" and selects Denmark and Sweden, Diver runs:

sql
SELECT country, count(*) AS signups
FROM users
WHERE created_at between ? and ?
  AND country in (?, ?)
GROUP BY country

The selected values are sent to the database separately (the ? marks), never pasted into the SQL. You don't need to quote them, and a filter value can't change what your query does.

Kinds of filters ​

FilterPlaceholderWhat the viewer gets
Time range{created_at=timerange}A time range picker: relative ("Last 7 days"), from–to, before or after.
Aggregation{created_at:aggregation}A picker for grouping by minute, hour, day, week, month, quarter or year.
Custom filters{country=countries}A dropdown whose options come from a query you write.

timerange and aggregation are built in. Custom filters are ones you create on the Filters page in Diver.

A dashboard only shows the controls its charts use. If no chart on a dashboard mentions timerange, the dashboard has no time range picker.

Placeholder syntax ​

A placeholder is a column or SQL expression, an operator and a filter name, inside curly braces:

text
{<column or expression><operator><filter name>}
FormExampleBecomes
Compare{country=countries}country = ?, or country in (?, ?, …) when several values are selected
Compare with another operator{amount>=min_amount}amount >= ? (also <=, >, <, != and <>)
Group by time{created_at:aggregation}date_trunc('day', created_at)
Read the selection{countries.count}The number of selected values. See Reading the selection.

Diver splits a placeholder at the first ., :, =, <, > or != it finds, so keep the part before the operator to a plain column name:

  • No spaces around the operator. {country=countries} works, but in {country = countries} the filter name becomes " countries" with a leading space, which doesn't match any filter.
  • No table prefixes. {o.country=countries} is read as "the country=countries selector of a filter called o" and fails. In a query with joins, filter inside a subquery or CTE where the column name is unambiguous.
  • No casts or other operators. {created_at::date=timerange} is split at the ::. Cast in a subquery or CTE instead.
sql
WITH order_rows AS (
  SELECT o.id, o.created_at, c.country
  FROM orders o
  JOIN customers c ON c.id = o.customer_id
)
SELECT country, count(*) FROM order_rows
WHERE {created_at=timerange} AND {country=countries}
GROUP BY country

When nothing is selected ​

When a viewer hasn't picked anything for a filter, a = placeholder turns into a condition that is always true. The filter then has no effect, and the chart shows all rows:

sql
WHERE {country=countries}
-- with nothing selected, becomes:
WHERE 1 = 1

That's why = is the operator to use for most filters. With other operators, an empty filter compares 1 with 1 instead: >= and <= keep every row, but >, <, != and <> remove every row.

Reading the selection ​

Sometimes you need the selected values somewhere other than a WHERE condition, for example in a chart title column or a CASE expression. Add a dot and one of these after the filter name:

PlaceholderValueWith nothing selected
{countries.selected}true if anything is selectedfalse
{countries.count}How many values are selected0
{countries.value}The selected value, if exactly one is selectedNULL
{countries.values}All selected values as a list, for IN(NULL)
{countries.values_concat}All selected values joined with commas, such as Denmark,SwedenAn empty string
{countries.min}, {countries.max}The lowest or highest selected valueNULL

For example, to show every country when nothing is selected and only the selected ones otherwise:

sql
SELECT country, count(*) AS signups
FROM users
WHERE NOT {countries.selected} OR country IN {countries.values}
GROUP BY country

The time range and aggregation pages describe the selectors they add.

Comments ​

Placeholders inside SQL comments (-- … and /* … */) are ignored, so you can comment out a filter while you work on a query:

sql
SELECT status, count(*) FROM orders
WHERE {created_at=timerange}
-- AND {status=statuses}
GROUP BY status

Reach new depths