How to use multi-value custom variable in query alias?

Hi all,

I am trying to add multiple measurements from InfluxDB with one query using custom variables. For my custom variable, I use this setup:


I add the variable to the query in the following way:
image

This works as expected and it adds a entry to the graph for each selected value of the custom variable.

My problem is that the names of the entries in the graph aren’t very readable, these for example result in: base_devices.mean {name: 0_Meter}
I would like to change it such that the entries are named according to the keys set in the custom variable (eg Basecase and Basecase Heat), but I can’t figure out how to achieve this.

I tried setting the Alias field to ${variable:text} but this just results in “Basecase + Basecase Heat”. Is there a way to make the query select the matching value from the custom variable?

Thanks in advance! If you need any additional information, please let me know. I am using Grafana v8.5.13.

Welcome @finn to the Grafana forum.

Have you considered using overrides to simplify the display names, like this?

Thank you for the quick response. I have looked into that and it works fine. However I am looking for a way to automate the naming of the display names. Using the override, we still have to change the naming for each variable entry manually.

Maybe a “Rename field by regex” override?

Yes, I manage to extract for example base_devices with the regex rename. Is it possible to use this value to get the corresponding key? Or if I switch the values and keys in the variable around, use “base_devices” as the key to get the corresponding value from variable?

Is this possible in Grafana?

To elaborate a bit more on this. I would like the display names to have to correct value automatically.

So either that I can insert the variable name (with some additional formatting if necessary) in the Alias field. Such that when the query generates multiple graphs from the multi-valued variable values, the corresponding text of the variable is used as the display name.

Or by renaming the display names using the text from variable. And then somehow get the correct text from a given value (which might be extracted from the display names using regex). So for the example, you could use “base_devices” to get “Basecase”.

Is this possible to do within Grafana?

Hi @finn,

If you’re using repeating visualisations based on the variable, you can use the ${variable_name:text} formatter as the Alias value to convert the raw variable value into it’s display name representation.

If you’re not using repeated visualisations then I do not believe there is currently a way to convert the multi-value into individual display names, apart from the more manual regex approach that @grant2 suggested.

1 Like

I have exactly the same problem to name and color my multi-value variable selection in one chart.
Is there no other way to do it?

It is working with repeat options - thanks for the hint to take the variable in Label,Legend and Ttitle! How is it working with different colors?

BUT i need them in one chart to compare them much better… :slight_smile:
Thanks for helping.

EDIT:

Can you please make an example for regex?

These dashboards contain some different examples of Rename by Regex:

1 Like

(post deleted by author)

Renaming fields with Regex is working with variables - pretty cool, but how to assign a color to the fields? They have all the same color in the panel…

How to compare them? I thought multi-vars did the trick?

Ok, i think i got it working how i aspected. I had to transform the multi-variable field with Partitions by value - now i have them individually

Thanks for all the hints!