Semi relative timeshift query InfluxDB/Grafana

Hello!

I’ve been struggling with Grafana and influxDB trying to do a query from yesterday midnight to today midnight (I wanted different time ranges on the same dashboard).

I’ve found a workaround that might be useful for more than this particular query since you can select a time range different from the Grafana timepicker.

To begin with, I made a query to get the current date from Influxdb, it looks like this:


The regex allows us to get the date in this format “yyyy-mm-dd”
You can get the current hours too, but it will be useless since you can use “now()” in your Influx query.

You can then use this variable to do semi-relative query like this: (hit the “Toggle edit mode” of the query")

You can specify the precise hours and you can do an operation on the date of the query with the time operations.
List of operations can be found here

If no hours are specified, Influxdb will assume that it is midnight (00:00:00), but i prefer to specify it.

Hope it will be useful for generations to come :wink:
Happy hacking!

You can actually change the timerange of the dashboard using from=now/d-1d and to=now/d. That will create a timerange (based on my time now) of from=2018-11-29 00:00:00 and to=2018-11-30 23:59:59.

Hi!

Thanks for you response.
Actually, I wanted to have multiples timeranges on the same dashbord and the format “from to” is applied to everything on the dashbord. So I made this workaround!

Thank you very much for sharing this information. Is this still the way to do it in 2020?