Start learning free

LESSON 24 OF 107 · DATA ENGINEERING: DATA MODELING

Changing Dimensions: Overwrite (Type 1) in Python

Practice

Dimensions change now and then: a customer moves to another city, a product gets a new name. A dimension whose rows change like this is a slowly changing dimension, and the warehouse has to decide what happens to the old value. The simplest answer is Type 1: overwrite it. The row keeps only the new value, and the old one is gone.

You overwrite with UPDATE, which changes rows already in a table: SET says which column gets which value, and WHERE says which rows. Without WHERE, every row changes. Here it renames a product:

UPDATE dim_product
SET product = 'Mug 300ml'
WHERE product = 'Mug'

The catch: the fact holds customer_id, not the city, and every report finds the city through the dimension. After an overwrite, the orders a customer placed before moving count for the new city too, so a report on February gives different numbers today than it did in March. That's right for fixing a mistake, like a city typed " cluj ", and wrong when the world really changed.

Customer 160 moves from Cluj to Rome on 2026-07-01. The starting code builds dim_customer and fact_order_lines (paid lines), and report prints the revenue of the two cities. With your UPDATE, the program prints the report before and after the move, then the check lists customer 160's orders with the city each one joins to:

Before the move:
Cluj 9800
Rome 9956
After the move:
Cluj 9512
Rome 10244
order_id | date | city | order_amount
1462 | 2026-02-21 | Rome | 117
1464 | 2026-03-04 | Rome | 18
1463 | 2026-09-28 | Rome | 100
1465 | 2026-10-04 | Rome | 53
(4 rows)

Your task: in query, write the UPDATE that sets customer 160's city to 'Rome', one clause per line, run it with con.execute, then call report("After the move:").


Assignment
Follow the Coding Tutorial to overwrite customer 160's city with an UPDATE and print the report again.


Hint
Writing the SQL into query changes nothing: only con.execute(query) runs it. Each string piece ends with a space so the clauses don't run together.


STEP 1 OF 5
    Ctrl + Enter to check

    De Model Scd1 in Python

    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free