Sources¶
Course map · Previous: Models · Next: Tests

{{ source('jaffle_shop', 'orders') }} points at a raw table declared in stg_jaffle_shop_source.yml. That file also sets freshness rules on _etl_loaded_at, the time load_raw.py loaded the data.
| Open | Run (from oms_dbt_proj) |
|---|---|
models/staging/*/*_source.yml |
dbt source freshness --profiles-dir . |
Learn¶
You could write raw.jaffle_shop.orders straight into a model. Declaring it as a source instead:
- names all of a source's tables in one place, so a moved schema is a one-line fix,
- puts the raw tables in the DAG and the docs, as the starting points,
- lets
dbt source freshnesscheck how old the data is.
Trimmed from models/staging/jaffle_shop/stg_jaffle_shop_source.yml:
sources:
- name: jaffle_shop
database: raw
schema: jaffle_shop
tables:
- name: customers
- name: orders
loaded_at_field: _etl_loaded_at
freshness:
warn_after: { count: 12, period: hour }
error_after: { count: 24, period: hour }
{{ source('jaffle_shop', 'orders') }} compiles to raw.jaffle_shop.orders. Freshness compares the newest _etl_loaded_at with now: older than 12 hours warns, older than 24 hours errors. See the source properties docs.
Run it in the browser¶
dbt source freshness runs a query like this one: how long ago was the newest row loaded? The playground loaded the data when you pressed Run, so the answer is close to 0 minutes.
select
max(_etl_loaded_at)::timestamp as last_loaded,
date_diff('minute', max(_etl_loaded_at)::timestamp, current_timestamp::timestamp) as minutes_old
from raw.jaffle_shop.orders
Quiz¶
What does {{ source('jaffle_shop', 'orders') }} compile to in this project?
raw.jaffle_shop.ordersanalytics.jaffle_shop.ordersstg_jaffle_shop__orders
Answer
raw.jaffle_shop.orders. The source YAML sets database: raw and schema: jaffle_shop, and the table is orders. Both targets expose that same name.
Orders were last loaded 13 hours ago. The rule is warn_after: 12 hours, error_after: 24 hours. What does dbt source freshness report?
- PASS
- WARN
- ERROR
Answer
WARN. 13 hours is past the 12-hour warning but inside the 24-hour error threshold.
Which column does dbt measure freshness from?
- The newest
order_date - The time
dbt runlast finished - The
loaded_at_field, here_etl_loaded_at
Answer
loaded_at_field. Freshness is about when the loader last wrote data, not about business dates. load_raw.py sets _etl_loaded_at to the load time.
Try it¶
Make orders freshness warn after 1 minute, wait 1 minute, and run the freshness check.
Hint
The rule lives under the orders table in models/staging/jaffle_shop/stg_jaffle_shop_source.yml. Change only warn_after. -s source:jaffle_shop.orders checks one table.
Answer
In models/staging/jaffle_shop/stg_jaffle_shop_source.yml, change the orders rule to:
warn_after: { count: 1, period: minute }
dbt source freshness --profiles-dir . -s source:jaffle_shop.orders
The result is WARN freshness of jaffle_shop.orders, because the data is older than 1 minute but younger than the 24-hour error_after.