Is there a way to set $__from and $__to based on a query?

I am trying to use Grafana with postgres db and have time-series data in a table. The data in my table could be from an old time-stamp. When I load the dashboard, I want the graph to adjust to the time-window of the data fetched by my query. Is there is way to set the initial time-window (when the dashboard loads) based on a query?

I tried the below things:

  • Added a variable to calculate (Now() - Start_Time) of my data in minutes
  • Set TimeShift to above variable
  • Set the default time window to last 15mins

With this, on first load the graph does show up data for my range.
But the problem is: when I drag-select a graph to zoom in a specific range, the time-shift doesn’t get overridden.

Does anyone know of a way to set __from and __to directly or any work-around to achieve what I need?