Build your Final Model
After creating initial models that transform raw data from your sources, you'll often need to build more complex models that combine and further transform your data. These "final" models typically reference other models rather than directly accessing source data.
Purpose
Building final models in dbt serves several important functions:
Combines data from multiple upstream models
Implements more complex business logic
Creates a final layer of data ready for consumption by BI tools or other stakeholders
Establishes a clear and maintainable structure in your dbt project
Key Components
Creating a Final Model File
Create a new .sql
file in your models
directory for your final model. For example:
Understanding the ref() Function
The ref()
function is crucial in building final models:
It references other models in your dbt project
Takes a single argument: the name of the model you're referencing
Allows dbt to automatically handle dependencies between models
Enables dbt to build models in the correct order
Here's a simplified example to illustrate how models can be "stacked":
The ref()
function performs two key tasks:
It interpolates the correct schema name into your model, allowing for flexible deployment configurations.
It helps dbt build a dependency graph, ensuring models are built in the correct order during
dbt run
.
Running Your Final Model
Execute your final model with the following dbt command:
This will ensure all upstream dependencies are built before materializing your final model.
Best Practices
Use clear, descriptive names for your final models
Leverage the
ref()
function to create a clear dependency structureConsider using table materializations for final models that will be queried frequently
Implement appropriate tests to ensure data quality and business logic
Document your final models thoroughly, including descriptions of what they represent and how they should be used
Review your model's query performance and optimize as necessary
Remember, final models should represent meaningful business concepts and be structured in a way that makes sense to your end users.
Last updated