To get the exact output format you showed above, you’d have to do something like this:
import "array"
import "date"
from(bucket: "ttn")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "waage-1")
|> filter(fn: (r) => r["_field"] == "wight")
|> window(every: 1d)
|> reduce(
identity:
{ idx: 0,
date: today(),
"first time": today(),
"first value": 0,
"last time": today(),
"last value": 0,
difference: 0
},
fn: (r, accumulator) => {
idx = accumulator.idx + 1
date = date.truncate(t: r._time, unit: 1d)
firstTime = if accumulator.idx == 0 then r._time else accumulator["first time"]
firstValue = if accumulator.idx == 0 then r._value else accumulator["first value"]
lastTime = r._time
lastValue = r._value
difference = lastValue - firstValue
return {
idx: idx,
date: date,
"first time": firstTime,
"first value": firstValue,
"last time": lastTime,
"last value": lastValue,
difference: difference,
}
})
|> drop(columns: ["idx", "_start", "_stop"])
But if you’re just trying to show the difference between the first and last value of each day, there’s a simpler way.