# How to build query filter from string OR dynamic filter generation from changing input variable

**URL:** <https://community.influxdata.com/t/how-to-build-query-filter-from-string-or-dynamic-filter-generation-from-changing-input-variable/27501>\
**Category:** Fluxlang\
**Tags:** grafana, flux\
**Created:** [November 24, 2022, 3:45pm UTC](https://community.influxdata.com/t/how-to-build-query-filter-from-string-or-dynamic-filter-generation-from-changing-input-variable/27501 "2022-11-24T15:45:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nuri](https://avatars.discourse-cdn.com/v4/letter/n/41988e/32.png) [@Nuri](https://community.influxdata.com/u/Nuri)\
**Post date:** [November 24, 2022, 3:45pm UTC](https://community.influxdata.com/t/how-to-build-query-filter-from-string-or-dynamic-filter-generation-from-changing-input-variable/27501/1 "2022-11-24T15:45:46Z")

</div>

Hi, I’m trying to reconstruct the multi value option for FLUX and Grafana. I have been trying out a lot in the REPL and think this is more a FLUX issue then Grafana.

Use case: via a template variable, one or more clients can be selected, and I want to see them all in one graph.

I have an array with the client names (can be of any length between 0 and 999):

```auto
> arr
[client1,client2,client3]

```

I do have a solution for when I know how long the array is:

```auto
data
  |> range(start: -1d)
  |> filter(fn: (r) => r._measurement == "custom_track")
  |> filter(fn: (r) => r._field == "value")
  |> filter(fn: (r) => r.client == arr[0] or r.client == arr[1] or r.client == arr[2])
  |> group(columns: ["client"])
  |> aggregateWindow(every: 1d, fn: sum)
  |> yield()

```

But I need dynamic code that can react on `length == 1` or `> 1`, so writing the `|> filter(fn: (r) => r.client ...`-line dynamicly in the manner of `|> filter(fn: (r) => r.client == arr[0] or r.client == arr[1] or r.client == arr[2] ...)`, depending on however many entries there exist in `arr`.

With `strings.joinStr()` I can concat my arr in the right syntax: `s = strings.joinStr(arr: arr, v: " or r.client == ")` which returns me: `"client1 or r.client == client2 or r.client == client3"`

if I now place this into the query, I get an empty `_result` because it is pasted as one large string, and the `or`s are not interpreted:

```auto
data
  |> range(start: -1d)
  |> filter(fn: (r) => r._measurement == "custom_track")
  |> filter(fn: (r) => r._field == "value")
  |> filter(fn: (r) => r.client == s)
  |> group(columns: ["client"])
  |> aggregateWindow(every: 1d, fn: sum)
  |> yield()

```

This makes sense, since it is an actual string, but I need it to be executed.  
I guess I’m looking for something like a “string to stream”-function, but I couldn’t find anything in that direction in the documentation.

Any ideas how I can resolve that?

---

<div class="post-metadata">

**Author:** ![Jay\_Clifford](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/jay_clifford/32/8287_2.png) [@Jay\_Clifford](https://community.influxdata.com/u/Jay_Clifford)\
**Post date:** [November 24, 2022, 3:59pm UTC](https://community.influxdata.com/t/how-to-build-query-filter-from-string-or-dynamic-filter-generation-from-changing-input-variable/27501/2 "2022-11-24T15:59:51Z")

</div>

Hi @Nuri,  
The flux function contains should fit your needs: [contains() function | Flux 0.x Documentation](https://docs.influxdata.com/flux/v0.x/stdlib/universe/contains/)

---

<div class="post-metadata">

**Author:** ![Jay\_Clifford](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/jay_clifford/32/8287_2.png) [@Jay\_Clifford](https://community.influxdata.com/u/Jay_Clifford)\
**Post date:** [November 24, 2022, 4:01pm UTC](https://community.influxdata.com/t/how-to-build-query-filter-from-string-or-dynamic-filter-generation-from-changing-input-variable/27501/3 "2022-11-24T16:01:37Z")

</div>

> [@Nuri](#):
>
> ```auto
> data
> |> range(start: -1d)
> |> filter(fn: (r) => r._measurement == "custom_track")
> |> filter(fn: (r) => r._field == "value")
> |> filter(fn: (r) => r.client == arr[0] or r.client == arr[1] or r.client == arr[2])
> |> group(columns: ["client"])
> |> aggregateWindow(every: 1d, fn: sum)
> |> yield()
> 
> ```

```auto
clients = [client1,client2,client3]
data
  |> range(start: -1d)
  |> filter(fn: (r) => r._measurement == "custom_track")
  |> filter(fn: (r) => r._field == "value")
   |> filter(fn: (r) => contains(value: r. client, set: clients))
  |> group(columns: ["client"])
  |> aggregateWindow(every: 1d, fn: sum)
  |> yield()

```

---

<div class="post-metadata">

**Author:** ![Nuri](https://avatars.discourse-cdn.com/v4/letter/n/41988e/32.png) [@Nuri](https://community.influxdata.com/u/Nuri)\
**Post date:** [November 30, 2022, 1:05pm UTC](https://community.influxdata.com/t/how-to-build-query-filter-from-string-or-dynamic-filter-generation-from-changing-input-variable/27501/4 "2022-11-30T13:05:36Z")

</div>

Awesome, works like a charm. Thank you so much @Jay_Clifford !
