0
0
dbtdata~5 mins

Building a DAG of models in dbt

Choose your learning style9 modes available
Introduction

A DAG helps organize your data models so they run in the right order. It shows how models depend on each other.

When you have multiple data models that rely on each other.
When you want to automate running models in the correct sequence.
When you want to understand how data flows through your transformations.
When you need to avoid errors from running models too early.
When you want to improve data pipeline efficiency by running only needed models.
Syntax
dbt
-- model_a.sql
select * from source_table

-- model_b.sql
select * from {{ ref('model_a') }} where condition

-- model_c.sql
select * from {{ ref('model_b') }}

Use {{ ref('model_name') }} to link models and create dependencies.

dbt automatically builds the DAG from these references.

Examples
This references model_a so dbt knows model_b depends on it.
dbt
select * from {{ ref('model_a') }}
This shows a model filtering data from another model it depends on.
dbt
select * from {{ ref('model_b') }} where status = 'active'
This aggregates data from a dependent model.
dbt
select id, count(*) from {{ ref('model_c') }} group by id
Sample Program

This example has three models. model_b depends on model_a, and model_c depends on model_b. dbt runs them in order.

dbt
/* model_a.sql */
select 1 as id, 'apple' as fruit

/* model_b.sql */
select * from {{ ref('model_a') }} where id = 1

/* model_c.sql */
select fruit, count(*) as count from {{ ref('model_b') }} group by fruit
OutputSuccess
Important Notes

Always use ref() to create dependencies; do not hardcode table names.

The DAG helps dbt run models in the right order automatically.

You can visualize the DAG using dbt docs generate and dbt docs serve.

Summary

A DAG shows how models depend on each other in dbt.

Use ref() to link models and build the DAG.

dbt uses the DAG to run models in the correct order.