Observed since 20 Oct, 17:47 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
Slowly Changing Dimensions implemenation with Databricks Delta Lake
Slowly Changing Dimensions (SCD) are dimensions which change over time and in Data Warehuse we need to track the changes of the attributes keep the accuracy of the report.
And typically there are three types of SCD
For example we have a dataset
| ShortName | Fruit | Color | Price |
|---|---|---|---|
| FA | Fiji Apple | Red | 3.6 |
| BN | Banana | Yellow | 1 |
| GG | Green Grape | Green | 2 |
| RG | Red Grape | Red | 2 |
If we change the price of "Fiji Apple" into 3.5, the dataset will be
with SCD1:
| ShortName | Fruit | Color | Price |
|---|---|---|---|
| FA | Fiji Apple | Red | 3.5 |
| BN | Banana | Yellow | 1 |
| GG | Green Grape | Green | 2 |
| RG | Red Grape | Red | 2 |
with SCD2:
| ShortName | Fruit | Color | Price | is_last |
|---|---|---|---|---|
| FA | Fiji Apple | Red | 3.5 | Y |
| FA | Fiji Apple | Red | 3.6 | N |
| BN | Banana | Yellow | 1 | Y |
| GG | Green Grape | Green | 2 | Y |
| RG | Red Grape | Red | 2 | Y |
with SCD3:
| ShortName | Fruit | Color | Price | Color_old | Price_old |
|---|---|---|---|---|---|
| FA | Fiji Apple | Red | 3.5 | Red | 3.6 |
| BN | Banana | Yellow | 1 | NULL | NULL |
| GG | Green Grape | Green | 2 | NULL | NULL |
| RG | Red Grape | Red | 2 | NULL | NULL |
In this repository, there are implementations of SCD1, SCD2 and SCD3 in python and Databricks Delta Lake.
SCD1(df, target_table_name, target_partition_keys, key_cols, current_time):
Parameters:
Here is the code to show an example of SCD1
from pyspark.sql import functions as F
from pyspark.sql import DataFrame
import datetime
# create sample dataset
df1 = spark.createDataFrame([('FA', 'Fiji Apple', 'Red', 3.5)
,('BN', 'Banana', 'Yellow', 1.0)
,('GG', 'Green Grape', 'Green', 2.0)
,('RG', 'Red Grape', 'Red', 2.0)],
['ShortName','Fruit', 'Color', 'Price'])
# prepare parameters
current_time = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
target_partition_keys = ['ShortName']
key_cols = "ShortName,Fruit"
target_table_name_scd1 = 'default.table_scd1'
# call the SCD1 function
SCD1(df1, target_table_name_scd1, target_partition_keys, key_cols, current_time)
# display the result
display(spark.sql(f"select * from {target_table_name_scd1}"))

Change the price of "Fiji Apple" into 3.5 and run SCD1 again
df2 = spark.createDataFrame([('FA', 'Fiji Apple', 'Red', 3.6)
,('BN', 'Banana', 'Yellow', 1.0)
,('GG', 'Green Grape', 'Green', 2.0)
,('RG', 'Red Grape', 'Red', 2.0)],
['ShortName','Fruit', 'Color', 'Price'])
# call the SCD1 function again
SCD1(df2, target_table_name_scd1, target_partition_keys, key_cols, current_time)
display(spark.sql(f"select * from {target_table_name_scd1}"))

SCD2(df, target_table_name, target_partition_keys, key_cols, current_time):
Parameters:
Here is the code to show an example of SCD2
from pyspark.sql import functions as F
from pyspark.sql import DataFrame
import datetime
# create sample dataset
df1 = spark.createDataFrame([('FA', 'Fiji Apple', 'Red', 3.5)
,('BN', 'Banana', 'Yellow', 1.0)
,('GG', 'Green Grape', 'Green', 2.0)
,('RG', 'Red Grape', 'Red', 2.0)],
['ShortName','Fruit', 'Color', 'Price'])
# prepare parameters
current_time = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
target_partition_keys = ['ShortName']
key_cols = "ShortName,Fruit"
target_table_name_scd1 = 'default.table_scd2'
# call the SCD1 function
SCD2(df1, target_table_name_scd1, target_partition_keys, key_cols, current_time)
# display the result
display(spark.sql(f"select * from {target_table_name_scd2}"))

Change the price of "Fiji Apple" into 3.5 and run SCD2 again
df2 = spark.createDataFrame([('FA', 'Fiji Apple', 'Red', 3.6)
,('BN', 'Banana', 'Yellow', 1.0)
,('GG', 'Green Grape', 'Green', 2.0)
,('RG', 'Red Grape', 'Red', 2.0)],
['ShortName','Fruit', 'Color', 'Price'])
# call the SCD1 function again
SCD2(df2, target_table_name_scd2, target_partition_keys, key_cols, current_time)
display(spark.sql(f"select * from {target_table_name_scd2}"))

SCD3(df, target_table_name, target_partition_keys, key_cols, current_time):
Parameters:
Here is the code to show an example of SCD3
from pyspark.sql import functions as F
from pyspark.sql import DataFrame
import datetime
# create sample dataset
df1 = spark.createDataFrame([('FA', 'Fiji Apple', 'Red', 3.5)
,('BN', 'Banana', 'Yellow', 1.0)
,('GG', 'Green Grape', 'Green', 2.0)
,('RG', 'Red Grape', 'Red', 2.0)],
['ShortName','Fruit', 'Color', 'Price'])
# prepare parameters
current_time = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
target_partition_keys = ['ShortName']
key_cols = "ShortName,Fruit"
target_table_name_scd1 = 'default.table_scd3'
# call the SCD1 function
SCD3(df1, target_table_name_scd3, target_partition_keys, key_cols, current_time)
# display the result
display(spark.sql(f"select * from {target_table_name_scd3}"))

Change the price of "Fiji Apple" into 3.5 and run SCD3 again
df2 = spark.createDataFrame([('FA', 'Fiji Apple', 'Red', 3.6)
,('BN', 'Banana', 'Yellow', 1.0)
,('GG', 'Green Grape', 'Green', 2.0)
,('RG', 'Red Grape', 'Red', 2.0)],
['ShortName','Fruit', 'Color', 'Price'])
# call the SCD1 function again
SCD3(df2, target_table_name_scd3, target_partition_keys, key_cols, current_time)
display(spark.sql(f"select * from {target_table_name_scd3}"))

Download counts and popularity will appear here as the collector receives them.
| Version | Published | Direct dependencies | Open version |
|---|---|---|---|
| 0.0.1 | 9 Aug 2021 | 0 |
This describes our collected data; it is not a security assessment.
Shared repository data · GitHub
Public sponsor counts have not been collected. These signals belong to the repository owner.
github · 1 observations shown. Missing days are unknown.
| Date / source period | Value | Observed at |
|---|---|---|
| 2026-10-20 | 3 | 2026-10-20T19:44:01.4652587Z |
Observed since 20 Oct, 17:47 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
package:0be4db50fb02f3525ec5fd605727b8bf1e382b9be5faeef96ea45298c08e3baa