How to Implement Slowly Changing Dimensions in Snowflake?
Data changes all the time. A customer may change their address, an employee may move to another department, or a product may get a new category. In a data warehouse, simply updating the old value can sometimes mean losing important historical information. This is where Slowly Changing Dimensions (SCD) become useful.
If you are learning data engineering concepts through Snowflake Training in Chennai, understanding SCD is an important step because it is commonly used in real-world data warehouses and ETL projects. Snowflake provides flexible SQL features that make different SCD strategies practical to implement.
What Are Slowly Changing Dimensions?
Slowly Changing Dimensions are techniques used to manage changes in dimension data while deciding how much historical information should be preserved.
Consider a customer table:
Customer ID | Name | City
101 | Arun | Chennai
Later, Arun moves to Bangalore.
If you simply update the record, you get:
101 | Arun | Bangalore
The warehouse now knows the current city but has no easy way to determine where Arun lived previously.
An SCD strategy allows you to decide whether the old value should be overwritten, preserved, or tracked with additional records.
Common Types of SCD
There are several SCD approaches, but Type 1 and Type 2 are especially common in Snowflake data engineering projects.
SCD Type 1 – Overwrite the Existing Value
Type 1 is the simplest approach. When a value changes, you update the existing record.
For example:
Before:
101 | Arun | Chennai
After:
101 | Arun | Bangalore
The previous value is not retained.
This approach works well when historical information isn't important. For example, if a customer corrects a spelling mistake or fixes an incorrect phone number, keeping the incorrect value may not be useful.
A simple Snowflake MERGE statement can be used for this type of operation:
MERGE INTO customer_dim target
USING customer_stage source
ON target.customer_id = source.customer_id
WHEN MATCHED THEN
UPDATE SET
target.customer_name = source.customer_name,
target.city = source.city
WHEN NOT MATCHED THEN
INSERT (customer_id, customer_name, city)
VALUES (source.customer_id, source.customer_name, source.city);
The existing row is updated when a matching customer is found, while a new customer is inserted when there is no match.
SCD Type 2 – Keep the Complete History
Type 2 is more interesting because it preserves historical changes.
Instead of replacing the Chennai record, you keep it and create another record for Bangalore.
For example:
Customer ID | Name | City | Start Date | End Date | Current
101 | Arun | Chennai | 2025-01-01 | 2026-05-10 | N
101 | Arun | Bangalore | 2026-05-11 | NULL | Y
Now you can answer questions such as:
-
Where did the customer live previously?
-
When did the customer move?
-
Which city was associated with the customer during a particular period?
This makes Type 2 particularly useful for reporting and historical analysis.
Designing an SCD Type 2 Table
A typical Type 2 dimension contains a few additional columns to track history.
For example:
CREATE TABLE customer_dim (
customer_key NUMBER AUTOINCREMENT,
customer_id NUMBER,
customer_name VARCHAR,
city VARCHAR,
effective_start_date DATE,
effective_end_date DATE,
is_current BOOLEAN
);
Here, customer_id identifies the business entity, while customer_key can act as a warehouse-specific key.
The effective_start_date and effective_end_date define the period during which the record was valid.
The is_current column makes it easier to identify the latest version.
Step-by-Step SCD Type 2 Implementation
The first step is to load incoming customer data into a staging table.
Suppose the staging data contains:
101 | Arun | Bangalore
You then compare it with the current record in the dimension table.
If the customer's city hasn't changed, there is nothing to do.
If the city has changed, the existing record needs to be closed and a new version needs to be created.
First, update the existing record:
UPDATE customer_dim
SET
effective_end_date = CURRENT_DATE - 1,
is_current = FALSE
WHERE customer_id = 101
AND is_current = TRUE;
Then insert the new version:
INSERT INTO customer_dim
(
customer_id,
customer_name,
city,
effective_start_date,
effective_end_date,
is_current
)
SELECT
customer_id,
customer_name,
city,
CURRENT_DATE,
NULL,
TRUE
FROM customer_stage
WHERE customer_id = 101;
The result is two versions of the same customer, with only the latest record marked as current.
How to Detect Changes Efficiently
One of the important parts of SCD implementation is identifying whether a record has actually changed.
You don't want to create a new historical version every time the pipeline runs if the customer's information is exactly the same.
You can compare individual columns:
target.city <> source.city
For dimensions with many attributes, comparing every column manually can become difficult. In such situations, teams may create a hash value from relevant attributes and compare the hash between source and target records.
For example:
MD5(
CONCAT(
customer_name,
'|',
city
)
)
If the hash changes, the underlying attribute values have probably changed, so a new Type 2 version can be created.
Using MERGE with SCD
Snowflake's MERGE command is useful for combining insert and update logic. However, Type 2 implementations often require careful handling because a changed record generally involves closing the old version and inserting a new version.
A common design is to first identify changed records in a staging or intermediate dataset. The pipeline can then expire the existing records and insert their new versions.
This approach keeps the logic easier to test and troubleshoot than trying to put every SCD operation into one enormous SQL statement.
Choosing the Right SCD Type
The right approach depends on the business requirement.
Use Type 1 when only the latest value matters. It is straightforward and usually requires less storage.
Use Type 2 when historical reporting matters. It requires additional records and logic, but it gives analysts the ability to understand how data changed over time.
Some projects may also use hybrid approaches where certain attributes are handled as Type 1 and others as Type 2.
Best Practices for SCD in Snowflake
Before implementing SCD, clearly define the business key. Without a reliable key, it becomes difficult to identify which source record belongs to which dimension record.
It is also important to define what counts as a meaningful change. Not every column needs historical tracking.
Keep your effective dates consistent and decide how your organization wants to handle same-day changes. Also, make sure there is normally only one current record for each business key.
Finally, test the pipeline with different scenarios: new customers, unchanged customers, changed customers, duplicate source records, and multiple changes over time.
Final Thoughts
Slowly Changing Dimensions may sound complicated at first, but the idea is quite simple: decide what history your business needs and design the dimension to preserve it. Type 1 is useful when you only need the latest information, while Type 2 is a strong choice when historical tracking is important.
With hands-on SQL, data modeling, ETL workflows, and real-world practice, SCD becomes much easier to understand. Qmatrix Technologies can be a useful place to build practical Snowflake and data engineering skills through structured learning and project-based practice.
- 🌟Karadeniz Magazin
- ⚽Karadeniz Spor
- 📍 Karadeniz Şehirleri
- 📰 Karadeniz Haberler
- 🍽️✈️ GEZGİN GURME
- Karadeniz Genel
- 🏞️ Karadeniz Türizm
- 🏛️ Tarih & Kültür