Appearance
Time range
The built-in timerange filter adds a time range picker to the dashboard and limits a chart to rows in the chosen period.
sql
SELECT date_trunc('day', created_at) AS day, count(*) AS orders
FROM orders
WHERE {created_at=timerange}
GROUP BY day
ORDER BY dayPut the date or timestamp column you want to filter on before the =. The filter name is always timerange.
What viewers can choose
As soon as one chart on a dashboard uses timerange, the dashboard gets a time range picker with four modes:
| Mode | Example | Rows included |
|---|---|---|
| Relative | Last 7 days, This month | From the start of the period until now |
| From-To | 1 March to 31 March | Between the two dates |
| After | After 1 March | On or after the date |
| Before | Before 1 March | On or before the date |
Relative ranges move with time. "Last 7 days" always means the seven days up to the moment the chart loads, and "This week" starts on Monday of the current week.
What the query becomes
| The viewer chooses | {created_at=timerange} becomes |
|---|---|
| Relative or From-To | created_at between ? and ? |
| After | created_at >= ? |
| Before | created_at <= ? |
| Nothing | 1 = 1 (no rows are filtered out) |
Both ends of the range are included.
Use the start and end separately
When you need the dates themselves rather than a condition, use {timerange.start} and {timerange.end}. Each one is NULL when that end of the range is open, for example the end of an After range.
Compare a column against only one end:
sql
SELECT count(*) AS open_tickets
FROM tickets
WHERE opened_at <= {timerange.end}
AND (closed_at IS NULL OR closed_at > {timerange.end})Or show the selected period in a table:
sql
SELECT {timerange.start} AS period_start, {timerange.end} AS period_end, sum(amount) AS revenue
FROM payments
WHERE {paid_at=timerange}{timerange.selected} is true when a range is chosen, which is useful in a CASE expression that needs a fallback.
Tips
- Filter several columns with one picker. Every
{…=timerange}placeholder on a dashboard uses the same picker, so one query can filter two columns at once:WHERE {ordered_at=timerange} AND {paid_at=timerange}. - Joined tables. A table-qualified column such as
o.created_atdoesn't work in a placeholder (see Placeholder syntax). Filter inside a subquery or CTE instead, where the column name is unambiguous. - Combine it with aggregation. Use aggregation to let viewers choose whether the chart groups by day, week or month within the range.