<?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 Re: Delta Live Tables Slowly Changing Dimensions Type 2 with Joins in Get Started Discussions</title>
    <link>https://community.databricks.com/t5/get-started-discussions/delta-live-tables-slowly-changing-dimensions-type-2-with-joins/m-p/53400#M6154</link>
    <description>&lt;P&gt;Can it be because &lt;A href="https://spark.apache.org/docs/latest/api/python/reference/pyspark.sql/api/pyspark.sql.DataFrame.join.html" target="_blank" rel="noopener"&gt;the default join is `inner`&lt;/A&gt;&amp;nbsp;and that means the row must exists in both tables&lt;/P&gt;</description>
    <pubDate>Tue, 21 Nov 2023 21:46:35 GMT</pubDate>
    <dc:creator>the_real_merca</dc:creator>
    <dc:date>2023-11-21T21:46:35Z</dc:date>
    <item>
      <title>Delta Live Tables Slowly Changing Dimensions Type 2 with Joins</title>
      <link>https://community.databricks.com/t5/get-started-discussions/delta-live-tables-slowly-changing-dimensions-type-2-with-joins/m-p/51647#M6153</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I may be missing something really obvious here. The organisation I work for&lt;SPAN&gt;&amp;nbsp;has started using Delta Live Tables in Databricks for data modelling, recently. One of the dimensions I am trying to model takes data from 3 existing tables in our data lake. It also needs to be a type 2 slowly changing dimension. The tables sit in a bronze, streaming layer (we will run this from a silver streaming layer in the future, but this is a proof-of-concept). The issue I'm coming across, is that because the data is coming from 3 tables, it doesn't work if changes occur to any tables that aren't the first one (person). My current code is like this:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;@dlt.view

def dim_person():
    person_df = spark.readStream.table("raw_person") \
    .filter("person_type_id = 2") \
    .alias("person")
    
    #gets people from the person_detail table, and replaces any blank job ids with -1
    person_detail_df = spark.readStream.table("raw_person_detail") \
    .na.fill(value=-1, subset=["job_id"]) \
    .select("person_id", "job_id", "workarea", "_Operation", "_IngestedDate") \
    .alias("person_detail")
    
    jobs_df = spark.readStream.table("raw_jobs") \
    .select("job_id", "name", "_Operation", "_IngestedDate")

    #unions the jobs with a table that contains a value of 'Unknown', to join to records 
    #of -1
    jobs_full_df = jobs_df.union(spark.readStream.table("staging_data_jobs").withColumn("_IngestedDate", lit("2023-11-13").cast("date"))) \
    .alias("jobs")

    full_df = person_df.join(person_detail_df, on="person_id") \
                    .join(jobs_full_df, on="job_id") \
                    .selectExpr("person.person_id","person.first_name","person.surname","person.date_of_birth","person.postcode","jobs.name as job","person_detail.workarea","person._Operation", "person_detail._IngestedDate AS CDIngestedDate", "jobs._IngestedDate AS OCIngestedDate", "person._IngestedDate AS CUIngestedDate") \
                    .withColumn("_IngestedDate", greatest(*["CDIngestedDate", "OCIngestedDate", "CUIngestedDate"]))

    return full_df.select("person_id","first_name","surname","date_of_birth","postcode","job","workarea","_Operation","_IngestedDate")&lt;/LI-CODE&gt;&lt;LI-CODE lang="python"&gt;dlt.create_streaming_table("person")

dlt.apply_changes(
  target = "person",
  source = "dim_person",
  keys = ["person_id"],
  sequence_by = col("_IngestedDate"),
  apply_as_deletes = expr("_Operation = 1"),
  except_column_list = ["_IngestedDate"],
  stored_as_scd_type = 2
)&lt;/LI-CODE&gt;&lt;P&gt;If I run this through a pipeline, and for example, one of the job IDs has been updated in the person_details table, it will break, because the schema has changed. Is there a better way of doing SCD type 2 dimensions when joining multiple tables?&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any suggestions much appreciated.&lt;/P&gt;</description>
      <pubDate>Tue, 14 Nov 2023 16:39:00 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/delta-live-tables-slowly-changing-dimensions-type-2-with-joins/m-p/51647#M6153</guid>
      <dc:creator>HowardLJ</dc:creator>
      <dc:date>2023-11-14T16:39:00Z</dc:date>
    </item>
    <item>
      <title>Re: Delta Live Tables Slowly Changing Dimensions Type 2 with Joins</title>
      <link>https://community.databricks.com/t5/get-started-discussions/delta-live-tables-slowly-changing-dimensions-type-2-with-joins/m-p/53400#M6154</link>
      <description>&lt;P&gt;Can it be because &lt;A href="https://spark.apache.org/docs/latest/api/python/reference/pyspark.sql/api/pyspark.sql.DataFrame.join.html" target="_blank" rel="noopener"&gt;the default join is `inner`&lt;/A&gt;&amp;nbsp;and that means the row must exists in both tables&lt;/P&gt;</description>
      <pubDate>Tue, 21 Nov 2023 21:46:35 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/delta-live-tables-slowly-changing-dimensions-type-2-with-joins/m-p/53400#M6154</guid>
      <dc:creator>the_real_merca</dc:creator>
      <dc:date>2023-11-21T21:46:35Z</dc:date>
    </item>
  </channel>
</rss>

