Working with Tags
Tags in dbt™ are powerful metadata labels that can be applied to various resources in your project. They enable flexible model selection, improved workflow management, and better project organization.
By leveraging tags, you can:
Group models logically – Categorize models based on refresh schedule, function, or ownership.
Control execution – Run or exclude specific sets of models.
Optimize CI/CD pipelines – Target models for incremental builds and tests.
Improve project maintainability – Standardize workflows across teams.
How to Apply Tags
Tags can be applied in two primary ways:
1. Defining Tags in a Model File
Tags can be assigned directly within SQL models using the config()
function:
2. Defining Tags in dbt_project.yml
dbt_project.yml
Tags can also be applied at the project level, affecting entire folders or groups of models:
✅ Tag Inheritance – Models inside a folder inherit the parent folder’s tags unless overridden.
Example:
Tags can also be applied to snapshots, seeds, and exposures in your project:
Selecting Models with Tags in dbt CLI
Once models have tags, they can be referenced in dbt commands.
Running Tagged Models:
Running Multiple Tags:
Excluding Specific Tags:
Running Models by Data Layer:
(Runs staging
models and all downstream dependencies.)
Running Tests or Snapshots by Tag:
Best Practices for Using Tags
To ensure effective and scalable use of tags in dbt™, follow these best practices:
✅ Use Clear Naming Conventions – Standardize tag names for consistency (daily_refresh
, finance_data
).
✅ Document Tag Usage – Maintain a README or project guide explaining tag structures.
✅ Avoid Overuse – Too many overlapping tags can create confusion.
✅ Integrate with CI/CD – Use tags to selectively run models in automated pipelines.
✅ Combine Tags with Graph Operators – Use +
and @
for efficient model selection.
Example: Layer-Based Tagging
Using tags for data modeling layers improves clarity:
Common Use Cases for Tags
1️⃣ Refresh Schedules
Group models by how frequently they update:
2️⃣ Data Classification
Differentiate PII data or public datasets:
3️⃣ Testing Strategies
Use tags to target critical models for testing:
Troubleshooting Tags
If tags are not behaving as expected, check the following:
1️⃣ Verify Tag Inheritance – Check if the model is inheriting tags from dbt_project.yml
.
2️⃣ Check Tag Syntax – Ensure tag names match exactly (case-sensitive).
3️⃣ Use dbt ls
to Validate Tags – Run:
(Lists all models with the finance
tag.)
4️⃣ Ensure Proper YAML Formatting – Indentation errors in dbt_project.yml
can cause tags to be ignored.
5️⃣ Confirm Tags Exist – Run:
(Checks for syntax issues that might impact tag recognition.)
By incorporating tags effectively, dbt™ users can optimize execution, improve project structure, and enhance collaboration. 🚀
Last updated
Was this helpful?