Adjust dbt asset config for incremental models
With lineage in place, there’s one final adjustment to make to our scaffolding. In our dbt project, the daily_metrics model is an incremental model. Incremental models improve performance by avoiding full refreshes: instead of reprocessing everything, they only handle new or modified data based on a time filter.
src/project_dbt/analytics/models/marts/daily_metrics.sql
{{
config(
materialized='incremental',
unique_key='date_of_business',
tags=["daily"]
)
}}
with
trips as (
select *
from {{ ref('stg_trips') }}
),
daily_summary as (
select
date_trunc('day', pickup_datetime) as date_of_business,
count(*) as trip_count,
sum(duration) as total_duration,
sum(duration) / count(*) as average_duration,
sum(total_amount) as total_amount,
sum(total_amount) / count(*) as average_amount,
sum(case when duration > 30 then 1 else 0 end) / count(*) as pct_over_30_min
from trips
group by all
)
select *
from daily_summary
{% if is_incremental() %}
where date_of_business between '{{ var('min_date') }}' and '{{ var('max_date') }}'
{% endif %}
Here’s how incremental models work:
- First run: the model processes the full dataset with no filters.
- Subsequent runs: dbt applies the
is_incremental()filter, usingmin_dateandmax_datevalues provided at runtime.