Exlude specific weekdays (e.g. weekend) from Flux query

Is it possible to exclude specific weekdays from a Flux query?
Example: I am querying the last 4 week but don’t want all data with timestamp on Saturdays and Sundays

I know this possible with SQL, see: StackOverflow

I could imagine you can do something similar with date.weekDay() function and some math. Unfortunately I don’t have too much experience with Flux so I couldn’t test it myself

Many thanks for your help.

Do you have some raw data to share , and what your output from your query currently looks like vs what you want it to look like?

@emil you can filter on the _time column and use date.weekDay() to determine the actual day of the week. To keep this as optimized as possible, I’d suggest you run this time filter after all other filters if possible:

import "date"

// ...
    |> filter(fn: (r) => date.weekDay(t: r._time) != 0 and date.weekDay(t: r._time) != 6)

Thanks for the answer @scott :+1: . I have tested the query today and it is working fine.

Maybe one more question. I am not sure if this is in scope of this topic but is it possible to specify regex (I want to use this together with the Grafana variables) instead of just a number in this query?

Something like this didn’t work:

import "date"

// ...
    |> filter(fn: (r) => date.weekDay(t: r._time) !~ /(1|2|3))/

Error: invalid: type error @9:5-9:29: expected string but found int

Maybe it is possible to change the format (e.g. int to string).
Another option how to exclude multiple days without multiple “date.weekDay(t: r._time) !=” would be fine too.

@emil Flux regular expression operators (=~ and !~) can only be used to evaluate strings. You can convert the output of date.weekDay() to a string:

import "date"

// ...
    |> filter(fn: (r) => string(v: date.weekDay(t: r._time)) !~ /(1|2|3))/
1 Like

That’s working. Thanks a lot for your help