Skip to content

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 day

Put 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:

ModeExampleRows included
RelativeLast 7 days, This monthFrom the start of the period until now
From-To1 March to 31 MarchBetween the two dates
AfterAfter 1 MarchOn or after the date
BeforeBefore 1 MarchOn 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-Tocreated_at between ? and ?
Aftercreated_at >= ?
Beforecreated_at <= ?
Nothing1 = 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_at doesn'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.

Reach new depths