# How to change Grafana query dynamically based on given variable?

**URL:** <https://community.grafana.com/t/how-to-change-grafana-query-dynamically-based-on-given-variable/133250>\
**Category:** Prometheus\
**Tags:** promethues\
**Created:** [September 30, 2024, 2:04pm UTC](https://community.grafana.com/t/how-to-change-grafana-query-dynamically-based-on-given-variable/133250 "2024-09-30T14:04:59Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![dentrax](https://avatars.discourse-cdn.com/v4/letter/d/b782af/32.png) [@dentrax](https://community.grafana.com/u/dentrax)\
**Post date:** [September 30, 2024, 2:04pm UTC](https://community.grafana.com/t/how-to-change-grafana-query-dynamically-based-on-given-variable/133250/1 "2024-09-30T14:04:59Z")

</div>

- What Grafana version and what operating system are you using?

- **What** are you trying to achieve?

I’d like to change the queries in the panels, dynamically based on given variable (user input).

- **How** are you trying to achieve it?

I have defined a single “custom” Variable called “app”. My “Custom Options” is like: “foo : bar, bar : baz, baz : qux” with “Label and value” type.

So that I can use that label:

```auto
avg(avg_over_time(my_custom_metric_exposed_by_foo{job="${app}"}[$__rate_interval])) * 100

```

But I’d like to change the metric based on given label. Imagine `my_custom_metric_exposed_by_foo` is not exposed by `bar` and `baz`.

For example, if I choose the `bar`, i’d like to adjust to query to use `my_custom_metric_exposed_by_bar`.

- What happened?

How can I do that?

- What did you **expect** to happen?

- Can you copy/paste the configuration(s) that you are having problems with?

- Did you receive any errors in the Grafana UI or in related logs? If so, please tell us **exactly** what they were.

- Did you follow any online instructions? If so, what is the URL?

Yes, but no luck. Its outdated and for SQL queries:

- [Change Grafana query dynamically based on user inputs](https://community.grafana.com/t/change-grafana-query-dynamically-based-on-user-inputs/26416)

---

<div class="post-metadata">

**Author:** ![dawiddebowski](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/dawiddebowski/32/49435_2.png) [@dawiddebowski](https://community.grafana.com/u/dawiddebowski)\
**Post date:** [September 30, 2024, 4:43pm UTC](https://community.grafana.com/t/how-to-change-grafana-query-dynamically-based-on-given-variable/133250/2 "2024-09-30T16:43:42Z")

</div>

Hi,

What’s changing in your query? Only name of the metric? If so, you can also filter that in

```auto
{ __name__ =~"${var}"}

```

way, so your query could look like:

```auto
avg(avg_over_time({ __name__ =~"my_custom_metric_exposed_by_${app}", job="${app}"}[$__rate_interval])) * 100

```

If you need to change an entire query, you can map those to custom variable (given that the key value pairs are not used), e.g.:  
`foo : sum(rate(go_gc_cycles_automatic_gc_cycles_total[5m])), bar : vector(0)`  
and then just use the variables like that:

 ![image](https://us1.discourse-cdn.com/grafana/original/3X/1/0/10388b6ccdd398064c6732a5ffb6ffc3cb51e05e.png)

---

<div class="post-metadata">

**Author:** ![dentrax](https://avatars.discourse-cdn.com/v4/letter/d/b782af/32.png) [@dentrax](https://community.grafana.com/u/dentrax)\
**Post date:** [October 1, 2024, 6:26am UTC](https://community.grafana.com/t/how-to-change-grafana-query-dynamically-based-on-given-variable/133250/3 "2024-10-01T06:26:36Z")

</div>

Thank you!

Only the name of the metric is changing the query, right. And yes, I’d like to change the entire query but _per_ panel. I could pass the query as a _custom value_ but imagine there are _N_ panels and want to replace for _each_ one.

---

<div class="post-metadata">

**Author:** ![dentrax](https://avatars.discourse-cdn.com/v4/letter/d/b782af/32.png) [@dentrax](https://community.grafana.com/u/dentrax)\
**Post date:** [October 9, 2024, 6:08am UTC](https://community.grafana.com/t/how-to-change-grafana-query-dynamically-based-on-given-variable/133250/4 "2024-10-09T06:08:27Z")

</div>

Any thoughts on this? I’ve check the issues but couldn’t find anything.

---

<div class="post-metadata">

**Author:** ![dawiddebowski](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/dawiddebowski/32/49435_2.png) [@dawiddebowski](https://community.grafana.com/u/dawiddebowski)\
**Post date:** [October 12, 2024, 9:10am UTC](https://community.grafana.com/t/how-to-change-grafana-query-dynamically-based-on-given-variable/133250/5 "2024-10-12T09:10:03Z")

</div>

I don’t think I understand tbh. Let’s say you have a dashboard with two panels - panelA and panelB. Also, you have a variable there with possible values `foo`, `bar`, and `baz` (I know you have the key-value pairs, but for simplicity let’s omit those).  
panelA has query: `sum(rate(metric_foo))` and  
panelB has query `avg(rate(metric_foo))`

Now I change `foo` to `bar` in the variable list. What would you like to happen? Also please tag me in your response, cause I don’t get notifications and otherwise I’m not going to see your response 😅

---

<div class="post-metadata">

**Author:** ![dentrax](https://avatars.discourse-cdn.com/v4/letter/d/b782af/32.png) [@dentrax](https://community.grafana.com/u/dentrax)\
**Post date:** [October 21, 2024, 7:28pm UTC](https://community.grafana.com/t/how-to-change-grafana-query-dynamically-based-on-given-variable/133250/6 "2024-10-21T19:28:13Z")

</div>

Hey @dawiddebowski,

I have a dashboard with ONE panel. I have a variable with _N_ possible values. I want to change the entire query for the entire panel by given query.

---

<div class="post-metadata">

**Author:** ![dawiddebowski](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/dawiddebowski/32/49435_2.png) [@dawiddebowski](https://community.grafana.com/u/dawiddebowski)\
**Post date:** [October 22, 2024, 2:57pm UTC](https://community.grafana.com/t/how-to-change-grafana-query-dynamically-based-on-given-variable/133250/7 "2024-10-22T14:57:55Z")

</div>

With your variable you could do something like this then:

 ![image](https://us1.discourse-cdn.com/grafana/original/3X/a/0/a098e7905fcb8d34c5c0417015dab85e8836490e.png)

Then, upon changing the variable, the query would also change:

 ![image](https://us1.discourse-cdn.com/grafana/original/3X/a/c/ac68951fc3eb1bdbd54f940a228f51f0d8dc42ce.png)

---

<div class="post-metadata">

**Author:** ![dentrax](https://avatars.discourse-cdn.com/v4/letter/d/b782af/32.png) [@dentrax](https://community.grafana.com/u/dentrax)\
**Post date:** [October 25, 2024, 7:23am UTC](https://community.grafana.com/t/how-to-change-grafana-query-dynamically-based-on-given-variable/133250/8 "2024-10-25T07:23:11Z")

</div>

Thanks @dawiddebowski, but what if i have multiple panels and want to change on each based on provided variable? It seems this way would only support for one panel. (Since we can only pass one query per one variable)

---

<div class="post-metadata">

**Author:** ![dawiddebowski](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/dawiddebowski/32/49435_2.png) [@dawiddebowski](https://community.grafana.com/u/dawiddebowski)\
**Post date:** [October 25, 2024, 7:36am UTC](https://community.grafana.com/t/how-to-change-grafana-query-dynamically-based-on-given-variable/133250/9 "2024-10-25T07:36:09Z")

</div>

You could always create one hidden variable per one panel. But I fail to understand why would you want to change all the queries. What’s your use-case for that? I mean I would find it pretty confusing.

---

<div class="post-metadata">

**Author:** ![jamalallogie](https://avatars.discourse-cdn.com/v4/letter/j/67e7ee/32.png) [@jamalallogie](https://community.grafana.com/u/jamalallogie)\
**Post date:** [August 5, 2025, 11:14am UTC](https://community.grafana.com/t/how-to-change-grafana-query-dynamically-based-on-given-variable/133250/10 "2025-08-05T11:14:15Z")

</div>

Hi @dawiddebowski  
I have a use case which might be related/interesting/impossible;  
I have multiple panels and one variable type (ssd, hdd). Now this variable is used as is in the query labels, but I’d also like to set some other labels based on that, eg:  
If the variable is set to ssd, I want the label `type="$type"` but also some label `pool=~"1|3|5"`  
but if the variable is set to hdd I want `pool=~"2|4|6"`

Is this possible?

---

<div class="post-metadata">

**Author:** ![dawiddebowski](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/dawiddebowski/32/49435_2.png) [@dawiddebowski](https://community.grafana.com/u/dawiddebowski)\
**Post date:** [August 5, 2025, 5:42pm UTC](https://community.grafana.com/t/how-to-change-grafana-query-dynamically-based-on-given-variable/133250/11 "2025-08-05T17:42:53Z")

</div>

Hi, do you have infinity plugin installed?

If so, you can create a variable (e.g. named `filter`) of type query with infinity plugin, type CSV, parser Backend, source Inline (ofc you can also use json but I’m quite used to CSV in that case).  
As for the data, go with such csv:

```auto
Type,expression
Ssd,pool=~”1|3|5”
Hdd,pool=~”2|4|6”

```

Then toggle `computed columns, filter, group by` and in filter type in `contains(“${type}”, Type)` (first `${type}` refers to your variable, second Type refers to the column in csv). Run the query and it should work (I’ve never tested it for multi value variable though, so I’m not sure it would work).

In your query use (metric name is sample, I assume you’re using PromQL):  
`some_metric{type=“$type”, $filter}`. I’m not too familiar with vanilla PromQL but it should work (I’m mainly considered with the `, ` part but as long as you’re always setting `ssd` or `hdd` it should be fine). I think it should work for you (worked for me at least).

If you don’t have infinity plugin installed nor you want to install it, let me know, but I’m not sure I’ll come up with something then 😅

P.S. the column doesn’t have to be called Type, it can as well be type, I’m just typing it on IPad and I still haven’t figured out how not to capitalize the first letter of the new line 😃

---

<div class="post-metadata">

**Author:** ![jamalallogie](https://avatars.discourse-cdn.com/v4/letter/j/67e7ee/32.png) [@jamalallogie](https://community.grafana.com/u/jamalallogie)\
**Post date:** [August 8, 2025, 9:53am UTC](https://community.grafana.com/t/how-to-change-grafana-query-dynamically-based-on-given-variable/133250/12 "2025-08-08T09:53:30Z")

</div>

I will test that. Thank you very much!
