Flux aggregate function to calculate the difference from last to first

I’m trying to learn the flux query language for InfluxDB. I’m using InfluxDB OSS 2.7.

I have a time-series with power usage from my power meter. It reports an ever increasing number in KWh, and I want to show how many Wh I have used per day, by using a custom function with aggregateWindow. Here is what I have tried:

myFunc = (tables=<-, column) => {
  a = tables
    |> first(column: column)
    |> findRecord(fn: (key) => true, idx: 0)

  b = tables
    |> last(column: column)
    |> findRecord(fn: (key) => true, idx: 0)

  d = b._value - a._value

  return tables
    |> first()
    |> map(fn: (r) => ({ r with _value: d}))
}

from(bucket: "a")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "el")
  |> filter(fn: (r) => r["_field"] == "ACTIVE_IMPORT")
  |> aggregateWindow(every: 1d, fn: myFunc, createEmpty: false)
  |> yield(name: "Wh") 

But this returns a new table, where all _value have the same number (in my case 134).

I was hoping that the variables a and b would have the first and the last value of each window, and that d would represent the usage in each window - but this does not seem to be the case.

What about fn: range?

And check for funny business with the tables and their names and their group keys in the stream of tables.