Get the essential data observability guide
Download this guide to learn:
What is data observability?
4 pillars of data observability
How to evaluate platforms
Common mistakes to avoid
The ROI of data observability
Unlock now
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Sign up for a free data observability workshop today.
Assess your company's data health and learn how to start monitoring your entire data stack.
Book free workshop
Sign up for news, updates, and events
Subscribe for free
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Getting started with Data Observability Guide

Make a plan to implement data observability across your company’s entire data stack

Download for free
Book a data observability workshop with an expert.

Assess your company's data health and learn how to start monitoring your entire data stack.

Book free workshop

Guide to dbt Data Quality Checks

and
May 17, 2023

Co-founder / Data and ML

May 17, 2023
Guide to dbt Data Quality Checks

As a data engineer or analytics engineer, you’re responsible for maintaining data quality to ensure that stakeholders trust your data. Whether your stakeholders use data for operational purposes, decision-making, or machine learning models, inaccurate data can cause costly errors and erode trust.

This is where dbt (data build tool) comes in. dbt is a widely adopted open-source command-line tool that enables data teams to orchestrate data transformations and check data quality. In this guide, we’ll dive into how you can leverage dbt for data quality checks to maintain data accuracy and trust.

What are dbt Data Quality Checks?

Ddbt (data build tool) is a popular open-source command-line tool written in Python. With dbt, data teams can create, test, and deploy complex data transformations. dbt is specifically designed to help improve data quality as well. In addition to its transformation capabilities, dbt features some built-in data quality checks. These checks help the data teams ensure the accuracy and consistency of their data.

dbt data quality checks with dbt are a collection of tests that allow you to verify data accuracy across your entire data processing pipeline. These data checks are an essential step towards ensuring data accuracy and reliability. With dbt data quality checks, you can confirm that your data is accurate before it is used for downstream analysis.

Why Use dbt Data Quality Checks?

Using dbt data quality checks can provide a number of benefits for data teams, including:

1. Increased confidence in data accuracy: By running automated tests on your data, you can have greater confidence in the accuracy and completeness of that data.

2. Early detection of data quality issues: With dbt data quality checks in place, you can catch issues early in the data pipeline, before they have a chance to cause problems downstream.

3. Faster resolution of data quality issues: With automated tests in place, you can quickly identify and resolve data quality issues, allowing your team to spend time on more strategic tasks.

Define Metrics

The first step is to define the metrics that matter to you for each model. For instance, you might define metrics such as completeness, accuracy, and consistency. These metrics will help you ensure your data meets the essential requirements.

After you've identified your metrics, you can write SQL tests in dbt to validate them.

Implementing dbt Data Quality Checks

Implementing dbt data quality checks is a relatively straightforward process. Here are the steps to follow:

1. Identify the data that needs to be tested: Start by identifying the tables or views that you want to test for data quality.

2. Define your testing criteria: Using SQL queries, define the tests that you want to run on your data. These may include tests for missing values, data types, or completeness.

3. Set up your dbt project: Configure your dbt project to run the data quality checks that you have defined.

4. Run your tests: With your checks in place, run them manually or on a schedule to ensure that your data meets your quality standards.

Setting up dbt for Data Quality Checks

Before we dive into how to set up dbt for data quality checks, let's first ensure that you have dbt installed on your machine. If you need help getting started with dbt installation, check out the installation guide in the dbt documentation.

Once you have dbt installed, you can get started with adding data quality checks into your dbt project. To set up dbt data quality checks, you'll use the `assert_*` functions that are provided by the dbt package.

Here is an example of a simple data quality check in dbt:

```sql

{% macro unique_key_check(model, column) %}

SELECT

 CASE

   WHEN COUNT(DISTINCT {{column}}) = COUNT(*)

   

   THEN 1

   

   ELSE 0

   

 END AS is_unique_key

 

FROM {{model}}

{% endmacro %}

```

This macro checks whether a given column in a given table is a unique key or not. If the column is a unique key, it returns a value of 1; otherwise, it returns a value of 0.

For example:

```sql

{% test %}

SELECT COUNT(*) FROM {{ ref('my_model') }}

WHERE important_field IS NULL;

{% endtest %}

```

This test code checks whether the `important_field` column of your `my_model` table contains any null values.

This is just one example of a data quality check that can be performed in dbt. There are many other checks that can be performed, such as checking for null values, checking for duplicates, and checking for data range.

```sql

-- an example dbt model using data quality checks

{{ config(materialized='table') }}

{{ sql("

   select *

   from orders

   where order_date > '2020-01-01'

") }}

-- add data quality checks

{{ assertTableRowCount(table='{{ this }}', schema='{{ ref('etl','schema')

   }}', count=1000) }}

{{ assertMin(column='order_date', table='{{ this }}', schema='{{ ref('etl'

   ,'schema') }}', value='2020-01-01') }}

```

As you can see, dbt data quality checks involve the use of functions like `assertTableRowCount` and `assertMin` to verify that data quality checks are being met.

Use Pre- and Post- Hook Functions

Another feature of dbt that is useful for quality checks is pre- and post-hooks. These features allow you to run SQL code before or after building a model. You can use pre- and post-hooks to set up temporary tables or clean your data before running tests.

Here's an example:

```sql

-- my_model.sql

-- This is the main SQL code for my_model

-- pre-hook.sql

-- This code will execute before the main SQL code for my_model is executed

CREATE TEMP TABLE tmp_my_model AS (

   SELECT ...

)

-- post-hook.sql

-- This code will execute after the main SQL code for my_model is executed

DELETE FROM tmp_my_model WHERE important_field IS NULL;

```

Pre- and post-hooks can be incredibly powerful for building robust data quality checks.

Other Useful Tools for Maintaining Data Quality

While dbt is a great tool for ensuring data quality checks, there are other tools and technologies that you can use to maintain data quality. Some of these tools include:

- Great Expectations: An open-source library that allows you to define, test, and document data quality expectations.

- Airflow: An open-source platform to programmatically author, schedule, and monitor workflows.

- Snowflake: A cloud-based data platform that provides a data warehouse, data lake, and data exchange.

By leveraging these tools in addition to dbt, you can create a robust data infrastructure that ensures data accuracy and reliability.

Conclusion

Data quality is essential for any data-driven organization. By using dbt data quality checks, you can verify data accuracy and reliability across your entire data processing pipeline. Additionally, by leveraging other tools and technologies like Great Expectations, Airflow, and Snowflake, you can create a data infrastructure that ensures data accuracy and reliability. If you're not currently using data quality checks in your data processing pipeline, start today and gain stakeholder trust in your data.

We’re hard at work helping you improve trust in your data in less time than ever. We promise to send a maximum of 1 update email per week.

Your email
No items found.
Ensure trust in data

Start monitoring your data in minutes.

Connect your warehouse and start generating a baseline in less than 10 minutes. Start for free, no credit-card required.