Instructions
- Type: Time:
- Creates a set of time-based dimensions (date, week, month, etc.) from a single timestamp.
- Required:
timeframes(list of frames to generate),sql(the timestamp column). - Optional:
datatype(if not standard timestamp),convert_tz(timezone conversion).
- Type: Duration:
- Calculates the time between two timestamps.
- Required:
intervals(day, week, etc.),sql_start,sql_end.
- Datatype Parameter:
- When to use: Use
datatypewhen your database column is not a standardtimestamp(YYYY-MM-DD HH:MM:SS). - Common Options:
timestamp(Default): Standard datetime/timestamp column.date: Date-only string (e.g., '2023-01-01').epoch: Integer unix timestamp.yyyymmdd: Integer date format.
- When to use: Use
- Datatype: Date:
- Usage: Explicitly set
datatype: dateif the source column has no time component. - Differences:
- Timezones: No timezone conversion is performed.
- Timeframes: restricted to date-level grains.
- Allowed Timeframes:
[date, week, month, quarter, year]. - Disallowed:
[time, hour, minute, second].
- Usage: Explicitly set
- Best Practices:
- Common Timeframes:
[raw, time, date, week, month, quarter, year]. - Extended Timeframes:
[hour_of_day, day_of_week, day_of_month, month_name, week_of_year]. - Naming: The dimension names will be
group_name_timeframe(e.g.,created_date).
- Common Timeframes:
- Labeling & Naming Strategy:
- How it works: Looker combines the
dimension_groupname with thetimeframeto generate the field name (name_timeframe) and the UI label ("Name Timeframe"). - Best Practice: Remove suffixes like
_at,_timestamp, or_datefrom the dimension group name to avoid redundant labels.- Bad:
dimension_group: created_atgeneratescreated_at_date-> Label: "Created At Date". - Good:
dimension_group: createdgeneratescreated_date-> Label: "Created Date". - Good:
dimension_group: ordergeneratesorder_date-> Label: "Order Date".
- Bad:
- How it works: Looker combines the
- Default Timeframes:
- What are they?: If
timeframesis omitted, Looker generates[date, week, month, year](andtimefor timestamps). - Crucial Missing Item: The
rawtimeframe is NOT included by default. Failing to includerawprevents efficient joins on the underlying column. - Best Practice: Never rely on defaults. Explicitly define
timeframes: [raw, time, date, week, month, quarter, year]to ensure full utility.
- What are they?: If
- Handling Date Strings:
- The Issue: Databases often store dates as strings (e.g.,
'2023-01-01') for various reasons. - Why It Matters: Looker's timeframes (month, week, quarter) rely on date/timestamp functions to truncate and group data. These functions fail on strings.
- The Fix: You must cast the string to a date or timestamp in the
sqlparameter.- Example:
sql: CAST(${TABLE}.date_string AS DATE) ;; - Example (BigQuery):
sql: PARSE_DATE('%Y-%m-%d', ${TABLE}.date_string) ;;
- Example:
- The Issue: Databases often store dates as strings (e.g.,
Examples
Basic Time Dimension Group
dimension_group: created {
type: time
timeframes: [
raw,
time,
date,
week,
month,
quarter,
year
]
sql: ${TABLE}.created_at ;;
}
Duration Dimension Group
dimension_group: duration_since_signup {
type: duration
intervals: [day, week, month]
sql_start: ${signup_raw} ;;
sql_end: ${orders.created_raw} ;;
}