Date-picker in grafana

I need help does grafana has a date-picker as a variable
e.g
image
I want my dashboards to change based on the date selected

What data source are you using? Most data sources have this built in. If you are using a database with a query language, then they have macros for using the date range as a variable (see $__timeFrom macro in mysql as an example).

I am using Microsoft SQL Server, but I would want a user to select a single date which is used in the query to retrieve data of that specific date

1 Like

I have a time series picker like this:

Would also like to have Date: --from-- --To–
when looking back for a long period

I see grafana has time range (from and to), but i would want users to choose only one date, not a range.

There is no way to get Grafana to have a time picker with one date. There is nearly always a range - for example for your date: 2020-04-20 I’m guessing that you are querying for that day. This is the range 2020-04:20 00:00:00 to 2020-04-20 23:59:59.

For Microsoft SQL Server, then you have similar macros for time that you can use in your sql query:

$__timeFrom() is probably the one you want to use. It gives you the from date of the time range.

Alright, thank you so much.

Hi @daniellee ,

I feel I actually need the user to select a single date as well instead of a date range to make an unambiguous/simple UI for a specific case. I m going to try to explain (hopefully it s clear enough…).

  • We are using a postgres data source with our own SQL queries & Grafana variables
  • The main goal of this dashboard is to show statistics over a given period (typically the previous week or previous month)
  • In addition, on some visualizations of the same dashboard, we want to show a history of these statistics in time to see how the current period compares to the past ones with a bar plot. So we typically have a variable “$NumberOfPastPeriods” to show multiple past bars.

When the main period of interest is 1 week, things work out. The user can just select a time range of 7 days, we get the interval size with $__timeTo()-__timeFrom() and go back in time $NumberOfPastPeriods * ($__timeTo()-__timeFrom()) in our query (while truncating per week) with each bin representing a period with the same characteristics (eg Monday to Sunday)

The problem is for months. If the user chooses an interval “Previous month” then what we get is the $__timeTo() and __timeFrom() for the previous month. The problem is that months can have 30, 31 or 28/29 days.
So "Going back in time by $NumberOfPastPeriods * ($__timeTo()-__timeFrom()) (while truncating dates by month) does not really work - we are not getting “true” months.

A straightforward/simple UI for users would be to select 1) a single current date (i.e., not a range); 2) the main period of interest with our own constraints (eg, 1w, 1M); 3) and the number of past periods for historical plots ($NumberOfPastPeriods).

This would require a new option (e.g. “Single date instead of range”) in the dashboard settings.

Or are we missing a creating way to do what we try to achieve?

Thanks so much
B

1 Like