Slowly Changing Dimensions (SCD Type 2) with dbt
📖 Scenario: You work as a data analyst for an online store. The store keeps track of customer information like their city and membership level. Sometimes customers move to a new city or change their membership level. You want to keep a history of these changes in your data warehouse.This is called Slowly Changing Dimensions Type 2 (SCD Type 2). It means you keep old records and add new records when customer info changes.
🎯 Goal: Build a dbt model that implements SCD Type 2 for a customers table. Your model will detect changes in customer city or membership level and keep historical records with start and end dates.
📋 What You'll Learn
Create a source table called
raw_customers with customer_id, city, membership_level, and effective_date columns.Create a variable
current_date to represent the current processing date.Write a dbt model SQL query that compares new data with existing data to detect changes in city or membership_level.
Output the full SCD Type 2 table with columns: customer_id, city, membership_level, start_date, end_date, and is_current.
💡 Why This Matters
🌍 Real World
SCD Type 2 is used in data warehouses to keep full history of changes in important data like customers, products, or employees.
💼 Career
Data engineers and analysts use SCD Type 2 to build reliable historical datasets for reporting and analysis.
Progress0 / 4 steps