dbt Core Tutorial: Building Modular Data Models
In modern data stacks, managing raw SQL scripts across complex data warehouses quickly leads to messy, unmaintainable spaghetti queries and undocumented tables. Traditional ETL pipelines often lack version control, automated testing, and modular lineage.
Enter dbt (data build tool). As the gold standard for analytics engineering, dbt allows data analysts and engineers to transform data in their warehouse by writing modular, parameterized SELECT statements combined with Jinja templating, automated testing, and self-documenting data lineage graphs.
The Modular dbt Layering Architecture
A well-architected dbt project organizes transformations into three distinct layers to ensure reusability and maintainability:
- Staging Layer (
stg_): Connects directly to raw source tables. Responsible for light cleaning, column renaming, type casting, and standardizing naming conventions without heavy business logic. - Intermediate Layer (
int_): Optional modular building blocks where complex joins, business logic aggregations, and data restructuring occur between staging tables and final models. - Marts Layer (
fct_/dim_): Business-ready dimensional and fact tables designed specifically for reporting, BI dashboards, and stakeholders.
Traditional Stored Procedures vs. dbt Modular Models
| Evaluation Metric | Traditional SQL Stored Procedures | dbt Modular Data Models |
|---|---|---|
| Version Control & CI/CD | Difficult to version control or run automated tests across procedures | Native Git version control, automated testing, and CI/CD integration |
| Dependency Management | Manual execution ordering prone to pipeline failure | Automatic dependency resolution via the ref() function |
| Data Lineage | Opaque dependencies hidden across database schemas | Interactive automatically generated documentation and lineage graphs |
dbt Model Implementation Blueprint
Using dbt's ref() function, you can build a modular fact model that references a cleaned staging table cleanly:
-- models/marts/fct_customer_orders.sql
WITH staging_orders AS (
SELECT
order_id,
customer_id,
order_date,
order_status,
amount
FROM {{ ref('stg_raw_orders') }}
),
staging_customers AS (
SELECT
customer_id,
signup_date,
country
FROM {{ ref('stg_raw_customers') }}
),
final AS (
SELECT
o.order_id,
o.customer_id,
c.country,
o.order_date,
o.amount,
CASE WHEN o.order_status = 'completed' THEN 1 ELSE 0 END AS is_successful_order
FROM staging_orders o
LEFT JOIN staging_customers c ON o.customer_id = c.customer_id
)
SELECT * FROM final
Production Best Practices
Operating dbt projects successfully in enterprise analytics environments requires strict adherence to modeling standards:
- Always Use
ref(): Never hardcode schema and table names in your SQL models; always use{{ ref('model_name') }}so dbt can build an accurate directed acyclic graph (DAG) of your dependencies. - Write Comprehensive Schema Tests: Add
not_null,unique, andaccepted_valuestests to yourschema.ymlfiles to catch data quality issues automatically before reports break. - Configure Materializations Appropriately: Keep staging models as views (
materialized='view') for zero storage overhead, while materializing heavy marts models as tables or incremental models (materialized='incremental').