Filter the first and last rows from a table

I have a measurement with five minute entries that are a meter reading, I would like to calculate the usage to date so I just need to subtract the values of the first and last records for a selected time period.

Is there a nice Flux filter/method to reduce a table to just the first and last records so I can apply the difference() function?

@sillygoose Rather than selecting the first and last rows, you can apply difference() and then sum() the differences. For example:

import "experimental/array"

array.from(rows: [
  {_time: 2021-01-01T00:00:00Z, _value: 12},
  {_time: 2021-01-01T00:00:00Z, _value: 8},
  {_time: 2021-01-01T00:00:00Z, _value: 14},
  {_time: 2021-01-01T00:00:00Z, _value: 18}
])
|> difference()
|> sum()

Returns:

_value
6

Edit:

If you wanted to use this with aggregateWindow(), it’d look something like this:

data
  |> aggregateWindow(
    every: 1h,
    fn: (tables=<-, column) => tables |> difference() |> sum()
  )

Thanks so much, I was too focused on getting the table reduced to not notice this elegant solution. As you might tell I am just getting started with Flux and have much more to master.

No problem. Happy to help!