0
0
dbtdata~10 mins

Building a DAG of models in dbt - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Building a DAG of models
Define base models
Create intermediate models
Reference base models in SQL
Create final models
Reference intermediate models
dbt builds DAG
Run models in dependency order
You start by defining simple base models, then build intermediate models that use those bases, and finally create final models that depend on intermediates. dbt reads these references to build a DAG and runs models in the right order.
Execution Sample
dbt
with base as (
  select * from raw_data
),
intermediate as (
  select * from base where value > 10
),
final as (
  select count(*) as total from intermediate
)
select * from final
This SQL defines a base model selecting raw data, then an intermediate model filtering that base, and a final model aggregating the intermediate, showing how models depend on each other.
Execution Table
StepActionModel CreatedDependenciesResult
1Define base model SQLbaseNoneModel 'base' selects raw_data table
2Define intermediate model SQLintermediatebaseModel 'intermediate' filters 'base' where value > 10
3Define final model SQLfinalintermediateModel 'final' aggregates 'intermediate' results
4dbt parses modelsDAG builtbase -> intermediate -> finalDAG shows dependencies
5dbt runs models in orderbaseNonebase model runs first
6dbt runs models in orderintermediatebaseintermediate runs after base
7dbt runs models in orderfinalintermediatefinal runs last
8Execution completeAll modelsAll dependencies metAll models built successfully
💡 All models executed in dependency order, DAG run complete
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
baseundefinedDefined as raw_data selectDefinedDefinedUsed by intermediate
intermediateundefinedundefinedDefined as filtered baseDefinedUsed by final
finalundefinedundefinedundefinedDefined as aggregation of intermediateBuilt last
DAGemptybase onlybase -> intermediatebase -> intermediate -> finalComplete DAG
Run Orderemptybasebase -> intermediatebase -> intermediate -> finalExecution done
Key Moments - 3 Insights
Why does dbt run the 'base' model before 'intermediate'?
Because 'intermediate' depends on 'base' as shown in the execution_table rows 5 and 6, dbt runs models respecting dependencies to ensure data is ready.
What happens if a model does not reference any other model?
It is treated as a base model with no dependencies, so dbt runs it first as shown in execution_table row 1 and 5.
How does dbt know the order to run models?
dbt builds a DAG from model references (execution_table row 4), then runs models in topological order to satisfy dependencies.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, which model runs immediately after 'base'?
Afinal
Bintermediate
Craw_data
DNone
💡 Hint
Check rows 5 and 6 in the execution_table showing run order
At which step does dbt build the DAG of models?
AStep 4
BStep 6
CStep 2
DStep 8
💡 Hint
Look at the 'Action' column in execution_table row 4
If the 'intermediate' model did not reference 'base', what would change in the DAG?
ADAG would be the same
BDAG would have extra edges
CDAG would have no edges between models
DDAG would fail to build
💡 Hint
Check variable_tracker row for DAG showing dependencies
Concept Snapshot
Building a DAG of models in dbt:
- Define base models with no dependencies
- Create intermediate models referencing base models
- Create final models referencing intermediate models
- dbt parses references to build a DAG
- dbt runs models in dependency order
- This ensures data flows correctly through models
Full Transcript
In dbt, you build a Directed Acyclic Graph (DAG) of models by writing SQL models that reference each other. You start with base models that select raw data. Then you create intermediate models that use base models in their SQL. Finally, you create final models that depend on intermediate models. dbt reads these references and builds a DAG showing dependencies. When you run dbt, it executes models in order so each model's dependencies are ready first. This process ensures your data transformations happen in the right sequence. The execution table shows each step: defining models, building the DAG, and running models in order. Variables track model definitions and DAG state. Key moments clarify why order matters and how dbt knows it. The quiz tests your understanding of model order and DAG building.