Can I change graphs or use different queries for a graph based on custom variable?

Hi all, I am on Grafana v11.5.1

I want to have a graph, which based on the value of a dashboard variable, changes from plotting the absolute values or relative values.
So if dashboard variable $displayMode is set to Absolute, it should plot absolute values and if set to Percentage it should plot percentages.

The queries I am using are:
Absolute:

sum by (instance, name) (
  container_memory_usage_bytes{
    instance="xxx",
    name=~"worker-prod.*"
  }
)

Percentage:

sum by (instance, name) (
  container_memory_usage_bytes{
    instance="xxx",
    name=~"worker-prod.*",
  }
) / 
on(instance) group_left() (
  sum by (instance) (
    machine_memory_bytes
  )
) * 100

Of course the unit within Standard Options changes as well, for one it is bytes(IEC), and for the other it is Percent.

I tried a few things with transformations or field overrides, but can not seem to get it to work. If it is even possible?

Sure, create dashboard variable, e. g. key value variable, where you store that whole query - you must be careful when crrating that syntax - escaping all characters which may be causing a problem.

Then use that variable (raw value) as panel query.

Thanks for your reply.

Would it be a Custom Dashboard variable?

And what would it look like in the variable?

Yes,

Thanks for your help!

One more questions this raises is that I have 3 different graphs/visualizations of 3 different servers, so in this case instances.

sum by (instance, name) (
  container_memory_usage_bytes{
    instance="ip1",
    name=~"worker-prod.*"
  }
)
sum by (instance, name) (
  container_memory_usage_bytes{
    instance="ip2",
    name=~"worker-prod.*"
  }
)
sum by (instance, name) (
  container_memory_usage_bytes{
    instance="ip3",
    name=~"worker-prod.*"
  }
)

How would this be possible to keep the 3 ip addresses separated per graph.

But if you have 3 queries, A,B,C then you already have 3 series.
And they are considered separately.

Or would you like to have a separate graph for each IP?
Then create a variable with the three instances, create a graph and use the repeat function with the variable.

That can be a single query - use regex for instance filter. Example (fix any syntax/functional issues):

sum by (instance, name) (
  container_memory_usage_bytes{
    instance=~"ip1|ip2|ip3",
    name=~"worker-prod.*"
  }
)

They are considered separate and I need to visualize those separately at the same time. So I can not make variable for the ip addresses, because I won’t be able to look at them side-by-side.

So I want 3 seperate graphs, thus per ip address. And they need to be interchangeable between percent and absolute. I do not know how to achieve that :frowning:

Is it possible?

Did you try what I suggested?

I tried it in the PromQL query field for a graph, but it does not visualize anything then. Or should it be done in the Custom dashboard variable?

As I mentioned it is not a copy&paste - improve that query - learn how to work with regexp in the promql, so you will have just single query for multiple instances.