Reducing Series to First Value

What Grafana version and what operating system are you using?
v11.1.0 on a raspberry debian bookworm

Maybe this is too simple to see a straightforward solution: I would like to display the balance (last - first value) of a battery soc in a certain time range.
I managed to get the last value by “reduce fileds” → last, but there is no option for the first value?!
Next try was to do the reduction in the influxdb query itself:

from(bucket: “evcc_log”)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r[“_measurement”] == “batterySoc”)
|> filter(fn: (r) => r[“_field”] == “value”)
|> filter(fn: (r) => r[“id”] == “1”)
|> aggregateWindow(every: v.windowPeriod, fn: first, createEmpty: false)
|> yield(name: “first”)

But despite yield(name: “first”) the influxdb query delivers the total series?!

So, what do to?

thats more of a flux question than a grafana question

I had already read that discussion before, but I am sorry, I can’t find a solution for my problem there.

again it is not a grafana issue. best to ask on influx forum if no one else chimes in here

That could be done by two flux queries. See this example:

Thanks so much, again especially to @ebabeshko.
The solution for me was quite simple:

> from(bucket: "evcc_log")
>   |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
>   |> filter(fn: (r) => r["_measurement"] == "batterySoc")
>   |> filter(fn: (r) => r["_field"] == "value")
>   |> filter(fn: (r) => r["id"] == "1")
>   |> filter(fn: (r) => r._value > 0)
>   |> first()
>   |> aggregateWindow(every: v.windowPeriod, fn: first, createEmpty: false)
>   |> yield(name: "first")

Nevertheless I would like to slightly criticize the design of grafana, in the point that “last” is offered in the reduce-transformation, but not “first”.
An occasional user like me does not necessarily want to additionally deal with influxdb, grafana is already complicated enough (next question will probably follow soon :roll_eyes:).