How to query last timestamp values?

I am trying to query only the last point added to the database with a specific tag value.
When I use last() function, it gives me the last value of all fields with that tag, which is not what I want.
As a similar question, I found this, which is not for influxdb2. I tried to implement a similar code, which didn’t work. This is the code:

from(bucket: "BUCKET1")
  |> range(start: -30d)
  |> filter(fn: (r) => r["_measurement"] == "DEVICE1")
  |> filter(fn: (r) => r["Tag"] == "Tag_Value")
  |> sort(columns:["_time"], desc: true)
  |> limit(n:1)

I expected the query to return all the fields and their values only for the last timestamp, which is the last point added to DB. But, this query returns the last value of all the fields.(for some fields, the last value has happened prior to the last timestamp)

I would appreciate any suggestion.

Hello @Amir_Ft,
Unfortunately this is a limitation of Flux and InfluxDB at the moment. You’ll have to increase your range() function.
I encourage you to comment on this issue though to help Product prioritize these features which would enable it.

1 Like