Skip to content

Aggregation ​

The built-in aggregation filter lets viewers choose how a chart groups rows over time: by minute, hour, day, week, month, quarter or year.

sql
SELECT {created_at:aggregation} AS period, count(*) AS signups
FROM users
WHERE {created_at=timerange}
GROUP BY period
ORDER BY period

Note the colon: {column:aggregation} rounds the column down to the start of the chosen unit. It doesn't filter any rows.

What viewers can choose ​

As soon as one chart on a dashboard uses aggregation, the dashboard gets a picker with Minute, Hour, Day, Week, Month, Quarter and Year. Every chart on the dashboard that uses aggregation follows the same choice.

What the query becomes ​

{created_at:aggregation} becomes date_trunc('<unit>', created_at):

The viewer choosesBecomes
Monthdate_trunc('month', created_at)
Weekdate_trunc('week', created_at)
Nothing yetdate_trunc('day', created_at) (day is the default)

Group by the result, as in the example above, to get one row per period.

Database support

date_trunc(unit, column) exists in PostgreSQL and Snowflake, among others, but not in MySQL. On MySQL, the aggregation filter produces a query the database can't run.

Use a fixed unit ​

To always group by the same unit, put the unit name after the colon instead of aggregation:

sql
SELECT {created_at:month} AS month, sum(amount) AS revenue
FROM payments
GROUP BY month

The units you can use are second, minute, hour, day, week, month, quarter and year. Viewers can't change a fixed unit, and the chart doesn't add a picker to the dashboard.

Diver replaces a misspelled unit, such as {created_at:months}, with 1, so the chart quietly shows a single group. If that happens, check the spelling.

Show the chosen unit ​

{aggregation.value} gives the chosen unit as text, such as 'month', or 'day' when nothing has been chosen yet. Use it to label a table or to format periods differently per unit:

sql
SELECT
  CASE {aggregation.value}
    WHEN 'month' THEN to_char({created_at:aggregation}, 'YYYY-MM')
    ELSE to_char({created_at:aggregation}, 'YYYY-MM-DD')
  END AS period,
  count(*) AS signups
FROM users
GROUP BY 1
ORDER BY 1

Reach new depths