0
0
dbtdata~3 mins

Why dbt-utils (surrogate_key, pivot, unpivot)? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if you could turn messy data into clean, ready-to-use tables with just a few simple commands?

The Scenario

Imagine you have a big spreadsheet with messy data. You want to create unique IDs, reshape rows into columns, or turn columns back into rows. Doing this by hand means copying, pasting, and lots of scrolling.

The Problem

Manually creating unique keys or reshaping data is slow and easy to mess up. You might miss duplicates, make typos, or spend hours repeating the same steps. It's frustrating and wastes time.

The Solution

dbt-utils offers ready-made tools like surrogate_key, pivot, and unpivot that automate these tasks. They make your data clean, organized, and ready for analysis with just a few lines of code.

Before vs After
Before
SELECT CONCAT(col1, col2) AS id FROM table -- manual key creation
-- manual pivot requires multiple CASE statements
After
SELECT dbt_utils.surrogate_key(['col1', 'col2']) AS id FROM table;
SELECT * FROM dbt_utils.pivot(...)
What It Enables

With dbt-utils, you can quickly build reliable data models that are easy to maintain and understand, unlocking faster insights.

Real Life Example

A marketing team uses surrogate_key to create unique customer IDs, then pivot sales data by month to spot trends, and unpivot campaign results for detailed analysis.

Key Takeaways

Manual data reshaping is slow and error-prone.

dbt-utils automates key creation and data pivoting tasks.

This saves time and improves data quality for analysis.