<?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 New Databricks SQL window fuction: counter_diff in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/new-databricks-sql-window-fuction-counter-diff/m-p/169619#M56130</link>
    <description>&lt;P&gt;This function was designed to work with cumulative counters in time series. It automatically calculates the difference between the current and previous value, turning a cumulative counter into a per-period variation.&lt;/P&gt;&lt;P&gt;Let's look at a practical example.&lt;/P&gt;&lt;P&gt;Imagine a factory that records the total number of parts produced every hour:&lt;/P&gt;&lt;P&gt;Time Cumulative Production&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;08:00&lt;/TD&gt;&lt;TD&gt;1,000&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;09:00&lt;/TD&gt;&lt;TD&gt;1,125&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;10:00&lt;/TD&gt;&lt;TD&gt;1,260&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;Using the new counter_diff function:&lt;/P&gt;&lt;PRE&gt;counter_diff(producao_acumulada) OVER (ORDER BY hora)&lt;/PRE&gt;&lt;P&gt;The result would be:&lt;/P&gt;&lt;P&gt;Time Cumulative Production Difference&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;08:00&lt;/TD&gt;&lt;TD&gt;1,000&lt;/TD&gt;&lt;TD&gt;NULL&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;09:00&lt;/TD&gt;&lt;TD&gt;1,125&lt;/TD&gt;&lt;TD&gt;125&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;10:00&lt;/TD&gt;&lt;TD&gt;1,260&lt;/TD&gt;&lt;TD&gt;135&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;Previously, this type of calculation could be handled using the LAG() function.&lt;/P&gt;&lt;P&gt;But what happens when the machine's counter is reset?&lt;/P&gt;&lt;PRE&gt;11:00 → 1,410
12:00 → 80
13:00 → 210&lt;/PRE&gt;&lt;P&gt;A simple calculation would produce:&lt;/P&gt;&lt;PRE&gt;80 - 1,410 = -1,330 &lt;span class="lia-unicode-emoji" title=":cross_mark:"&gt;❌&lt;/span&gt;&lt;/PRE&gt;&lt;P&gt;counter_diff can identify this counter reset, preventing the restart from being interpreted as negative production.&lt;/P&gt;&lt;P&gt;It's a small feature, but a very useful one when working with data such as:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;P&gt;&lt;span class="lia-unicode-emoji" title=":factory:"&gt;🏭&lt;/span&gt; Industrial production&lt;/P&gt;&lt;/LI&gt;&lt;LI&gt;&lt;P&gt;&lt;span class="lia-unicode-emoji" title=":high_voltage:"&gt;⚡&lt;/span&gt; Energy consumption&lt;/P&gt;&lt;/LI&gt;&lt;LI&gt;&lt;P&gt;&lt;span class="lia-unicode-emoji" title=":droplet:"&gt;💧&lt;/span&gt; Water consumption&lt;/P&gt;&lt;/LI&gt;&lt;LI&gt;&lt;P&gt;&lt;span class="lia-unicode-emoji" title=":bar_chart:"&gt;📊&lt;/span&gt; Other cumulative time-series metrics&lt;/P&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;STRONG&gt;Note:&lt;/STRONG&gt; This function requires Databricks Runtime 19+.&lt;BR /&gt;&lt;BR /&gt;Documentation:&amp;nbsp;&lt;A href="https://docs.databricks.com/aws/en/sql/language-manual/functions/counter_diff" target="_blank" rel="noopener"&gt;https://docs.databricks.com/aws/en/sql/language-manual/functions/counter_diff&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Wed, 23 Sep 2026 17:38:35 GMT</pubDate>
    <dc:creator>lessalucas</dc:creator>
    <dc:date>2026-09-23T17:38:35Z</dc:date>
    <item>
      <title>New Databricks SQL window fuction: counter_diff</title>
      <link>https://community.databricks.com/t5/data-engineering/new-databricks-sql-window-fuction-counter-diff/m-p/169619#M56130</link>
      <description>&lt;P&gt;This function was designed to work with cumulative counters in time series. It automatically calculates the difference between the current and previous value, turning a cumulative counter into a per-period variation.&lt;/P&gt;&lt;P&gt;Let's look at a practical example.&lt;/P&gt;&lt;P&gt;Imagine a factory that records the total number of parts produced every hour:&lt;/P&gt;&lt;P&gt;Time Cumulative Production&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;08:00&lt;/TD&gt;&lt;TD&gt;1,000&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;09:00&lt;/TD&gt;&lt;TD&gt;1,125&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;10:00&lt;/TD&gt;&lt;TD&gt;1,260&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;Using the new counter_diff function:&lt;/P&gt;&lt;PRE&gt;counter_diff(producao_acumulada) OVER (ORDER BY hora)&lt;/PRE&gt;&lt;P&gt;The result would be:&lt;/P&gt;&lt;P&gt;Time Cumulative Production Difference&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;08:00&lt;/TD&gt;&lt;TD&gt;1,000&lt;/TD&gt;&lt;TD&gt;NULL&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;09:00&lt;/TD&gt;&lt;TD&gt;1,125&lt;/TD&gt;&lt;TD&gt;125&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;10:00&lt;/TD&gt;&lt;TD&gt;1,260&lt;/TD&gt;&lt;TD&gt;135&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;Previously, this type of calculation could be handled using the LAG() function.&lt;/P&gt;&lt;P&gt;But what happens when the machine's counter is reset?&lt;/P&gt;&lt;PRE&gt;11:00 → 1,410
12:00 → 80
13:00 → 210&lt;/PRE&gt;&lt;P&gt;A simple calculation would produce:&lt;/P&gt;&lt;PRE&gt;80 - 1,410 = -1,330 &lt;span class="lia-unicode-emoji" title=":cross_mark:"&gt;❌&lt;/span&gt;&lt;/PRE&gt;&lt;P&gt;counter_diff can identify this counter reset, preventing the restart from being interpreted as negative production.&lt;/P&gt;&lt;P&gt;It's a small feature, but a very useful one when working with data such as:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;P&gt;&lt;span class="lia-unicode-emoji" title=":factory:"&gt;🏭&lt;/span&gt; Industrial production&lt;/P&gt;&lt;/LI&gt;&lt;LI&gt;&lt;P&gt;&lt;span class="lia-unicode-emoji" title=":high_voltage:"&gt;⚡&lt;/span&gt; Energy consumption&lt;/P&gt;&lt;/LI&gt;&lt;LI&gt;&lt;P&gt;&lt;span class="lia-unicode-emoji" title=":droplet:"&gt;💧&lt;/span&gt; Water consumption&lt;/P&gt;&lt;/LI&gt;&lt;LI&gt;&lt;P&gt;&lt;span class="lia-unicode-emoji" title=":bar_chart:"&gt;📊&lt;/span&gt; Other cumulative time-series metrics&lt;/P&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;STRONG&gt;Note:&lt;/STRONG&gt; This function requires Databricks Runtime 19+.&lt;BR /&gt;&lt;BR /&gt;Documentation:&amp;nbsp;&lt;A href="https://docs.databricks.com/aws/en/sql/language-manual/functions/counter_diff" target="_blank" rel="noopener"&gt;https://docs.databricks.com/aws/en/sql/language-manual/functions/counter_diff&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 23 Sep 2026 17:38:35 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/new-databricks-sql-window-fuction-counter-diff/m-p/169619#M56130</guid>
      <dc:creator>lessalucas</dc:creator>
      <dc:date>2026-09-23T17:38:35Z</dc:date>
    </item>
    <item>
      <title>Re: New Databricks SQL window fuction: counter_diff</title>
      <link>https://community.databricks.com/t5/data-engineering/new-databricks-sql-window-fuction-counter-diff/m-p/169740#M56160</link>
      <description>&lt;P&gt;The counter_diff function is a significant upgrade over the traditional LAG() approach for IoT and telemetry data because it is "reset-aware."&lt;/P&gt;&lt;P&gt;While a standard subtraction (Current - LAG(Previous)) produces a large negative number when a hardware counter resets to zero, counter_diff detects the drop and treats the new value as the start of a new sequence (returning the value itself rather than a negative delta). This eliminates the need for complex CASE WHEN logic to handle machine restarts or counter rollovers in manufacturing and energy monitoring pipelines.&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Note: This function is available in Databricks Runtime 16.0+ and SQL Warehouse (Pro/Serverless)&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Sep 2026 18:22:53 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/new-databricks-sql-window-fuction-counter-diff/m-p/169740#M56160</guid>
      <dc:creator>Khasim_1</dc:creator>
      <dc:date>2026-09-24T18:22:53Z</dc:date>
    </item>
  </channel>
</rss>

