<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic SCD Type 2 Using MERGE in delta in Data Engineering</title>
    <link>https://community.fabric.microsoft.com/t5/Data-Engineering/SCD-Type-2-Using-MERGE-in-delta/m-p/4384567#M6609</link>
    <description>&lt;P&gt;Whats the best practice when I want to both update AND insert a record into a target table in one operation ensureing atomisity?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;newRecord&lt;/P&gt;&lt;P&gt;ID, Product, Color&lt;/P&gt;&lt;P&gt;123, A, Blue&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;myTable&lt;/P&gt;&lt;P&gt;ID, Product, Color, is_current&lt;/P&gt;&lt;P&gt;123, A, Yellow, TRUE&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I basically want to do an atomic operation that gives the following result in myTable&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID, Product, Color, is_current&lt;/P&gt;&lt;P&gt;123, A, Yellow, FALSE&lt;/P&gt;&lt;P&gt;123, A, Blue, TRUE&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I basically want to do a .whenMatchedUpdate AND a&amp;nbsp;.whenMatchedInsert in a MERGE statement, but I know thats not supported.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 28 Jan 2025 14:31:16 GMT</pubDate>
    <dc:creator>AndersASorensen</dc:creator>
    <dc:date>2025-01-28T14:31:16Z</dc:date>
    <item>
      <title>SCD Type 2 Using MERGE in delta</title>
      <link>https://community.fabric.microsoft.com/t5/Data-Engineering/SCD-Type-2-Using-MERGE-in-delta/m-p/4384567#M6609</link>
      <description>&lt;P&gt;Whats the best practice when I want to both update AND insert a record into a target table in one operation ensureing atomisity?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;newRecord&lt;/P&gt;&lt;P&gt;ID, Product, Color&lt;/P&gt;&lt;P&gt;123, A, Blue&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;myTable&lt;/P&gt;&lt;P&gt;ID, Product, Color, is_current&lt;/P&gt;&lt;P&gt;123, A, Yellow, TRUE&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I basically want to do an atomic operation that gives the following result in myTable&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID, Product, Color, is_current&lt;/P&gt;&lt;P&gt;123, A, Yellow, FALSE&lt;/P&gt;&lt;P&gt;123, A, Blue, TRUE&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I basically want to do a .whenMatchedUpdate AND a&amp;nbsp;.whenMatchedInsert in a MERGE statement, but I know thats not supported.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jan 2025 14:31:16 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Data-Engineering/SCD-Type-2-Using-MERGE-in-delta/m-p/4384567#M6609</guid>
      <dc:creator>AndersASorensen</dc:creator>
      <dc:date>2025-01-28T14:31:16Z</dc:date>
    </item>
    <item>
      <title>Re: SCD Type 2 Using MERGE in delta</title>
      <link>https://community.fabric.microsoft.com/t5/Data-Engineering/SCD-Type-2-Using-MERGE-in-delta/m-p/4384708#M6612</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="423657" data-lia-user-login="AndersASorensen" class="lia-mention lia-mention-user"&gt;AndersASorensen&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;The `MERGE` statement is not natively supported in Microsoft Fabric’s Data Warehouse as of now. Users need to rely on PySpark or SQL workarounds for similar functionality&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;from delta.tables import DeltaTable&lt;/P&gt;&lt;P&gt;# Define source and target&lt;BR /&gt;target_table = DeltaTable.forName(spark, "Lakehouse.TargetTable")&lt;BR /&gt;source_df = spark.read.table("Lakehouse.SourceTable")&lt;/P&gt;&lt;P&gt;# Perform the merge&lt;BR /&gt;target_table.alias("target").merge(&lt;BR /&gt;source=source_df.alias("source"),&lt;BR /&gt;condition="target.key = source.key"&lt;BR /&gt;).whenMatchedUpdateAll().whenNotMatchedInsertAll().execute()&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://aventius.co.uk/2024/03/28/microsoft-fabric-using-pyspark-to-dynamically-merge-data-into-many-tables" target="_blank" rel="nofollow noopener noreferrer"&gt;https://aventius.co.uk/2024/03/28/microsoft-fabric-using-pyspark-to-dynamically-merge-data-into-many...&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Currently, the `MERGE` statement is not supported in Fabric’s Data Warehouses. As a workaround:&lt;BR /&gt;• Use a combination of `ROW_NUMBER()` for deduplication and `INSERT`/`UPDATE` statements to achieve similar functionality:&lt;BR /&gt;&lt;BR /&gt;WITH DeduplicatedSource AS (&lt;BR /&gt;SELECT *, ROW_NUMBER() OVER (PARTITION BY ID ORDER BY LOAD_DATE DESC) AS row_num&lt;BR /&gt;FROM SourceTable&lt;BR /&gt;WHERE row_num = 1&lt;BR /&gt;)&lt;BR /&gt;-- Merge logic&lt;BR /&gt;MERGE INTO TargetTable AS target&lt;BR /&gt;USING DeduplicatedSource AS source&lt;BR /&gt;ON target.ID = source.ID&lt;BR /&gt;WHEN MATCHED THEN UPDATE SET target.Column = source.Column&lt;BR /&gt;WHEN NOT MATCHED THEN INSERT (Column) VALUES (source.Column);&lt;BR /&gt;&lt;BR /&gt;Hope this helps&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jan 2025 15:52:30 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Data-Engineering/SCD-Type-2-Using-MERGE-in-delta/m-p/4384708#M6612</guid>
      <dc:creator>nilendraFabric</dc:creator>
      <dc:date>2025-01-28T15:52:30Z</dc:date>
    </item>
    <item>
      <title>Re: SCD Type 2 Using MERGE in delta</title>
      <link>https://community.fabric.microsoft.com/t5/Data-Engineering/SCD-Type-2-Using-MERGE-in-delta/m-p/4384768#M6614</link>
      <description>&lt;P&gt;I am happy to use pyspark, and I want to do it on a lakehouse, not a DW.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, your&amp;nbsp;&lt;SPAN&gt;.whenMatchedUpdateAll().whenNotMatchedInsertAll().execute() suggestion will not work as this will only update the matched row. &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Yes, I would like it to update a is_current column when matched, but it will not insert the record as a new row too.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jan 2025 16:23:52 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Data-Engineering/SCD-Type-2-Using-MERGE-in-delta/m-p/4384768#M6614</guid>
      <dc:creator>AndersASorensen</dc:creator>
      <dc:date>2025-01-28T16:23:52Z</dc:date>
    </item>
    <item>
      <title>Re: SCD Type 2 Using MERGE in delta</title>
      <link>https://community.fabric.microsoft.com/t5/Data-Engineering/SCD-Type-2-Using-MERGE-in-delta/m-p/4385173#M6625</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="423657" data-lia-user-login="AndersASorensen" class="lia-mention lia-mention-user"&gt;AndersASorensen&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;give it a try&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;from pyspark.sql.functions import *&lt;BR /&gt;from delta.tables import *&lt;/P&gt;&lt;P&gt;# Assuming 'target_table' is your existing Delta table&lt;BR /&gt;target_table = DeltaTable.forName(spark, "target_table")&lt;/P&gt;&lt;P&gt;# 'source_df' is your new data&lt;BR /&gt;merge_condition = "target.natural_key = source.natural_key"&lt;/P&gt;&lt;P&gt;(target_table.alias("target")&lt;BR /&gt;.merge(source_df.alias("source"), merge_condition)&lt;BR /&gt;.whenMatchedUpdate(set={&lt;BR /&gt;"attribute1": "source.attribute1",&lt;BR /&gt;"attribute2": "source.attribute2",&lt;BR /&gt;"is_current": lit(True),&lt;BR /&gt;"update_date": current_date()&lt;BR /&gt;})&lt;BR /&gt;.whenNotMatchedInsert(values={&lt;BR /&gt;"natural_key": "source.natural_key",&lt;BR /&gt;"attribute1": "source.attribute1",&lt;BR /&gt;"attribute2": "source.attribute2",&lt;BR /&gt;"is_current": lit(True),&lt;BR /&gt;"update_date": current_date()&lt;BR /&gt;})&lt;BR /&gt;.execute())&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://iterationinsights.com/article/implementing-a-hybrid-type-1-and-2-slowly-changing-dimension-in-fabric" target="_blank"&gt;https://iterationinsights.com/article/implementing-a-hybrid-type-1-and-2-slowly-changing-dimension-in-fabric&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope this helps.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please accept the answer if this works.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jan 2025 21:28:32 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Data-Engineering/SCD-Type-2-Using-MERGE-in-delta/m-p/4385173#M6625</guid>
      <dc:creator>nilendraFabric</dc:creator>
      <dc:date>2025-01-28T21:28:32Z</dc:date>
    </item>
    <item>
      <title>Re: SCD Type 2 Using MERGE in delta</title>
      <link>https://community.fabric.microsoft.com/t5/Data-Engineering/SCD-Type-2-Using-MERGE-in-delta/m-p/4386082#M6662</link>
      <description>&lt;P&gt;Hi again &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code provided won't work, but the article you linked to had a working solution &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jan 2025 12:24:34 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Data-Engineering/SCD-Type-2-Using-MERGE-in-delta/m-p/4386082#M6662</guid>
      <dc:creator>AndersASorensen</dc:creator>
      <dc:date>2025-01-29T12:24:34Z</dc:date>
    </item>
    <item>
      <title>Re: SCD Type 2 Using MERGE in delta</title>
      <link>https://community.fabric.microsoft.com/t5/Data-Engineering/SCD-Type-2-Using-MERGE-in-delta/m-p/4790644#M11586</link>
      <description>&lt;P&gt;This comment saved my life. Thank you&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="423657" data-lia-user-login="AndersASorensen" class="lia-mention lia-mention-user"&gt;AndersASorensen&lt;/a&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Aug 2025 19:41:06 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Data-Engineering/SCD-Type-2-Using-MERGE-in-delta/m-p/4790644#M11586</guid>
      <dc:creator>ricauduro</dc:creator>
      <dc:date>2025-08-07T19:41:06Z</dc:date>
    </item>
  </channel>
</rss>

