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.
Let's look at a practical example.
Imagine a factory that records the total number of parts produced every hour:
Time Cumulative Production
| 08:00 | 1,000 |
| 09:00 | 1,125 |
| 10:00 | 1,260 |
Using the new counter_diff function:
counter_diff(producao_acumulada) OVER (ORDER BY hora)
The result would be:
Time Cumulative Production Difference
| 08:00 | 1,000 | NULL |
| 09:00 | 1,125 | 125 |
| 10:00 | 1,260 | 135 |
Previously, this type of calculation could be handled using the LAG() function.
But what happens when the machine's counter is reset?
11:00 โ 1,410
12:00 โ 80
13:00 โ 210
A simple calculation would produce:
80 - 1,410 = -1,330 โ
counter_diff can identify this counter reset, preventing the restart from being interpreted as negative production.
It's a small feature, but a very useful one when working with data such as:
Note: This function requires Databricks Runtime 19+.
Documentation: https://docs.databricks.com/aws/en/sql/language-manual/functions/counter_diff