Using dbt-utils Macros: surrogate_key, pivot, and unpivot
📖 Scenario: You work as a data analyst in a retail company. You have sales data with customer names, product categories, and sales amounts. You want to create a unique surrogate key for each sale, then transform the data to see sales by category in columns (pivot), and finally convert it back to a long format (unpivot) for detailed analysis.
🎯 Goal: Build a dbt model that uses dbt-utils.surrogate_key to create unique keys, dbt-utils.pivot to reshape data from long to wide format, and dbt-utils.unpivot to reshape data back from wide to long format.
📋 What You'll Learn
Create a source table with sales data including
customer_name, category, and sales_amount.Use
dbt-utils.surrogate_key macro to generate a unique sale_id for each row.Use
dbt-utils.pivot macro to pivot sales amounts by category.Use
dbt-utils.unpivot macro to unpivot the pivoted data back to long format.💡 Why This Matters
🌍 Real World
Retail and sales data often need reshaping to analyze sales by category or customer. Surrogate keys help uniquely identify transactions.
💼 Career
Data analysts and engineers use dbt and dbt-utils macros to clean, transform, and prepare data efficiently for reporting and analytics.
Progress0 / 4 steps