MetricFlow time spine
It's common in analytics engineering to have a date dimension or "time spine" table as a base table for different types of time-based joins and aggregations. The structure of this table is typically a base column of daily or hourly dates, with additional columns for other time grains, like fiscal quarters, defined based on the base column. You can join other tables to the time spine on the base column to calculate metrics like revenue at a point in time, or to aggregate to a specific time grain.
MetricFlow requires you to define a time spine table as a model-level configuration in the Semantic Layer for time-based joins and aggregations, such as cumulative metrics. This configuration informs dbt which model should be used for time range joins. It is especially useful for cumulative metrics or calculating time-based offsets. The time spine model is joined to other tables when calculating certain types of metrics or dimensions. MetricFlow will join the time spine model in the compiled SQL for the following types of metrics and dimensions:
- Cumulative metrics
- Metric offsets
- Conversion metrics
- Slowly Changing Dimensions
- Metrics with the
join_to_timespine
configuration set to true
To see the generated SQL for the metric and dimension types that use time spine joins, refer to the respective documentation or add the compile=True
flag when querying the Semantic Layer to return the compiled SQL.
Configuring time spine in YAML
-
The time spine is a special model that tells dbt and MetricFlow how to use specific columns by defining their properties.
-
The
models
key for the time spine must be in yourmodels/
directory. -
You only need to configure time-spine models that the Semantic Layer should recognize.
-
At a minimum, define a time-spine table for a daily grain.
-
You can optionally define a time-spine table for a different granularity, like hourly.
-
Note that if you don’t have a date or calendar model in your project, you'll need to create one.
-
If you're looking to specify the grain of a time dimension so that MetricFlow can transform the underlying column to the required granularity, refer to the Time granularity documentation
If you already have a date dimension or time spine table in your dbt project, you can point MetricFlow to this table by updating the model
configuration to use this table in the Semantic Layer. This is a model-level configuration that tells dbt to use the model for time range joins in the Semantic Layer.
For example, given the following directory structure, you can create two time spine configurations, time_spine_hourly
and time_spine_daily
. MetricFlow supports granularities ranging from milliseconds to years. Refer to the Dimensions page (time_granularity tab) to find the full list of supported granularities.
Previously, you had to create a model called metricflow_time_spine
in your dbt project. Now, if your project already includes a date dimension or time spine table, you can simply configure MetricFlow to use that table by updating the model
setting in the Semantic Layer.
If you don’t have a date dimension table, you can still create one by using the following code snippet to build your time spine model.
For an example project, refer to our Jaffle shop example. Note that the models
key in the time spine configuration must be placed in your models/
directory.
Example time spine tables
Daily
Daily (BigQuery)
Use this model if you're using BigQuery. BigQuery supports DATE()
instead of TO_DATE()
:
Hourly
{{
config(
materialized = 'table',
)
}}
with hours as (
{{
dbt.date_spine(
'hour',
"to_date('01/01/2000','mm/dd/yyyy')",
"to_date('01/01/2025','mm/dd/yyyy')"
)
}}
),
final as (
select cast(date_hour as timestamp) as date_hour
from hours
)
select * from final
-- filter the time spine to a specific range
where date_day > dateadd(year, -4, current_timestamp())
and date_hour < dateadd(day, 30, current_timestamp())