0
0
dbtdata~30 mins

Building a DAG of models in dbt - Mini Project: Build & Apply

Choose your learning style9 modes available
Building a DAG of models
📖 Scenario: You work in a company that uses dbt to organize data transformations. You want to build a simple Directed Acyclic Graph (DAG) of models where one model depends on another. This helps keep your data organized and updated in the right order.
🎯 Goal: Create two dbt models where the second model depends on the first. This will build a DAG of models showing how data flows from one step to the next.
📋 What You'll Learn
Create a base model with a simple SQL SELECT statement
Create a second model that selects from the first model using the ref() function
Use the ref() function to define dependencies between models
Run the models to see the DAG in action
💡 Why This Matters
🌍 Real World
In real companies, dbt helps organize complex data transformations by building DAGs of models. This keeps data pipelines clear and maintainable.
💼 Career
Data engineers and analysts use dbt to build reliable data workflows. Knowing how to build DAGs is essential for managing dependencies and ensuring data quality.
Progress0 / 4 steps
1
Create the base model
Create a dbt model file called base_customers.sql with a SQL query that selects id and name from a table called raw_customers. Write the exact SQL: select id, name from raw_customers.
dbt
Need a hint?

This model is the starting point. It just selects data from the raw source table.

2
Create a dependent model
Create a second dbt model file called active_customers.sql. Use the ref() function to select all columns from the base_customers model. Write the exact SQL: select * from {{ ref('base_customers') }}.
dbt
Need a hint?

This model depends on the base model. The ref() function tells dbt about this dependency.

3
Add a filter to the dependent model
Modify the active_customers.sql model to select only customers with id greater than 100. Use the exact SQL: select * from {{ ref('base_customers') }} where id > 100.
dbt
Need a hint?

Adding a filter shows how you can transform data in dependent models.

4
Print the DAG structure
Run the dbt command to show the DAG graph. Write the exact command: dbt ls --select active_customers --output graph.
dbt
Need a hint?

This command lists the models and shows their dependencies as a graph.