de-dbt-transform
When to use
Use this when implementing in-warehouse transforms with dbt: laying out the model DAG, writing SQL models, and attaching tests and docs. This is the ELT "T" step.
Not for: deciding the dimensional schema (use de-data-modeling), moving data into the warehouse (use de-pipeline-design), scheduling dbt run (use de-orchestration), or defining warehouse-wide quality SLAs (use de-data-quality).
Method
- Layer the models. Three tiers:
staging (one model per source table — rename, cast, light clean, no joins), intermediate (reusable joins/aggregations), marts (business-facing facts and dims). Decision point: if two marts repeat the same join, extract it into intermediate.
- One staging model per source, prefixed
stg_. Reference raw only through source(); reference other models only through ref(). Never hard-code table names.
- Choose materialization per model:
view for staging (cheap, always fresh), table for marts read often, incremental for large append/merge facts with a unique_key and is_incremental() filter.
- Write tests. Generic tests in YAML (
unique, not_null, accepted_values, relationships) on keys and enums; singular SQL tests for business rules. Every mart primary key gets unique + not_null.
- Document every model and its key columns in the schema YAML — descriptions render into dbt docs and become the data catalog.
- Verify:
dbt build (runs + tests) on a dev target before promoting. Decision point: a failing test blocks the model's downstream consumers — fix, do not --warn.
Example
-- models/staging/stg_orders.sql
with source as (select * from {{ source('shop','orders') }})
select
order_id,
customer_id,
cast(ordered_at as timestamp) as ordered_at,
cast(amount_cents as numeric) / 100 as amount
from source
# models/staging/_stg_orders.yml
models:
- name: stg_orders
description: "One row per order, typed and renamed from shop.orders."
columns:
- name: order_id
description: "Natural key."
tests: [unique, not_null]
- name: customer_id
tests:
- relationships: {to: ref('stg_customers'), field: customer_id}
Pitfalls
- Business logic or joins inside staging, so raw fixes ripple everywhere.
- Hard-coded schema names instead of
ref()/source(), breaking env promotion and lineage.
- Marts materialized as views, forcing expensive recomputation on every BI query.
- Incremental models with no
unique_key, quietly appending duplicates on re-run.
Output format
DAG: staging (stg_*) → intermediate (int_*) → marts (fct_/dim_)
Model: <name> | layer: <...> | materialization: <view|table|incremental>
Refs: source(<...>) / ref(<...>)
Tests: <col → unique|not_null|accepted_values|relationships>
Docs: model + key-column descriptions in schema.yml
Verify: dbt build --select <model>+ (green before promote)
1---2name: de-dbt-transform3version: 1.0.04description: Structure and write dbt models — staging to marts layering, tests, and documentation.5author: matrixx00706tags: [data-engineering, dbt, transform, sql, testing]7capabilities: []8metadata:9 source: https://skillmd.com/skills/matrixx0070/de-dbt-transform10---1112# de-dbt-transform1314## When to use1516Use this when implementing in-warehouse transforms with dbt: laying out the model DAG, writing SQL models, and attaching tests and docs. This is the ELT "T" step.1718**Not for:** deciding the dimensional schema (use de-data-modeling), moving data into the warehouse (use de-pipeline-design), scheduling `dbt run` (use de-orchestration), or defining warehouse-wide quality SLAs (use de-data-quality).1920## Method21221. **Layer the models.** Three tiers: `staging` (one model per source table — rename, cast, light clean, no joins), `intermediate` (reusable joins/aggregations), `marts` (business-facing facts and dims). Decision point: if two marts repeat the same join, extract it into intermediate.232. **One staging model per source, prefixed `stg_`.** Reference raw only through `source()`; reference other models only through `ref()`. Never hard-code table names.243. **Choose materialization per model:** `view` for staging (cheap, always fresh), `table` for marts read often, `incremental` for large append/merge facts with a unique_key and `is_incremental()` filter.254. **Write tests.** Generic tests in YAML (`unique`, `not_null`, `accepted_values`, `relationships`) on keys and enums; singular SQL tests for business rules. Every mart primary key gets `unique` + `not_null`.265. **Document** every model and its key columns in the schema YAML — descriptions render into dbt docs and become the data catalog.276. **Verify:** `dbt build` (runs + tests) on a dev target before promoting. Decision point: a failing test blocks the model's downstream consumers — fix, do not `--warn`.2829## Example3031```sql32-- models/staging/stg_orders.sql33with source as (select * from {{ source('shop','orders') }})34select35 order_id,36 customer_id,37 cast(ordered_at as timestamp) as ordered_at,38 cast(amount_cents as numeric) / 100 as amount39from source40```4142```yaml43# models/staging/_stg_orders.yml44models:45 - name: stg_orders46 description: "One row per order, typed and renamed from shop.orders."47 columns:48 - name: order_id49 description: "Natural key."50 tests: [unique, not_null]51 - name: customer_id52 tests:53 - relationships: {to: ref('stg_customers'), field: customer_id}54```5556## Pitfalls5758- Business logic or joins inside staging, so raw fixes ripple everywhere.59- Hard-coded schema names instead of `ref()`/`source()`, breaking env promotion and lineage.60- Marts materialized as views, forcing expensive recomputation on every BI query.61- Incremental models with no `unique_key`, quietly appending duplicates on re-run.6263## Output format6465```66DAG: staging (stg_*) → intermediate (int_*) → marts (fct_/dim_)67Model: <name> | layer: <...> | materialization: <view|table|incremental>68Refs: source(<...>) / ref(<...>)69Tests: <col → unique|not_null|accepted_values|relationships>70Docs: model + key-column descriptions in schema.yml71Verify: dbt build --select <model>+ (green before promote)72```