Appearance
Custom filters
A custom filter gives a dashboard a dropdown of values to filter by, such as countries, product lines or customer tiers. You define the options with a query, and charts use the filter through a placeholder:
sql
WHERE {country=countries}Create a filter
- Open Filters in Diver's sidebar and select +.
- Enter a Filter name and a Filter identifier. The identifier is what you type in placeholders, such as
countries. Use lowercase letters, digits and underscores, and don't usetimerangeoraggregation, which are built in. - Choose the data source and write a query that returns the dropdown's options (see below).
- Select Test filter to run the query and check the options.
- Select Save.
The options query
The query returns one row per option, with one or two columns:
- First column: the value that's put into chart queries.
- Second column (optional): the label viewers see in the dropdown. Without it, the value is also the label.
sql
SELECT DISTINCT country_code, country_name
FROM customers
ORDER BY country_nameHere viewers pick from names like "Denmark", and charts filter on codes like DK. Diver doesn't save a filter whose query returns more than two columns.
Keep the list short enough to scroll through. For hundreds of options, consider grouping them first, for example by region instead of city.
Use the filter in a chart
Write the column you want to filter, =, and the filter's identifier:
sql
SELECT date_trunc('month', ordered_at) AS month, sum(amount) AS revenue
FROM orders
WHERE {country_code=countries}
GROUP BY month
ORDER BY monthOnce a chart on a dashboard uses the filter, the dashboard shows its dropdown. Viewers can select several values:
| The viewer selects | {country_code=countries} becomes |
|---|---|
DK | country_code = ? |
DK and SE | country_code in (?, ?) |
| Nothing | 1 = 1 (no rows are filtered out) |
The selected values are part of the dashboard's address, so a link to the dashboard keeps the selection.
Filters that depend on other filters
A filter's options query can use other filters, just like a chart's query. Its options then follow what viewers select in those filters. For example, a cities filter that only lists cities in the selected countries:
sql
SELECT DISTINCT city
FROM customers
WHERE {country_code=countries}
ORDER BY cityWhen a viewer selects Denmark in the countries dropdown, the cities dropdown lists only Danish cities. With no country selected, the placeholder has no effect, so every city is listed.
A filter's options can depend on several filters, including the time range. For example, use {created_at=timerange} to list only products that sold in the selected period.
Use both filters in a chart
A dashboard only shows a dropdown for filters that its charts use. A filter that's only used in another filter's options query doesn't get a dropdown of its own. For the example above, use both filters in a chart, such as WHERE {country_code=countries} AND {city=cities}. This also keeps the chart consistent with the dropdowns.
More ways to use a filter
- Other comparisons. Use another operator when the values are numbers or dates, such as
{amount>=min_amounts}. With several values selected, every operator becomesIN, so these work best when viewers pick one value. See When nothing is selected for how operators other than=behave with an empty selection. - The selection itself. Selectors such as
{countries.count}and{countries.values}give you the selected values directly. See Reading the selection. - See where a filter is used. The Usages column on the Filters page counts the charts and other filters that use each filter. Select the number to see them: charts grouped by dashboard, and filters whose options depend on this one. Check it before you rename or delete a filter.