In this lesson, you will learn how to extract repeated business logic into external dbt packages to keep your data pipelines DRY (Don't Repeat Yourself) across multiple project repositories.
The Problem of Duplication
As your organization grows, you likely maintain several dbt projects. Often, these projects share identical requirements—such as calculating a specific "customer lifetime value" (CLV) metric or standardizing a naming convention for created_at timestamps. If you define this logic separately in every project, updating the business rule becomes a maintenance nightmare: you have to hunt down and manually update the code in five different places.
Moving Logic to Packages
A dbt package is essentially a self-contained dbt project that serves as a library. By moving your common macros and models into a separate repository, you create a "single source of truth." Instead of copy-pasting code, you import the package into your project’s packages.yml file.
For example, if you create a package called my_company_utils, you can define a standard macro there:
-- In your shared package: macros/standardize_currency.sql
{% macro standardize_currency(column_name) %}
({{ column_name }} * 1.05) -- Applying a standard global adjustment
{% endmacro %}
In your actual data pipeline project, you simply reference it:
-- In your project's model: models/marts/orders.sql
select
order_id,
{{ my_company_utils.standardize_currency('amount_usd') }} as adjusted_amount
from {{ ref('stg_orders') }}
Why This Matters
When the business decides to change the currency adjustment logic, you update it once in the my_company_utils package. The next time you run dbt deps and execute your pipelines, all your projects automatically inherit the update.
This approach shifts your development style from managing monolithic scripts to building a modular ecosystem. It reduces bugs, ensures consistent reporting across different data products, and allows your team to share standardized logic like building blocks rather than starting from scratch every time you spin up a new pipeline.
Key Takeaway: Modularizing your code into dbt packages allows you to centralize business logic, eliminate code duplication, and ensure consistency across your entire data landscape.