Select data from specific hours

You may want to select data from specific hours of the day. For example, you may only want data within normal business hours (9am - 5pm).

Solution 1: hourSelection()

Use hourSelection() to filter data by a specific hour range in each day.

from(bucket: "example-bucket")
    |> range(start: -7d)
    |> filter(fn: (r) => r["_measurement"] == "example-measurement")
    |> filter(fn: (r) => r["_field"] == "example-field")
    |> hourSelection(start: 9, stop: 17)

Solution 2: date.hour()

Use date.hour() to evaluate hours in a filter() predicate.

import "date"

from(bucket: "example-bucket")
    |> range(start: -7d)
    |> filter(fn: (r) => r["_measurement"] == "example-measurement")
    |> filter(fn: (r) => r["_field"] == "example-field")
    |> filter(fn: (r) => date.hour(t: r["_time"]) > 9 and date.hour(t: r["_time"]) < 17)

This solution also applies if you want to select data from certain seconds in a minute, minutes in an hour, days in the month, months in the year, and so on. Use the Flux date package to assign integer representations to your data and filter for your desired schedule.


Was this page helpful?

Thank you for your feedback!