year()
One bucket per year. Example: 2024-01-01.
AQLearn 1.4 / Function
Source lesson: AQLearn 1.4: Function: date_trunc. Tangible win: bucket a date or datetime dimension into year, quarter, month, day.
date_trunc() is a scalar function that truncates a date or datetime field to a given time granularity. It returns the start of that bucket (for example, the first day of the month).
date_trunc(model.field, 'datetime_part')
Shorthand pipe form, more common in practice:
model.field | datetime_part()
year()One bucket per year. Example: 2024-01-01.
quarter()One bucket per calendar quarter. Example: 2024-04-01.
month()One bucket per calendar month. Example: 2024-03-01.
day()One bucket per day. Example: 2024-03-12.
Other valid parts: 'hour', 'minute', 'week', and more. See the date_trunc reference.
Same AQLearn ecommerce dataset as before. This lesson focuses on the orders.created_at field.
orders.created_at. We will truncate it to year, quarter, month, and day.
Starting from this explore:
explore {
measures {
count_of_orders: orders | count(orders.id),
}
dimensions {
time: orders.created_at,
}
}
Swap the time dimension to get different buckets.
| Goal | Dimension expression | Example bucket |
|---|---|---|
| By year | orders.created_at | year() | 2024-01-01 |
| By quarter | orders.created_at | quarter() | 2024-04-01 |
| By month | orders.created_at | month() | 2024-03-01 |
| By day | orders.created_at | day() | 2024-03-12 |
explore {
measures {
count_of_orders: orders | count(orders.id),
}
dimensions {
time: orders.created_at | year(),
}
}
explore {
measures {
avg_order_discount: orders | avg(orders.discount),
}
dimensions {
time: orders.created_at | quarter(),
}
}
orders.created_at | year() and run. Expect 4 rows (2020 to 2023).avg_order_discount: orders | avg(orders.discount),.orders.created_at | quarter(). Run.Next: filters. How to narrow the rows that feed your measures using explore filters {}, datetime literals, and operators.