Skip to content

1. Staging Model for Raw Orders

Difficulty: Easy · Topic: Sources & staging

The shop's app writes orders to {{ source('shop', 'raw_orders') }}. Write the staging model stg_orders, the one place where raw columns are cleaned up for every model downstream.

Return order_id, customer_id, ordered_on, status, amount.

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

Sample inputs

{{ source('shop', 'raw_orders') }}

iduser_idorder_datestatusamount_cents_is_test
1102024-03-01Completed1999false
2112024-03-02 Shipped4500null
3102024-03-02PLACED 250true
4122024-03-03returned1200false

Expected output

order_idcustomer_idordered_onstatusamount
1102024-03-01completed19.99
2112024-03-02shipped45
4122024-03-03returned12

Hints

Hint 1Read the raw table with {{ source('shop', 'raw_orders') }}, never with a hard-coded table name: dbt uses it to draw lineage and check freshness.
Hint 2In SQL, not _is_test is null when the flag is null, and a WHERE clause drops null. coalesce(_is_test, false) = false (or _is_test is not true) keeps those rows.
Hint 3The usual staging shape is two CTEs: source (select * from the source) and renamed (the cleaned columns).

More dbt problems

All dbt problems