I've got a bit of a potentially unusual requirement for a KQL query. It boils down to finding the difference of the first instance to the last instance of a value within a specified timespan.
Say we got the following schema with value
being incremented at random times:
"TableName": OdometerData,
"Schema": timestamp:datetime, value:real
Querying this is simple enough with for example:
OdometerData
| where timestamp between (ago(7d) .. now())
| summarize diff=(max(value) - min(value))
The issue is that this value
field will rollover and reset to 0 when it reaches a predetermined value. This means that the query above would not return the proper value if a rollover happened during the span of the query. Correct result would be to get a value that "continues" the value after the rollover instead of resetting to 0 and summarizing the difference.
My question is if there is some built-in functionality or perhaps a way to use a materialized view to keep a value that will continue to increment independent of the value
rollover? Or would this be better solved with pre-processing of the data before writing it into the database?
Thanks
This is indeed an interesting scenario. I don't believe there is anything built-in which would fix this entire use but since it's KQL there is usually at least one way of doing it.
Personally I'd approach this with windows functions. Namely
serialise
,next
androw_cumsum
which does the incrementation you're asking for in your question. Also add in acase
statement for some logic.An interesting possibly edge case I've commented in the code below is what happens if the value is the same across two sequential
value
pairs? Do you assume it's gone full circle or assume it's not changed?Since you also mention pre-processing before this data gets ingested that could absolutely be another sensible option too.
Or example output with the
summarize
above.