<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Andras Lendvay on Medium]]></title>
        <description><![CDATA[Stories by Andras Lendvay on Medium]]></description>
        <link>https://medium.com/@andras.lendvay_58630?source=rss-1245a6179891------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*DXb3YvjgJ6dkGrZu</url>
            <title>Stories by Andras Lendvay on Medium</title>
            <link>https://medium.com/@andras.lendvay_58630?source=rss-1245a6179891------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 05:06:09 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@andras.lendvay_58630/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[An Analytics Engineer’s Guide to Slowly Changing Dimensions]]></title>
            <link>https://medium.com/@andras.lendvay_58630/an-analytics-engineers-guide-to-slowly-changing-dimensions-ecfbac98634c?source=rss-1245a6179891------2</link>
            <guid isPermaLink="false">https://medium.com/p/ecfbac98634c</guid>
            <category><![CDATA[data]]></category>
            <category><![CDATA[data-engineering]]></category>
            <category><![CDATA[analytics]]></category>
            <dc:creator><![CDATA[Andras Lendvay]]></dc:creator>
            <pubDate>Tue, 05 Aug 2025 16:47:30 GMT</pubDate>
            <atom:updated>2025-08-05T16:53:36.481Z</atom:updated>
            <content:encoded><![CDATA[<p>This post was created for analytics engineers who want to get into working with slowly changing dimensions.</p><h3><strong>What are slowly changing dimensions?</strong></h3><p>Slowly Changing Dimensions (SCD) are used in data warehouses as a data historization technique to track and manage changes in dimensional data. We use SCDs to maintain data accuracy and consistency when analyzing trends over time, ensuring that past records reflect the correct context.</p><p>SCD has multiple types, but Type 2 (SCD2) is the most commonly used format, because it allows full historical tracking. Let’s assume we want to report on customer data. We have a record of John, who lives in New York.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/913/0*r4h5hWgUpE8s7eJd" /></figure><p>If John decides to move to London, within our transaction database, we will most likely lose the information that he was ever living in New York.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/930/0*ljlYdD_YZ0YMiAvQ" /></figure><p>However, within our SCD2 tables, we will still retain this information. We will “cap” the old record and create a new one with infinite validity. Our Customers SCD2 table will contain:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HBX5Tb5LjvZ6Fk0LHLCcsw.png" /></figure><p>If we want to report on revenue by location, this structure ensures that sales to John from 2024 will be attributed to the city of New York. We will be able to rebuild the same report that we created years ago.</p><h3><strong>How to load SCD2 records incrementally?</strong></h3><p>Once the SCD2 tables have been created, it’s a common challenge for both data and analytics engineers to create an incremental logic for loading large SCD2 tables — either for ingestions or transformations.</p><p>Loading SCD2 tables is no different from incrementally loading any other data set. We have to find all records that have changed since the last load window, insert new records, and update existing ones. We don’t have to worry about deleted records since we never delete records in SCD2.</p><p>Therefore, there are only two reasons why a record will change: the creation or the cap. When John is registered from New York, we will load the first record</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/777/0*QqTyOjAyFrkQkzzh" /></figure><p>We will end up with one record in our target SCD2 table:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NFcbSd_LiCiba30Q9e1rxw.png" /></figure><p>When he moves to London, two things will change. The “New York” record will be capped, and the London record will be created within the time window:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/795/0*atVbHLtJwQwMBoTM" /></figure><p>We will use our increment and apply it to our target table. The “New York” record will be updated with a valid_to, and the “London” record will be inserted. We can execute the merge along the valid_from and entity primary key fields.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/796/0*8pUmXSjOnzw8x2Ig" /></figure><p>In SQL, the condition is defined as:</p><pre>where @increment_from &lt;= valid_from and valid_from &lt; @increment_to<br>or @increment_from &lt;= valid_to and valid_to &lt; @increment_to</pre><p>Where <strong>@increment_from</strong> and <strong>@increment_to</strong> define our incremental window. We load all records that started or ended in the window.</p><p>We will end up with:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*V0fMNhFVzTXRam60kiJfqQ.png" /></figure><h3><strong>How to transform SCD2 records?</strong></h3><p>While most data engineers stop at this point, analytics engineers rarely get away from joining multiple tables together. This can get a bit more complicated with SCD2 records.</p><p>Let’s say we want to analyze customer behavior over time, combining their location (from the Customers table) and subscription plan (from the Subscriptions table). John currently uses our Business plan because he switched from the Starter plan in 2024.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/874/0*XaDLlidlGdtrESWW" /></figure><p>We are going to slice the target record into SCD2 records every time a change has occurred:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/955/0*5lsfrNdKhYCq9V-E" /></figure><p>We can do this by joining the two tables and picking the overlap between the Customer and Subscription records:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/0*wv4y_AmtioZABt3d" /></figure><p>For each example, we pick the latest <strong>valid_from</strong> and the earliest <strong>valid_to</strong>. If there is no overlap between two records, we ignore them. In SQL, this is created as:</p><pre>select<br>  c.name,<br>  c.city,<br>  s.plan,<br>  greatest(c.valid_from,s.valid_from) as valid_from,<br>  least(c.valid_to, s.valid_to) as valid_to<br>from customers c<br>inner join subscriptions s<br>on c.customer_id = s.customer_id -- keys between entities<br>and greatest(c.valid_from,s.valid_from) &lt; least(c.valid_to, s.valid_to) -- SCD2 join condition</pre><p>where</p><ul><li><strong>greatest(c.valid_from,s.valid_from) </strong>is the latest <strong>valid_from</strong>,</li><li><strong>least(c.valid_to, s.valid_to)</strong> is the earliest <strong>valid_to,</strong></li><li>and <strong>greatest(c.valid_from,s.valid_from) &lt; least(c.valid_to, s.valid_to) </strong>ensures there is an overlap between the two records.</li></ul><p>We will end up with the following data:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1HDDuhcGEGgV43B4NdabKQ.png" /></figure><p><strong>What about Incremental SCD2 Joins?</strong></p><p>Using a full refresh to create SCD2 tables is easier, but sometimes we want to implement incremental logic to load larger tables faster and more cost-effectively. Therefore, we have to combine what we’ve learnt about incremental loads and SCD2 joins.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/941/0*KXiaiAH2ni01ewFI" /></figure><p>We need all changes from both the Customers and Subscriptions tables within the incremental window. However, for every change of the Customer, we need the matching Subscription record that was valid at that time, even if there was no change in the Subscription.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/941/0*6PEce94AgdYvbMEL" /></figure><p>Notice that the “Business Plan” Subscription record is needed for the two new records, even if there was no plan change during our incremental window.</p><p>In SQL, this will be defined as:</p><pre>with customers as (<br>  select<br>    customer_id,<br>    name,<br>    city,<br>    valid_from,<br>    valid_to<br>  from product.customers<br>  where<br>    valid_from &lt;= @increment_to and valid_to &gt;= @increment_from<br>    -- ^all customers records valid within the incremental window<br>), subscriptions as (<br>  select<br>    customer_id,<br>    plan,<br>    valid_from,<br>    valid_to<br>  from product.subscriptions<br>  where<br>    valid_from &lt;= @increment_to and valid_to &gt;= @increment_from<br>    -- ^all subscriptions records valid within the incremental window<br>)<br>select<br>  c.name,<br>  c.city,<br>  s.plan<br>  greatest(c.valid_from,s.valid_from) as valid_from,<br>  least(c.valid_to, s.valid_to) as valid_to,<br>from customers c<br>inner join subscriptions s<br>  on c.customer_id = s.customer_id -- keys between entities<br>  and greatest(c.valid_from,s.valid_from) &lt; least(c.valid_to, s.valid_to) -- SCD2 join condition<br>where<br>  @increment_from &lt;= greatest(c.valid_from,s.valid_from) and greatest(c.valid_from,s.valid_from) &lt; @increment_to<br>  or @increment_from &lt;= least(c.valid_to, s.valid_to) and least(c.valid_to, s.valid_to) &lt; @increment_to<br>   -- ^incremental window filter</pre><h3><strong>At what layer should I create my SCD2 tables?</strong></h3><p>You can use multiple tools or processes to create your SCD2 tables (dbt snapshots, Fivetran History Mode, CDC features of data movement tools, etc.), but you can pick different strategies based on where you want to start working with SCD2 tables.</p><h4><strong>The Lazy Data Analyst</strong></h4><p>A very simple strategy you could follow is to create your historical reporting tables (technically not an SCD, because it is denormalized) in the Gold Layer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/792/0*c3JE0OiJf-25zh6M" /></figure><p>It’s easy to execute (with tools such as dbt snapshot, maybe weekly snapshots), and you will be able to see historical reports; however, as soon as anything changes in the report logic or structure, your history is gone. This might work in a very early stage, while you are the data engineer, data analyst, and data scientist of your company, as one person.</p><h4><strong>The Hardcore Data Engineer</strong></h4><p>The only strategy that is “technologically right”. Start historization (building SCD2 tables) as early as possible.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/873/0*eou5Bm5Pzb5RreRT" /></figure><p>You create the SCD2 Tables in the Bronze layer either through dbt snapshot or data movement tools that support SCD2. You will always use your SCD2 (_history) tables through each layer. Therefore, even if you completely redo your Customers entity, you will be able to retain complete history from Bronze. However, the downside is that your Silver layer already uses SCD joins. SCD2 joins at this layer require careful logic and can increase compute cost and model complexity. You do this when you have a strong data engineering team and culture.</p><p>You’ve probably noticed the customers tables (or view) created from the customers_history table. It’s quite frequent that for simple cases of analytics, we only need the most recent record. We create this table by applying a valid_to = ‘9999–12–31’ filter.</p><h4><strong>The Reasonable Analytics Engineer</strong></h4><p>An approach that I would suggest to all data teams getting started with SCD2 is to create the first snapshots in your Silver layer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/870/0*eet-RU5iUNrdGidx" /></figure><p>This will allow you to modify all of your reports, while you don’t have to handle complex SCD2 joins for all of your entities.</p><p>At the same time, you can start snapshotting your source data in the bronze layer (maybe you get it in SCD2 already,) even if you don’t use it yet. If you would introduce a breaking change to your Silver models, you can rebuild them from the history tables using SCD2 joins. This approach combines early velocity with long-term capabilities.</p><h3><strong>Summary</strong></h3><p>Slowly Changing Dimensions Type 2 (SCD2) tables are a powerful tool for maintaining historical accuracy in dimensional data. As analytics engineers, understanding how to build, load, and transform these tables is essential for enabling reliable time-based analysis.</p><p>Here’s a quick recap:</p><p><strong>SCD2 captures history</strong> by “capping” old records and inserting new ones, rather than overwriting.</p><p><strong>Incremental loading</strong> requires detecting both <em>new</em> and <em>changed</em> records, updating their valid_to, and inserting fresh versions.</p><p><strong>Joining SCD2 tables</strong> (e.g., customers with subscriptions) involves finding overlapping validity windows using GREATEST and LEAST logic.</p><p><strong>Incremental SCD2 joins</strong> combine concepts from both incremental loading and temporal joins — often necessary for performance at scale.</p><p>You can adopt SCD2 historization at different layers:</p><ul><li><strong>Gold</strong> (quick and dirty, lossy over time)</li><li><strong>Silver</strong> (recommended: balance of flexibility and performance)</li><li><strong>Bronze</strong> (complete control, but complexity and cost are higher)</li></ul><p>Your SCD2 strategy should reflect your team’s size, skills, and maturity. Start simple, and layer in complexity only when needed. What matters most is that you never lose track of what your data <em>used to look like</em>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ecfbac98634c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>