Skip to content

6. Smaller Builds in Development

Difficulty: Medium · Topic: Jinja, vars & packages

{{ source('app', 'events') }} holds years of events, and rebuilding all of it on every dbt run in development is slow and expensive.

Write stg_events (columns event_id, user_id, event_type, event_at) so that:

Hidden tests run in both targets.

Your model is compiled like dbt does and run in DuckDB against 4 test cases, including hidden edge cases.

Sample inputs

{{ source('app', 'events') }}

event_iduser_idevent_typeevent_at
110view2024-05-01 08:00:00
211click2024-05-06 12:00:00
310view2024-05-07 09:30:00
412purchase2024-05-09 12:00:00

Expected output

event_iduser_idevent_typeevent_at
211click2024-05-06 12:00:00
310view2024-05-07 09:30:00
412purchase2024-05-09 12:00:00

Hints

Hint 1{% if target.name == 'dev' %} ... {% endif %} adds the WHERE clause only when compiling for dev; prod gets plain SQL with no filter at all.
Hint 2Anchor the window on the data, not on today: (select max(event_at) from {{ source('app', 'events') }}) - interval 3 day. Sample data never moves, so a filter on the current date would return nothing.
Hint 3"At or after" means >=.

More dbt problems

All dbt problems