18. Rerun a Day Without Duplicates
Difficulty: Medium · Topic: Incremental models
The scheduler builds fct_page_views once a day with dbt run --vars '{run_date: "2024-03-02"}', and each run loads only that day's rows from {{ source('web', 'page_views') }} (view_id, user_id, page, view_date).
Days get rerun: the scheduler retries a failed run, or the upstream team fixes a day's data (removing bot views, adding rows that arrived late) and asks you to reload it. Every rerun must leave the table as if that day had been loaded once, from the source as it is now. Other days must not change.
This problem is graded over several runs against the same table, like a real schedule: day 1, day 2, then day 2 again. The table after every run must match.
Graded over several runs against the same table, like a real schedule: 3 tests, including hidden edge cases. The table after every run must match.
Run 1 (run_date = 2024-03-01)
{{ source('web', 'page_views') }}
| view_id | user_id | page | view_date |
|---|---|---|---|
| 1 | 10 | /home | 2024-03-01 |
| 2 | 11 | /pricing | 2024-03-01 |
Expected table after run 1
| view_id | user_id | page | view_date |
|---|---|---|---|
| 1 | 10 | /home | 2024-03-01 |
| 2 | 11 | /pricing | 2024-03-01 |
Run 2 (run_date = 2024-03-02)
{{ source('web', 'page_views') }}
| view_id | user_id | page | view_date |
|---|---|---|---|
| 1 | 10 | /home | 2024-03-01 |
| 2 | 11 | /pricing | 2024-03-01 |
| 3 | 10 | /docs | 2024-03-02 |
| 4 | 12 | /home | 2024-03-02 |
| 5 | 99 | /home | 2024-03-02 |
Expected table after run 2
| view_id | user_id | page | view_date |
|---|---|---|---|
| 1 | 10 | /home | 2024-03-01 |
| 2 | 11 | /pricing | 2024-03-01 |
| 3 | 10 | /docs | 2024-03-02 |
| 4 | 12 | /home | 2024-03-02 |
| 5 | 99 | /home | 2024-03-02 |
Run 3 (run_date = 2024-03-02)
{{ source('web', 'page_views') }}
| view_id | user_id | page | view_date |
|---|---|---|---|
| 1 | 10 | /home | 2024-03-01 |
| 2 | 11 | /pricing | 2024-03-01 |
| 3 | 10 | /docs | 2024-03-02 |
| 4 | 12 | /home | 2024-03-02 |
| 6 | 13 | /blog | 2024-03-02 |
Expected table after run 3
| view_id | user_id | page | view_date |
|---|---|---|---|
| 1 | 10 | /home | 2024-03-01 |
| 2 | 11 | /pricing | 2024-03-01 |
| 3 | 10 | /docs | 2024-03-02 |
| 4 | 12 | /home | 2024-03-02 |
| 6 | 13 | /blog | 2024-03-02 |
Hints
Hint 1
A plain incremental model appends: rerunning a day inserts its rows a second time. You need aunique_key so dbt removes rows before inserting.Hint 2
Think about what one run replaces. If the key isview_id, a row the upstream team deleted is never removed from your table.Hint 3
Withdelete+insert (dbt-duckdb's default) and unique_key='view_date', dbt deletes every row of the dates in the new batch, then inserts the batch: the whole day is replaced.Learn the concepts
- Incremental strategies · 6 min read. append, merge, delete+insert, insert_overwrite and microbatch: what each runs and which one fits.
- Late data and idempotent incrementals · 5 min read. Lookback windows, re-runs that give the same answer, schema changes and when to full refresh.