docs-v2/content/resources/how-to-guides/select-hours-from-data.md

1.4 KiB

title description menu weight
Select data from specific hours Learn how to select data from specific hours of the day.
resources
parent
How-to guides
102

Problem

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

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

import "date"

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

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 to select data from certain seconds in a minute, minutes in an hour, days in the month, months in the year, etc. Use the [Flux `date` package](/flux/v0.x/stdlib/date/) to assign integer representations to your data and filter for your desired schedule.