Calculate value range of a field/column

Hello,
i want to calculate the value range of a field in a time range.
With InfluxQL i would simply use:
SELECT max(fieldname)-min(fieldname) FROM measurement where time > now()-120s

I found no solution to do the above with flux. I tried to transpile with influx tool but only get:
panic: interface conversion: *ast.BinaryExpression is not ast.PropertyKey: missing method Key

Thanks in advance for your help,
Harald Tillmanns

Hello,
in the meantime I’ve given myself the answer:

from(bucket: “myBucket”)
|> range(start: -1h)
|> filter(fn: ® => r["_measurement"] == “myMeasurement”)
|> filter(fn: ® => r["_field"] == “myField”)
|> reduce(fn: (r,
accumulator) => ({
min: if r._value < accumulator.min then r._value else accumulator.min,
max: if r._value > accumulator.max then r._value else accumulator.max
}),
identity: {min: 10000000000.0, max: -10000000000.0})
|> map(fn: ® => ({ r with range: r.max - r.min }))
|> yield(name: “powerRange/[kWh]”)

Hope it helps somebody.

@Harald_Tillmanns ,
:star: :star2: Thank you so much for sharing your answer!!