Datacoves logo

Ultimate dbt-utils Cheat Sheet

ByMayra Peña 13 mins read
Ultimate dbt-utils cheat sheet
Key Summary

A practical cheat sheet for dbt-utils, covering essential macros, generic tests, and tips to streamline SQL modeling and boost data transformation workflows in dbt.

The dbt-utils package enhances the dbt experience by offering a suite of utility macros. Designed to tackle common SQL modeling patterns, it streamlines complex operations, allowing users to focus on data transformation rather than the intricacies of SQL. dbt-utils is a must-have tool for dbt aficionados!

dbt-utils Cheat sheet

The dbt-utils package is a gem in the world of data transformations. Let this cheat sheet guide you swiftly through its features, ensuring you get the most out of dbt-utils. Enjoy!

Get our free ebook dbt Cloud vs dbt Core

Comparing dbt Core and dbt Cloud? Download our eBook for insights on feature, pricing and total cost. Find the best fit for your business!

Get the PDF
Get free ebook dbt cloud

SQL Generators

The SQL generators in the dbt-utils package streamline your modeling tasks. By automating common SQL patterns, they minimize manual coding and guarantee consistent, high-quality queries. Think of it as a handy toolkit for every dbt user’s SQL endeavors!

Generic tests

Within the dbt-utils package lies a set of generic tests, designed to validate your data effortlessly. These tests ensure consistency and quality, checking for common issues without the need to craft custom validation rules. It’s data integrity made simple for dbt users.

Introspective macros

The introspective macros within the dbt-utils package are a window into your data’s metadata. They empower you to dynamically interact with and understand the underlying structure of your datasets. It’s like having a magnifying glass for the intricacies of your dbt projects!

‍

Please contact us with any errors or suggestions.

FAQ

Does dbt-utils work across different data warehouses?

+

Yes. dbt-utils is designed to be warehouse-agnostic. Its cross-database macros detect your adapter and generate the correct SQL syntax automatically. It supports Snowflake, BigQuery, Redshift, Databricks, Postgres, DuckDB, and any warehouse with a supported dbt adapter.

How do I fix version compatibility errors when upgrading dbt-utils?

+

Most errors come from using deprecated macro names or mismatched dbt Core versions. Common fixes include replacing surrogate_key with generate_surrogate_key, replacing condition with where in expression_is_true, and updating packages.yml to the latest compatible version. If you are on dbt Core 1.10.6 or higher, nest test arguments under an arguments key.

How do I install dbt-utils in my dbt project?

+

Add the package to packages.yml as - package: dbt-labs/dbt_utils with version: [latest version], then run dbt deps to install. Always pin to a specific version to avoid unexpected breaking changes from future releases. Check the dbt-utils hub page for the current version.

Is dbt-utils still relevant now that dbt Core has absorbed some of its macros?

+

Yes. While many cross-database macros from dbt-utils have moved into dbt Core under the dbt namespace, dbt-utils remains the standard for testing utilities, SQL generators like date_spine and pivot, and introspective macros. The package continues to be actively maintained and is still recommended for production dbt projects.

What are the most commonly used dbt-utils macros?

+

The dbt-utils package provides a collection of reusable macros that simplify common data transformation patterns. Some of the most commonly used macros include:

• generate_surrogate_key: Creates a deterministic hashed key from one or more columns

• date_spine: Generates a continuous sequence of dates or timestamps

• union_relations: Combines multiple tables using UNION ALL while aligning columns automatically

• pivot: Converts row values into columns

• star: Selects all columns from a relation except those explicitly excluded

• get_column_values: Returns distinct values from a column, for generating SQL from data

• safe_divide: Performs division while returning null instead of raising an error when dividing by zero

dbt-utils also includes many reusable test macros that extend the tests provided with dbt Core, making it easier to validate data quality and enforce modeling assumptions.

What does the date_spine macro do and when is it useful?

+

date_spine generates a continuous sequence of dates between a start and end date. It is useful when your source data has gaps and you need a complete date range for time-series analysis, dashboards, or forecasting. Join your fact data to the date spine to fill missing periods and avoid broken trend lines in BI tools.

What generic tests does dbt-utils add that are not in dbt Core?

+

dbt-utils extends dbt’s built-in tests with additional checks including equal_rowcount, fewer_rows_than, expression_is_true, not_empty_string, sequential_values, and equality tests to compare two relations column by column.

What is dbt-utils and why do data teams use it?

+

dbt-utils is an open-source package maintained by dbt Labs that provides reusable macros, SQL generators, and generic tests for dbt projects. Data teams use it to eliminate repetitive SQL patterns, enforce data quality standards, and write cross-database compatible code. It is the most widely installed package in the dbt community and is used in many dbt projects.

What is generate_surrogate_key and when should I use it?

+

generate_surrogate_key is a macro from dbt-utils that creates a deterministic hashed identifier from one or more columns. It is commonly used to produce stable primary or join keys when your source data does not contain a reliable unique identifier.Surrogate keys are preferred over auto-incrementing IDs because they produce the same value across development, test, and production environments, ensuring consistent joins and reproducible models.

What is the difference between surrogate_key and generate_surrogate_key?

+

surrogate_key is the legacy version of the macro and was deprecated in dbt-utils v1.0. generate_surrogate_key is the current replacement and handles nulls more precisely, distinguishing between null values and empty strings. If you are still using surrogate_key, replace it with generate_surrogate_key in your models.

Last updated onDecember 29, 2024
Mayra Peña
Mayra Peña
Author

Data Engineering | Technical Customer Success.

Solving enterprise data challenges quickly with dbt & Airflow.

Continue reading

End-to-end data analytics with Managed dbt and Airflow

Don’t let platform limitations or maintenance overhead hold you back.

Noel gomez

Subscribe to our newsletter & be a part of our Exclusive Crew!

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Datacoves Logo

Datacoves is an enterprise DataOps platform with Managed dbt and Airflow for data transformation and orchestration, as well as VS Code in the browser for development

LinkedIn X YouTube
Datacoves is snowflake ready technology badge
© 2026 Datacoves. All rights reserved.
Privacy Policy Terms of Service Sitemap

Apache, Apache Airflow, Airflow, Apache Superset, the Airflow logo, the Apache feather logo, Superset, and the Superset logo are trademarks of the Apache Software Foundation. Dbt, dbt core, dbt logo are trademarks of the dbt Labs, Inc. Airbyte, Airbyte logo are trademarks of the Airbyte, Inc. Snowflake, Snowflake logo are trademarks of the Snowflake Inc.