name: design-dbt-project-structure
description: Use when starting a dbt (data build tool) project or analytics repo, or when an existing one has hundreds of tangled models with no clear layers — marts reading raw sources directly, duplicated business logic, inconsistent naming, and nobody sure which model is the source of truth.
source: 'dbt Labs, "How we structure our dbt projects" (Best Practice Guides), docs.getdbt.com; dbt Labs, "dbt style guide"; Kimball & Ross, "The Data Warehouse Toolkit", 3rd ed., 2013; Beauchemin, "Functional Data Engineering — a modern paradigm for batch data processing", 2018'
tags: [project-structure, dbt, analytics-engineering, data-modeling, sql, data-warehouse]
related: [design-data-pipeline, design-data-model, design-data-quality-framework, write-sql-query]
Design dbt Project Structure
Organize transformations in three layers (staging → intermediate → marts), with consistent naming, one model per source table in staging, business logic defined once, and tests and docs next to the models.
Why This Is Best Practice
Adopted by:
- dbt Labs' official "How we structure our dbt projects" guide, followed by the thousands of companies using dbt (JetBlue, GitLab, whose dbt project is public, and HubSpot).
- The marts layer applies Kimball dimensional modeling, the long-standing data-warehousing standard.
Impact:
- Layering confines source-specific cleanup to one place. When a source system changes, you fix one staging model instead of every downstream query.
- The DAG becomes readable, and tests at each layer catch data issues before they reach dashboards.
Why best:
- Without layers, every analyst rewrites the same joins and cleaning in different marts, and numbers stop matching across reports.
- Staging (clean once), intermediate (compose), and marts (serve the business) create a single source of truth with a clear direction of dependency.
Sources: dbt Labs, "How we structure our dbt projects" and "dbt style guide" (docs.getdbt.com). Kimball, R. & Ross, M. (2013), The Data Warehouse Toolkit, 3rd ed., Wiley. Beauchemin, M. (2018), "Functional Data Engineering".
Steps
Use the standard folder layout:
models/
staging/
stripe/
_stripe__sources.yml
_stripe__models.yml
stg_stripe__payments.sql
shopify/
stg_shopify__orders.sql
intermediate/
finance/
int_payments_pivoted_to_orders.sql
marts/
finance/
fct_orders.sql
_finance__models.yml
marketing/
dim_customers.sql
seeds/ snapshots/ macros/ tests/ analyses/
dbt_project.yml packages.yml
Staging: one model per source table, light cleanup only.
- Rename columns, cast types, convert units, and do basic cleaning.
- No joins and no aggregations.
- Folders are organized by source system.
- Name models
stg_<source>__<entity>s and materialize them as views.
- Only staging models use
{{ source() }}. Everything else uses {{ ref() }}.
Intermediate: purpose-built transformation steps.
- Joins, pivots, and re-graining that prepare data for marts.
- Folders are organized by business domain.
- Name them
int_<entity>s_<verb>s (for example int_payments_pivoted_to_orders).
- Not exposed to end users. Usually ephemeral or views.
Marts: business-facing entities.
- Facts (
fct_) and dimensions (dim_), or wide entity tables, organized by department or domain.
- Materialized as tables or incremental models.
- Each concept (customer, order, revenue) is defined once and reused.
Keep YAML next to models. Use _<dir>__sources.yml and _<dir>__models.yml per folder, with descriptions and tests. Every model gets at least a primary-key unique + not_null test. Add relationships and accepted_values where meaningful. See design-data-quality-framework.
Configure by folder in dbt_project.yml (materialization, schema, tags) instead of repeating config in every model.
Follow the style guide:
- lowercase SQL, CTEs over subqueries
- an "import CTEs → logical CTEs → final select" structure
- explicit column lists in marts
- SQLFluff enforced in CI
Use reusable code deliberately.
- Macros for repeated SQL patterns.
- Packages (dbt_utils, dbt_expectations) instead of hand-rolled versions.
- Seeds only for small, static mapping tables.
Run CI on every pull request. Use dbt build --select state:modified+ against a CI schema (slim CI), so changed models and their downstreams are built and tested before merge.
Rules
- Sources are referenced only in staging.
- Data flows staging → intermediate → marts, never backwards or sideways from marts.
- One staging model per source table, with one-to-one grain.
- Each business concept is defined in exactly one mart model.
- Every model has a description and a primary-key test.
Common Mistakes
- Marts selecting from
source() directly. Cleaning logic gets duplicated, and source changes break everything.
- Joins in staging. Staging becomes a second intermediate layer, and grain gets confusing.
- Folder-per-person or per-dashboard. The structure should follow sources and domains, not requesters.
- Materializing everything as tables. Slow and costly. Default staging to views and promote only as needed.
- No tests until something breaks. Add key tests when creating each model.
Examples
E-commerce analytics: stg_shopify__orders, stg_stripe__payments → int_payments_pivoted_to_orders → fct_orders (one row per order with payment totals) and dim_customers. The finance and marketing dashboards both read fct_orders, so revenue matches everywhere.
Source migration: Stripe is replaced by Adyen. A new stg_adyen__payments is mapped to the same columns, and int_payments_pivoted_to_orders unions both sources. The marts are unchanged.
When NOT to Use
- Non-dbt transformation stacks (Spark jobs, SQLMesh, Dataform). The layering principle still applies, but follow the tool's conventions.
- Tiny projects with a handful of models, where three layers add more ceremony than clarity. Start with staging plus marts.
1---2name: design-dbt-project-structure3description: Use when starting a dbt (data build tool) project or analytics repo, or when an existing one has hundreds of tangled models with no clear layers — marts reading4---5---6name: design-dbt-project-structure7description: Use when starting a dbt (data build tool) project or analytics repo, or when an existing one has hundreds of tangled models with no clear layers — marts reading raw sources directly, duplicated business logic, inconsistent naming, and nobody sure which model is the source of truth.8source: 'dbt Labs, "How we structure our dbt projects" (Best Practice Guides), docs.getdbt.com; dbt Labs, "dbt style guide"; Kimball & Ross, "The Data Warehouse Toolkit", 3rd ed., 2013; Beauchemin, "Functional Data Engineering — a modern paradigm for batch data processing", 2018'9tags: [project-structure, dbt, analytics-engineering, data-modeling, sql, data-warehouse]10related: [design-data-pipeline, design-data-model, design-data-quality-framework, write-sql-query]11---1213# Design dbt Project Structure1415Organize transformations in three layers (**staging → intermediate → marts**), with consistent naming, one model per source table in staging, business logic defined once, and tests and docs next to the models.1617## Why This Is Best Practice1819**Adopted by:**20- dbt Labs' official "How we structure our dbt projects" guide, followed by the thousands of companies using dbt (JetBlue, GitLab, whose dbt project is public, and HubSpot).21- The marts layer applies Kimball dimensional modeling, the long-standing data-warehousing standard.2223**Impact:**24- Layering confines source-specific cleanup to one place. When a source system changes, you fix one staging model instead of every downstream query.25- The DAG becomes readable, and tests at each layer catch data issues before they reach dashboards.2627**Why best:**28- Without layers, every analyst rewrites the same joins and cleaning in different marts, and numbers stop matching across reports.29- Staging (clean once), intermediate (compose), and marts (serve the business) create a single source of truth with a clear direction of dependency.3031Sources: dbt Labs, "How we structure our dbt projects" and "dbt style guide" (docs.getdbt.com). Kimball, R. & Ross, M. (2013), *The Data Warehouse Toolkit*, 3rd ed., Wiley. Beauchemin, M. (2018), "Functional Data Engineering".3233## Steps34351. **Use the standard folder layout:**36 ```37 models/38 staging/39 stripe/40 _stripe__sources.yml41 _stripe__models.yml42 stg_stripe__payments.sql43 shopify/44 stg_shopify__orders.sql45 intermediate/46 finance/47 int_payments_pivoted_to_orders.sql48 marts/49 finance/50 fct_orders.sql51 _finance__models.yml52 marketing/53 dim_customers.sql54 seeds/ snapshots/ macros/ tests/ analyses/55 dbt_project.yml packages.yml56 ```57582. **Staging: one model per source table, light cleanup only.**59 - Rename columns, cast types, convert units, and do basic cleaning.60 - No joins and no aggregations.61 - Folders are organized by *source system*.62 - Name models `stg_<source>__<entity>s` and materialize them as views.63 - Only staging models use `{{ source() }}`. Everything else uses `{{ ref() }}`.64653. **Intermediate: purpose-built transformation steps.**66 - Joins, pivots, and re-graining that prepare data for marts.67 - Folders are organized by *business domain*.68 - Name them `int_<entity>s_<verb>s` (for example `int_payments_pivoted_to_orders`).69 - Not exposed to end users. Usually ephemeral or views.70714. **Marts: business-facing entities.**72 - Facts (`fct_`) and dimensions (`dim_`), or wide entity tables, organized by department or domain.73 - Materialized as tables or incremental models.74 - Each concept (customer, order, revenue) is defined once and reused.75765. **Keep YAML next to models.** Use `_<dir>__sources.yml` and `_<dir>__models.yml` per folder, with descriptions and tests. Every model gets at least a primary-key `unique` + `not_null` test. Add `relationships` and `accepted_values` where meaningful. See `design-data-quality-framework`.77786. **Configure by folder in `dbt_project.yml`** (materialization, schema, tags) instead of repeating config in every model.79807. **Follow the style guide:**81 - lowercase SQL, CTEs over subqueries82 - an "import CTEs → logical CTEs → final select" structure83 - explicit column lists in marts84 - SQLFluff enforced in CI85868. **Use reusable code deliberately.**87 - Macros for repeated SQL patterns.88 - Packages (dbt_utils, dbt_expectations) instead of hand-rolled versions.89 - Seeds only for small, static mapping tables.90919. **Run CI on every pull request.** Use `dbt build --select state:modified+` against a CI schema (slim CI), so changed models and their downstreams are built and tested before merge.9293## Rules9495- Sources are referenced only in staging.96- Data flows staging → intermediate → marts, never backwards or sideways from marts.97- One staging model per source table, with one-to-one grain.98- Each business concept is defined in exactly one mart model.99- Every model has a description and a primary-key test.100101## Common Mistakes102103- **Marts selecting from `source()` directly.** Cleaning logic gets duplicated, and source changes break everything.104- **Joins in staging.** Staging becomes a second intermediate layer, and grain gets confusing.105- **Folder-per-person or per-dashboard.** The structure should follow sources and domains, not requesters.106- **Materializing everything as tables.** Slow and costly. Default staging to views and promote only as needed.107- **No tests until something breaks.** Add key tests when creating each model.108109## Examples110111**E-commerce analytics:** `stg_shopify__orders`, `stg_stripe__payments` → `int_payments_pivoted_to_orders` → `fct_orders` (one row per order with payment totals) and `dim_customers`. The finance and marketing dashboards both read `fct_orders`, so revenue matches everywhere.112113**Source migration:** Stripe is replaced by Adyen. A new `stg_adyen__payments` is mapped to the same columns, and `int_payments_pivoted_to_orders` unions both sources. The marts are unchanged.114115## When NOT to Use116117- **Non-dbt transformation stacks** (Spark jobs, SQLMesh, Dataform). The layering principle still applies, but follow the tool's conventions.118- **Tiny projects with a handful of models**, where three layers add more ceremony than clarity. Start with staging plus marts.