# How to filter and display data in Bar Chart by hour range

**URL:** <https://community.grafana.com/t/how-to-filter-and-display-data-in-bar-chart-by-hour-range/120298>\
**Category:** InfluxDB\
**Tags:** bar-chart\
**Created:** [April 25, 2024, 2:40pm UTC](https://community.grafana.com/t/how-to-filter-and-display-data-in-bar-chart-by-hour-range/120298 "2024-04-25T14:40:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![joelhdo](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@joelhdo](https://community.grafana.com/u/joelhdo)\
**Post date:** [April 25, 2024, 2:40pm UTC](https://community.grafana.com/t/how-to-filter-and-display-data-in-bar-chart-by-hour-range/120298/1 "2024-04-25T14:40:38Z")

</div>

Hello friends,  
I’m using Grafana v10.3.3 on Windows and getting data from an InfluxDB.

My database stores an application login history data and I want to show it in grafana classified in three groups by hour range. 00:00 - 8:30am, 8:30am - 11:30am and 11:30am - 2:00pm) something like this:

 ![image](https://us1.discourse-cdn.com/grafana/original/3X/e/8/e812f98db1d55da940a2274bf7343dd8e8b0e7f9.png)

I used this below query in the screenshot above  
SELECT count(“Type”) FROM “ICAU” WHERE (“active”::tag = ‘True’) AND time \>= ${\_\_from}ms and time \< ${\_\_to}ms-15h30m GROUP BY time(1d)  
and I used three queries in the bart chart graph with different time range to show what I want.

**Issue** : The query above works only with one day range selected, if I choose a range date for more than one day it does not work because the two vars ${\_\_from} and ${\_\_to} takes the two days range to do the calculation and then the query shows the sum of the values of the two days in that hour range. Example below

 ![image](https://us1.discourse-cdn.com/grafana/original/3X/e/a/eade631a205f5982f7ca90aa349c4c40ea26804e.png)

I have tried:

1. Transform Data → Filter data by values: This I can’t figure out how to set the range since it does note have the option.
2. Do a subquery to get the data by date and filtered by hour range but grafana throws error

What I want to achieve is to show the data grouped by hour range when any date range is selected.

Any ideas please?

---

<div class="post-metadata">

**Author:** ![grant2](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/grant2/32/42281_2.png) [@grant2](https://community.grafana.com/u/grant2)\
**Post date:** [April 25, 2024, 11:26pm UTC](https://community.grafana.com/t/how-to-filter-and-display-data-in-bar-chart-by-hour-range/120298/2 "2024-04-25T23:26:12Z")

</div>

Hi @joelhdo and welcome to the Grafana forum.

So as you correctly noted, the queries you wrote do not work for a range longer than one day. I am not even sure this is possible using InfluxQL.

Have you considered using Flux or SQL instead of InfluxQL? In Flux and SQL, I created queries that do an aggregation by the hour of the day (i.e. 24 bars representing 24 hours). Maybe you could modify these queries to reflect the 3 bin sizes that you specified (00:00 - 8:30am, 8:30am - 11:30am and 11:30am - 2:00pm)?

[Here’s a blog](https://grafana.com/blog/2023/12/29/a-comparison-of-influxql-sql-and-flux-query-languages-for-grafana-dashboards/) post comparing the 3 languages. Here are the two bar charts that “bin by the hour of the day” and will change based on the Time Picker (7 days, 30 days, 6 months, 1 year, etc.)

[Flux](https://play.grafana.org/d/e7bad3ef-db0c-4bbd-8245-b85c0b2ca2b9/389d020a-e36b-58aa-a626-3b37e03730bd?orgId=1&refresh=1m&editPanel=27&from=1711495750580&to=1714087750580)

[SQL](https://play.grafana.org/d/f9a5b2d1-0227-4341-93fd-113fedba6481/217d6661-10d9-5c57-9e4c-31da5a341b15?orgId=1&editPanel=28&from=1712791449412&to=1714087449412)

---

<div class="post-metadata">

**Author:** ![joelhdo](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@joelhdo](https://community.grafana.com/u/joelhdo)\
**Post date:** [April 26, 2024, 7:25pm UTC](https://community.grafana.com/t/how-to-filter-and-display-data-in-bar-chart-by-hour-range/120298/3 "2024-04-26T19:25:05Z")

</div>

Hey @grant2

I appreciate your answer. I followed your suggestion to try with Flux, I did not get it done not because is not possible, it was because I’m not familiar with Flux, this is what I got so far:

 ![image](https://us1.discourse-cdn.com/grafana/original/3X/d/a/da9aef10188b77e80156543d59be60e2008ef9e6.png)  
Using this:

```auto
*import "date"*
*import "strings"*
*from(bucket:"intellichief")*
* |> range(start: v.timeRangeStart, stop: v.timeRangeStop)*
* |> filter(fn: (r) => r["_measurement"] == "ICAU")*
* |> filter(fn: (r) => r["_field"] == "Type")*
* |> filter(fn: (r) => date.hour(t: r["_time"]) >= 0 and date.hour(t: r["_time"]) <= 8)*
* |> aggregateWindow(every: 1d, fn:count, createEmpty: false)*

```

**Workaround** : However I did find a workaround and I will see the behavior in the next couple of days. I owned the data base so I extracted the hour and minute value, concatenated them in a new column in the measurement and then filter the values between the range I’m expecting to graph in my query. I’ll see how it works.

Thank you so much!
