Apply different time ranges on same panel

  • Grafana version: 10.4.7

  • Apply different time ranges to different timestamp columns (e.g. gate1_ts and gate2_ts). My sample data looks like (tag is getting read at different gates):
    |tag | gate1_ts | gate2_ts | gate3_ts |
    |— | — | — | — |
    |tag_1 | 2024-09-15 10:35:30 | 2024-09-15 13:55:10 | 2024-09-15 19:20:50|
    |tag_1 | 2024-09-13 14:15:30 | 2024-09-13 15:25:10 | 2024-09-13 18:30:50|
    |tag_2 | 2024-09-13 14:05:30 | 2024-09-13 16:45:10 | 2024-09-13 17:50:50|

For applying a time range to one timestamp column (gate1_ts) I can use panel global time filter and write my query something like this:
select * from table1 where $__timeFilter(gate1_ts)

I can use following query to add time range for second timestamp column (gate2_ts):
select * from table1 where $__timeFilter(gate1_ts) and $__timeFilter(gate2_ts)
which constructs my query like:
select * from table1 WHERE gate1_ts BETWEEN '2024-09-13T13:29:59Z' AND '2024-09-13T16:59:58Z' and gate2_ts BETWEEN '2024-09-13T13:29:59Z' AND '2024-09-13T16:59:58Z
but it restricts me to use same time range for both columns, however, I want different time ranges for different columns (gate1_ts and gate2_ts).
I want my query to look like:
select * from table1 WHERE gate1_ts BETWEEN '2024-09-13T13:29:59Z' AND '2024-09-13T14:59:58Z' and gate2_ts BETWEEN '2024-09-13T16:29:59Z' AND '2024-09-13T17:59:58Z

I am aware about “relative time” and “time shift” options under query option but it doesn’t solve my problem as I don’t need fixed time intervals

Thanks in advance!

@grafanauser009
Just curious why don’t you use multiple queries? In the same panel?

what is your data source?

Thanks for the response. I am using Pie Chart as visualization, and as soon as I am writing second query it doesn’t give me any option to use Pie chart as visualization. Also, I am think it won’t solve my issue as I want to use AND operator between 2 time filters

Thanks for the reply. I am using PostgreSQL

use a stored procedure and send the selected date(s) from grafana date picker.

there in your stored procedure you can do what grafana does which is the BETWEEN function and for the second time column gate2_ts just add +3 hours to the date time sent in the stored procedure date paramater.

create proc bujubanton(@from datetime, @to datetime)
as
begin
select * 
  from table1 
 WHERE gate1_ts BETWEEN @from AND @to and 
gate2_ts BETWEEN dateadd(hh,@from, 3) AND dateadd(hh,@to, 3)
end

type of thing. that way you have control over things and it performs much fast too

Hey, thanks for the suggestion. However, the difference between 2 datetime columns not fixed (3 hours, as mentioned in your solution) unintentinally I added 3 hours diff in example. Sorry for that. I was just wondering if its possible to create custom time filter like global time filter (top right of panel)

then see this for custom date picker