How can I filter from a multi value variable in Flux?

Hi everyone,

I have a variable called NumLinea which has values from 1 to 6.

In the flux query, I used a filter to get this NumLinea values.

|> filter(fn: (r) => r[“Codlinea”] =~ “${NumLinea}”)

When a value is selected there is no problem. However, when multiple values are selected no data appears.

Anybody knows how to get multiple values in a filter.

Thank you beforehands.

Hi @JMU,
I hope you are doing okay. So this is a little tricky, this is because filter is looking for conditional logic. For example:

|> filter(fn: (r) => r._measurement == "cpu" and r._field == "usage_system" and r.cpu == "cpu-total")


|> filter(fn: (r) => r.Codlinea == "1" or r. Codlinea == "2" or r. Codlinea == "3")

Are valid notation.

I know but this would be manual filters.

The idea of the Numlinea variable is that the user chooses the number of lines he wants to visualize.

This solution doesn’t work for this situation.

You can structure NumLinea as an array and use contains() to see if the if the number is in the array. There are some caveats here:

  1. contains() can be pretty slow on large datasets.
  2. You’re currently using a regular expression operator to compare r.Codelinea to the user specified variable. contains() doesn’t support regular expressions. It needs to be an exact match.
selectedLines = ["1", "2"]

// ...
    |> filter(fn: (r) => contains(value: r["Codlinea"], set: selectedLines)
1 Like

Hi Scott,

I tried your code but it didn’t work for my solution.

I find this one and it works.

|> filter(fn: (r) => r[“Codlinea”] =~ /^${NumLinea:regex}$/)

Best regards