<?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: Add CreatedDate to Delta Live Table in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/add-createddate-to-delta-live-table/m-p/101380#M40645</link>
    <description>&lt;P&gt;Hi Mounika,&lt;/P&gt;&lt;P&gt;Why would defining the&amp;nbsp;&lt;SPAN&gt;CreatedDate in the schema automatically mean "the column will be populated&amp;nbsp;with the current timestamp when a new record is inserted"?&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Sun, 08 Dec 2024 16:22:52 GMT</pubDate>
    <dc:creator>tommyhmt</dc:creator>
    <dc:date>2024-12-08T16:22:52Z</dc:date>
    <item>
      <title>Add CreatedDate to Delta Live Table</title>
      <link>https://community.databricks.com/t5/data-engineering/add-createddate-to-delta-live-table/m-p/83144#M36852</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;I have a very simple DLT set up using the following code:&lt;/P&gt;&lt;PRE&gt;@dlt.view(
  name="view_name",
  comment="comments"
)
def vw_DLT():
  return spark.readStream.format("cloudFiles").option("cloudFiles.format", "csv").load(file_location)

dlt.create_streaming_table(
  name="table_name",
  comment="comments"
)

dlt.apply_changes(
  target = "table_name", 
  source = "view_name",
  keys = ["id"], 
  sequence_by = col("Date")
)&lt;/PRE&gt;&lt;P&gt;I would like to create an extra column called CreatedDate which would be when the record was first inserted into the table, I can't put&amp;nbsp;withColumn(CreatedDate, current_timestamp()) into&amp;nbsp;vw_DLT() above because that would just get updated every load, so I've tried a couple of things:&lt;/P&gt;&lt;P&gt;First I added a schema to&amp;nbsp;dlt.create_streaming_table&amp;nbsp;which includes&amp;nbsp;CreatdDate TIMESTAMP GENERATED ALWAYS AS (NOW()), but that fails with this error when I run the DLT pipeline:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="tommyhmt_0-1723758970190.png" style="width: 400px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/10372iA45B40858E3CA813/image-size/medium/is-moderation-mode/true?v=v2&amp;amp;px=400" role="button" title="tommyhmt_0-1723758970190.png" alt="tommyhmt_0-1723758970190.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Secondly I was able to:&lt;/P&gt;&lt;P&gt;1. Alter the&amp;nbsp;&lt;SPAN&gt;__apply_changes_storage_&lt;/SPAN&gt; table and adding the CreatedDate column:&lt;/P&gt;&lt;P&gt;2. Alter the CreatedDate column and set default&amp;nbsp;&lt;SPAN&gt;current_timestamp()&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;3. Update the existing records to current_timestamp() manually&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;All this works in a notebook and when I run the pipeline again, the new records do indeed have a new CreatedDate value, and old records did not update that column.&amp;nbsp; But I'm not able to run the notebook above as part of the DLT job, since I get this error:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="tommyhmt_1-1723759255869.png" style="width: 400px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/10373iEBF89F1FFE291CA5/image-size/medium/is-moderation-mode/true?v=v2&amp;amp;px=400" role="button" title="tommyhmt_1-1723759255869.png" alt="tommyhmt_1-1723759255869.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;If that works, then all I'd need to do is run the notebook just once after the initial load, and the rest will work fine.&lt;/P&gt;</description>
      <pubDate>Thu, 15 Aug 2024 22:02:57 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/add-createddate-to-delta-live-table/m-p/83144#M36852</guid>
      <dc:creator>tommyhmt</dc:creator>
      <dc:date>2024-08-15T22:02:57Z</dc:date>
    </item>
    <item>
      <title>Re: Add CreatedDate to Delta Live Table</title>
      <link>https://community.databricks.com/t5/data-engineering/add-createddate-to-delta-live-table/m-p/101311#M40630</link>
      <description>&lt;P&gt;To add a CreatedDate column that captures the timestamp when a record is first inserted into the table, you can modify your Delta Live Tables (DLT) pipeline setup as follows:&lt;/P&gt;
&lt;P&gt;1) Define the schema for your streaming table to include the CreatedDate column. This column will be populated with the current timestamp when a new record is inserted.&lt;/P&gt;
&lt;P&gt;dlt.create_streaming_table(&lt;BR /&gt;name="table_name",&lt;BR /&gt;comment="comments",&lt;BR /&gt;schema="""&lt;BR /&gt;id STRING,&lt;BR /&gt;Date TIMESTAMP,&lt;BR /&gt;CreatedDate TIMESTAMP&lt;BR /&gt;"""&lt;BR /&gt;)&lt;BR /&gt;2)&amp;nbsp;Ensure that the apply_changes function does not overwrite the CreatedDate column on subsequent updates.&lt;/P&gt;
&lt;P&gt;dlt.apply_changes(&lt;BR /&gt;target="table_name",&lt;BR /&gt;source="view_name",&lt;BR /&gt;keys=["id"],&lt;BR /&gt;sequence_by=col("Date"),&lt;BR /&gt;except_column_list=["CreatedDate"]&lt;BR /&gt;)&lt;/P&gt;</description>
      <pubDate>Sat, 07 Dec 2024 06:45:27 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/add-createddate-to-delta-live-table/m-p/101311#M40630</guid>
      <dc:creator>Mounika_Tarigop</dc:creator>
      <dc:date>2024-12-07T06:45:27Z</dc:date>
    </item>
    <item>
      <title>Re: Add CreatedDate to Delta Live Table</title>
      <link>https://community.databricks.com/t5/data-engineering/add-createddate-to-delta-live-table/m-p/101380#M40645</link>
      <description>&lt;P&gt;Hi Mounika,&lt;/P&gt;&lt;P&gt;Why would defining the&amp;nbsp;&lt;SPAN&gt;CreatedDate in the schema automatically mean "the column will be populated&amp;nbsp;with the current timestamp when a new record is inserted"?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 08 Dec 2024 16:22:52 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/add-createddate-to-delta-live-table/m-p/101380#M40645</guid>
      <dc:creator>tommyhmt</dc:creator>
      <dc:date>2024-12-08T16:22:52Z</dc:date>
    </item>
  </channel>
</rss>

