# Performant way for conditional filter

**URL:** <https://community.influxdata.com/t/performant-way-for-conditional-filter/32087>\
**Category:** Dashboards\
**Tags:** grafana, flux\
**Created:** [November 9, 2023, 11:34am UTC](https://community.influxdata.com/t/performant-way-for-conditional-filter/32087 "2023-11-09T11:34:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![norkler](https://avatars.discourse-cdn.com/v4/letter/n/dbc845/32.png) [@norkler](https://community.influxdata.com/u/norkler)\
**Post date:** [November 9, 2023, 11:34am UTC](https://community.influxdata.com/t/performant-way-for-conditional-filter/32087/1 "2023-11-09T11:34:07Z")

</div>

Hi there,  
I’m new to Flux and try to visualize my data in an easy way to the user.  
I’ve a lot of measurements and want to give the user the ability to filter for measurement (devicename in my case), customer and consumer. For each I got variables in Grafana, filled from queries e.g.

Variable: measurement

> import “influxdata/influxdb/schema”  
> schema.measurements(bucket: “buckeeet”)

Custom all value = “All”

Now if I want to see some data in grafana my query took \> 30 seconds

> from(bucket: “buckeeet”)  
> |\> range(start: v.timeRangeStart, stop: v.timeRangeStop)  
> |\> filter(fn: (r) =\>  
> (if “${measurements}” != “All” then r.\_measurement == “${measurements}” else r.\_measurement != “”) and  
> (if “${customer}” != “All” then r.customer == “${customer}” else r.customer != “”) and  
> (if “${consumer}” != “All” then r.consumer == “${consumer}” else r.consumer != “”) and  
> (r.\_field == “voltage”)  
> )  
> |\> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)  
> |\> yield(name: “mean”)

When the query is static and looks like the followed, I tooks 1 - 3 seconds (3 seconds for max series of 1000) to get a result.

> from(bucket: “buckeeet”)  
> |\> range(start: v.timeRangeStart, stop: v.timeRangeStop)  
> |\> filter(fn: (r) =\>  
> r.\_measurement != “” and  
> r.customer == “secret” and  
> r.consumer != “” and  
> r.\_field == “voltage”  
> )  
> |\> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)  
> |\> yield(name: “mean”)

Can someone help me to optimize it? I don’t found a solution online.

---

<div class="post-metadata">

**Author:** ![norkler](https://avatars.discourse-cdn.com/v4/letter/n/dbc845/32.png) [@norkler](https://community.influxdata.com/u/norkler)\
**Post date:** [November 9, 2023, 12:21pm UTC](https://community.influxdata.com/t/performant-way-for-conditional-filter/32087/2 "2023-11-09T12:21:37Z")

</div>

This post helped me out:

> [@Show multiple value of variable on dashabord](https://community.influxdata.com/t/show-multiple-value-of-variable-on-dashabord/31747/2):
>
> Hi @Indeed_1 In Grafana, this works for me: Example is a variable for “region” which has multiple values:

Next Problem to solve: Grafana is only storing 1k values in their variables.

This topic here could be closed

---

<div class="post-metadata">

**Author:** ![Anaisdg](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/anaisdg/32/6401_2.png) [@Anaisdg](https://community.influxdata.com/u/Anaisdg)\
**Post date:** [November 15, 2023, 12:09am UTC](https://community.influxdata.com/t/performant-way-for-conditional-filter/32087/3 "2023-11-15T00:09:59Z")

</div>

@norkler thank you for sharing your answer with the community! I appreciate it.
