For the complete documentation index, see llms.txt. This page is also available as Markdown.

SCDs

Slowly Changing Dimensions ..

Workshop - Slowly changing dimensions (SCDs)

Dimension Lookup

Workshop files

Download the following files.

Keep the filenames unchanged.

Save them in your workshop folder.

Open

Create a new transformation

Use any of these options to open a new transformation tab:

  • Select File > New > Transformation

  • Use Ctrl+N (Windows/Linux) or Cmd+N (macOS)

tr_scd

Transformation

SCD table

First step in these series of workshops, is to create a simple DIM_SCD table that has the required fields to illustrate a Type 1 change - just overwrite the value - and Type 2 - where you need to record when any change in the value occurs.

  1. Start PDI.

  1. In your DB management tool, execute the following statement.

DIM_SCD

Data grid

This step allows you to enter a static list of rows in a grid. This is usually done for testing, reference or demo purposes.

  1. Drag the Data Grid step onto the canvas.

  2. Open the Data Grid properties dialog box.

  3. Ensure the following details are configured, as outlined below:

Data grid - meta
  1. On the Data tab enter the following values:

Data grid - data

Get system info

The Get System Info step retrieves information from the Kettle environment.

This step generates a single row with the fields containing the requested information. It also accepts input rows.

  1. Drag the Get System Info step onto the canvas.

  2. Open the Get System Info properties dialog box.

  3. Ensure the following details are configured, as outlined below:

Get system info

Dimension lookup/update

The Dimension Lookup/Update step allows you to implement Ralph Kimball's slowly changing dimension for both types: Type I (update) and Type II (insert) together with some additional functions. Not only can you use this step to update a dimension table, it may also be used to look up values in a dimension.

  1. Drag the Dimension lookup/update step onto the canvas.

  2. Open the Dimension lookup/update properties dialog box.

  3. Ensure the following details are configured, as outlined below:

  • Connection: your sampledata connection

  • Target table: DIM_SCD

  • Technical key field: TK

  • Version field: version

  • Lookup key: id

You will switch between Lookup mode and Update mode in the workflows below.

Lookup

This step is rarely used for lookups because specific steps exist. However, it does illustrate how referential integrity is maintained.

  1. Add ‘London’ to the Data Grid.

Data grid - lookup
Configure fields
  1. Ensure that the Dimension Lookup / Update is set to: Lookup Mode.

set lookup mode

Sets the step to Update Mode with lookup keys: id

There’s no point adding the last_update field because we’re dealing with Type 1 changes.

  1. Click on the Fields tab.

Set city to string

Run the transformation

Preview the output from Dimension lookup/update.

  1. Click the Run button in the Canvas Toolbar.

  2. Preview data for the Dimension lookup/update step.

Preview data

Why does London have a TK 0 and a value of NULL?

To maintain referential integrity, the record is assigned a TK 0, and as it doesn’t exist in the database, the value returned is NULL. As this is a lookup, no record is written to the database table.

The Dimension Lookup/Update step allows you to implement Ralph Kimball's slowly changing dimension for both types:

Type 1

Overwriting the old value. In this method, no history of dimension changes is kept in the database. The old dimension value is simply overwritten with the new one. This type is easy to maintain and is often used for data where changes are caused by processing corrections (for example, removal of special characters, correcting spelling errors).

  1. Open the Dimension Lookup / Update properties dialog box.

  2. Ensure the following details are configured, as outlined below:

Type 1
  1. Click on the Get Fields button.

This will add the key fields used in the Lookup.

The keys that map dimension table rows to stream rows are: id

  1. Click on the Fields tab and map the stream name field to the dimension name field and ensure:

Field mapping and set database strategy

There are several options available to insert / update the dimension record (row).

Insert:

This option implements a Type I & II slowly changing dimension policy. If the difference is detected for one or more mappings that have the Insert option, then a row is added to the dimension table.

Update:

This option simply updates the matched row. It can be used to implement a Type I slowly changing dimension.

Punch through:

The punch through option also performs an update. But instead of only updating the matched dimension row, it will update all versions of the row in a Type II slowly changing dimension.

Date of last insert or update (without stream field as source):

Use this option to let the step automatically maintain a date field that records the date of the insert or update using the system date field as source.

Date of last insert (without stream field as source):

Use this option to let the step automatically maintain a date field that records the date of the last insert using the system date field as source.

Date of last update (without stream field as source):

Use this option to let the step automatically maintain a date field that records the date of the last update using the system date field as source.

Last version (without stream field as source):

Use this option to let the step automatically maintain a flag that indicates if the row is the last version.


  1. Click the Run button in the Canvas Toolbar.

  2. Click on the Preview tab:

Preview data
  1. Examine the table in your SQL Query Tool:

DIM_SCD - view data

So what's happening behind TK 0?

Kettle automatically inserts an additional record with a technical key of value 0 (for default or unknown values). This will only happen in the first execution. Below this record, you find the one record (London) from our sample dataset.

In update mode (update option is enabled) the step first performs a lookup of the dimension entry. The result of the lookup is different though. Not only the technical key is retrieved from the query, but also the dimension attribute fields. A field-by-field comparison then follows. The result can be one of the following situations:

  • The record was not found, we insert a new record in the table.

  • The record was found and any of the following is true:

  • One or more attributes were different and had an "Insert" (Kimball Type II) setting: A new dimension record version is inserted.

  • One or more attributes were different and had a "Punch through" (Kimball Type I) setting: These attributes in all the dimension record versions are updated.

  • One or more attributes were different and had an "Update" setting: These attributes in the last dimension record version are updated.

  • All the attributes (fields) were identical: No updates or insertions are performed.

If you mix Insert, Punch Through and Update options in this step, this algorithm acts like a Hybrid Slowly Changing Dimension. (it is no longer just Type I or II, it is a combination)

Try different scenarios

  1. Double-click on the Data Grid step.

  2. Add the following City: Madrid

data Grid - Madrid
  1. Double-click on the Dimension lookup/update step.

  2. Click on the Fields tab and set the following:

Field mapping and set database operation
  1. Execute the transformation and view the results.

DIM_SCD update

Note that if the record does not exist (Madrid), then it gets inserted. Both records are updated with the same last_update timestamp.

The Dimension Lookup/Update step allows you to implement Ralph Kimball's slowly changing dimension for both types:

Type 2

Creating a new additional record. In this methodology, all history of dimension changes are kept in the database. You capture attribute change by adding a new row with a new surrogate key to the dimension table. Both the prior and new rows contain as attributes the natural key (or another durable identifier). Also 'effective date' and 'current indicator' columns are used in this method. There could be only one record with current indicator set to 'Y'. For 'effective date' columns, i.e. start_date and end_date, the end_date for current record usually is set to value 9999-12-31. Introducing changes to the dimensional model in Type 2 could be very expensive database operation so it is not recommended to use it in dimensions where a new attribute could be added in the future.

  1. Double-click on the Data Grid step.

  2. Add the following City: Pariss (intentionally misspelled)

Data grid - Pariss
  1. Keep the Fields strategy the same:

Field mapping and set database strategy

RUN Transformation

  1. Click the Run button in the Canvas Toolbar.

  2. Click on the Preview tab:

Preview data
  1. Examine the table in your SQL Query Tool:

  2. Execute the transformation and view the results.

DIM_SCD - view data

As expected the Pariss record is inserted. Notice that the natural primary keys (id) are the same as the TK (Technical Key) and we're on version 1 for each record.

Try out different Strategies

The workflows below will give you an idea of the different strategies that can be implemented.

Any records that are changed are:

  • Archived with date_from to date_to timestamp.

  • Record is assigned Version 1. New record Version 2

  • Note the Keys.

  1. Double-click on the Data Grid step.

  2. Edit the Pariss value to: Paris

Data grid - edit Paris
  1. Double-click on the Dimension lookup/update step.

  2. Click on the Fields tab and set the following:

Field mapping and set database strategy

RUN the Transformation

  1. Click the Run button in the Canvas Toolbar.

  2. Examine the table in your SQL Query Tool:.

DIM_SCD - view data
  • A new record has been inserted, with the value ‘Paris’, with an updated date_from and last_update timestamp, and version.

  • Notice that the last_update field has also been updated for the other records.

Punch through:

The punch through option also performs an update. But instead of only updating the matched dimension row, it will update all versions of the row in a Type II slowly changing dimension.

So let's update all versions to Paris.

  1. Let's start with a clean table to clearly illustrate the results. Truncate the table.

  1. Repeat the workflow above and check the results.

Preview data
  1. Double-click on the Data Grid step.

  2. Edit the Pariss value to: Paris

    Data grid - edit Paris
  3. Double-click on the Dimension lookup/update step.

  4. Click on the Fields tab and set the following:

Field mapping and set database strategy

RUN the Transformation

  1. Click the Run button in the Canvas Toolbar.

  2. Examine the table in your SQL Query Tool:

DIM_SCD - view data

Punch through

  1. Edit the Fields tab in the Dimension Lookup/update step to Punch through:

Field mapping and set database strategy

RUN the Transformation

  1. Click the Run button in the Canvas Toolbar.

  2. Examine the table in your SQL Query Tool:

DIM_SCD - view data

What happened

Remember a Punch through updates the fields where the records match ..

  • All the records have a last_update field .. so this will be updated

  • As both our Pariss & Paris records have a matching key id=3 then the archived record will be updated with the current city value .. Paris

Last updated

Was this helpful?