Flux Query/ Function to find the cumulative sum of the data

Hello,

I am Sending my KWH consumption values to the InfluxDB every 3 hours from my Meter which logs the running totaled value.
On the cloud influxDb2 dashboard I want to show the KWH consumption. To find the Consumption for a given period of time. I am using the Spread() function to subtract the first value from the last value. this is causing issues when the meter reset to zero due to many things.

I want to calculate the cumulative sum of the data instead of just trying to use spread(). Can someone help me with this?

This meaning I need to sum only the delta between two consecutive values, that too the positive delta not the negative delta.

This code is not working for me.

data = from(bucket: “Jushi”)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r[“_measurement”] == “DentMeter”)
|> filter(fn: (r) => r[“_field”] == “JushiPanel208KWH”)
min = data
|> min()
|> set(key: “_field”, value: “delta”)
max = data
|> max()
|> set(key: “_field”, value: “delta”)
union(tables: [min, max])
|> difference()

If I understood your problem right I solved it here: