How do I get the last timestamp of all measurements filtered by a tag?

I have a few measurements, all using the same tag field (ID).
I would like to know the last timestamp for a given ID (so return one timestamp, not the last timestamp for each measurement).

Made a few assumptions , but this should work.


  from(bucket:"example-bucket")
  |> range(start:-72h)
#  |> filter(fn: (r) =>
#    r._measurement == "cpu" and
#    r._field == "usage_system"
#  )
  |> last()
  |> group()
  |> sort(columns: ["_time"])
  |> last()

This is untested, but give it a try. The sort may not be needed in your use case - see how fast it runs with and without sort (checking accuracy as well! )

Flux is still a growing language and doesn’t yet have full optimizations built in.
Uncomment the filter and adjust as needed.
Last 72 hours assumes you get new data for all measurements at least once in that period . The first last() then instructs to only keep the latest record from each table. Group() flattens all tables into one table . The sort() is required since 9/10 times we prefer sequential timestamps. And the final last() keeps only a single row.

1 Like