Date and DateTime Dimensions
Use date and date_time dimensions for time-based grouping and filtering.
Overview
date: Date without time (e.g. order date, birth date). Format: YYYY-MM-DD.date_time: Timestamp with time (e.g. created_at, event time). Format: YYYY-MM-DD HH:MM.
Define them as dimensions and set grains to advertise how callers can group and filter.
Grains
Grains are the granularities you allow for a date or date_time dimension.
- type: dimension
name: Order Date
data_type: date
grains:
- day
- week
- month
- quarter
- year
expression:
sql: order_date
Available grains:
| Grain | Typical use | date | date_time |
|---|---|---|---|
raw | Original value | n/a | ✓ |
second | Second-level | n/a | ✓ |
minute | Minute-level | n/a | ✓ |
hour | Hour-level | n/a | ✓ |
day | Day-level | ✓ | ✓ |
week | Week-level | ✓ | ✓ |
month | Month-level | ✓ | ✓ |
quarter | Quarter-level | ✓ | ✓ |
year | Year-level | ✓ | ✓ |
For date_time, you can include raw, second, minute, and hour when you need sub-day precision.
Date Dimension Example
- type: dimension
name: Order Date
description: Date when order was placed
data_type: date
grains:
- day
- week
- month
- quarter
- year
expression:
sql: order_date
DateTime Dimension Example
- type: dimension
name: Created At
description: When the record was created
data_type: date_time
grains:
- raw
- hour
- day
- week
- month
expression:
sql: created_at
When to Use date vs date_time
date: Order date, ship date, birth date, any dimension where time-of-day does not matter.date_time: Created at, updated at, event time, any dimension where hour/minute/second can be used for grouping or filtering.
Grouping and Formatting
- A grain is applied at request time with a
truncatedecorator on the projection; the SQL you get back carriesDATE_TRUNC('month', ...)in the datasource’s dialect. See Projections. - Set a field format such as
date:shortwhen your client should print the value a particular way; it is metadata, not SQL.
Best Practices
- Set grains: only include grains you need; they tell callers what grouping makes sense.
- Prefer
datewhen time is irrelevant: Simpler and more efficient. - Use
date_timefor events: when callers need hour or minute level.