With flux request, how to get the date of the first point and the last point inserted in the bucket?

With flux query, how to get the date of the first point and the last point inserted in the bucket?
thanks.

@lepton.phlb

Does this bucket contain hours, days, months or years’ worth of data? Usually a Flux query is applied on a certain time window (e.g. find the first and last points).

If I use and imported bucket, for a deferred exploitation of the data, and I don’t know the date of the first point and the last point, I need to send a query to retrieve this time window.

@lepton.phlb

OK, got it. Something like this should work for the first point. Change the last 2 lines to get the last point using the last() function.

from(bucket: "HillValley")
  |> range(start: 0, stop: now())
  |> filter(fn: (r) => r["_measurement"] == "ElectricityData")
  |> filter(fn: (r) => r["Location"] == "clocktower")
  |> filter(fn: (r) => r["Source"] == "lightning")
  |> filter(fn: (r) => r["_field"] == "power")
  |> first(column: "_time")
  |> yield(name: "first_point_timestamp")

Great! I hadn’t thought about range(start: 0, stop: now()) and the last 2 lines of your query. Thanks for your reply and your help.

It works :slight_smile:
To obtain the first data point date:

from(bucket: "my-bms-timeseries")
  |> range(start: 0, stop: now())
  |> filter(fn: (r) => r["_measurement"] == "BATTERY_1")
  |> filter(fn: (r) => r["0"] == "BRANCH_A")
  |> filter(fn: (r) => r["1"] == "BRANCH_DATA")
  |> filter(fn: (r) => r["_field"] == "BRANCH_CURRENT")
  |> first(column: "_time")
  |> yield(name: "first")

To obtain the last data point date:

from(bucket: "my-bms-timeseries")
  |> range(start: 0, stop: now())
  |> filter(fn: (r) => r["_measurement"] == "BATTERY_1")
  |> filter(fn: (r) => r["0"] == "BRANCH_A")
  |> filter(fn: (r) => r["1"] == "BRANCH_DATA")
  |> filter(fn: (r) => r["_field"] == "BRANCH_CURRENT")
  |> last(column: "_time")
  |> yield(name: "last")
1 Like