Skip to content

15. Incremental Model with a Composite Key

Difficulty: Hard · Topic: Incremental models

The ERP extracts stock levels into {{ source('erp', 'stock_snapshots') }}. The stock level of a SKU in a warehouse on a day can be re-extracted later with a corrected on_hand; the row with the latest extracted_at is the truth. Corrections arrive at most one day late.

Build the incremental model fct_inventory_daily with one row per warehouse_id, sku and snapshot_date: warehouse_id, sku, snapshot_date, on_hand.

On an incremental run, take every source row whose snapshot_date is at or after the latest snapshot_date in the table minus 1 day, keep the latest extract per key, and upsert it on the three-column key. Older days are left as they are.

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

Sample inputs

{{ source('erp', 'stock_snapshots') }}

warehouse_idskusnapshot_dateon_handextracted_at
1A2024-09-01102024-09-01 23:00:00
1B2024-09-0152024-09-01 23:00:00
1A2024-09-0282024-09-02 23:00:00
1B2024-09-0242024-09-03 01:00:00
1A2024-09-0372024-09-03 23:00:00

The table before the run

warehouse_idskusnapshot_dateon_hand
1A2024-09-0110
1B2024-09-015
1A2024-09-028

Expected output

warehouse_idskusnapshot_dateon_hand
1A2024-09-0110
1B2024-09-015
1A2024-09-028
1B2024-09-024
1A2024-09-037

Hints

Hint 1A composite key is a list: unique_key=['warehouse_id', 'sku', 'snapshot_date'].
Hint 2Deduplicate the batch to the latest extracted_at per key, or dbt inserts two rows for one key.
Hint 3Filter: snapshot_date >= (select max(snapshot_date) from {{ this }}) - interval 1 day.

More dbt problems

All dbt problems