0

I have mapped values in my Grafana Dashboards as shown in the following Link:

https://grafana.com/docs/grafana/latest/panels-visualizations/configure-value-mappings/

For example: Variable is called Name and it contains the values 1,2,3,4,5 from the Database. The mapping is as follows:

1 - Name1
2 - Name2
3 - Name3
4 - Name4
5 - Name5

I have applied the settings of that panel and saved the dashboard.

But in the dropdown Name, i still see the numbers instead of their mapped values (Name1, Name2, etc.)

Does anyone know why this is occuring and if i need to do something in addition for that matter ?

Thanks in advance :)

albdev
  • 43
  • 5
  • 1
    Value mapping are for panels, not variables. If you want variable to show in dropdown something other then its value you need to also add labels to it. More on how it can be done [here](https://grafana.com/docs/grafana/latest/datasources/mssql/template-variables/) – markalex Sep 01 '23 at 09:36
  • Thanks for the suggestion. After reading the link you sent here, i am still not able to map the values of the variable. The label section in the variable only changes the name of the variable itself, not its values in the dropdown. In my Databse i only have the numbers as values, so the names for those numbers i want to add first in grafana. I cannot extract them within the SQL Query. Do you know if that is possible ? – albdev Sep 01 '23 at 11:47
  • I can't say that I understand what you want to do. Could maybe a couple screenshots help? – markalex Sep 01 '23 at 11:52
  • I just posted the solution that worked for me. Thanks for the support :) – albdev Sep 01 '23 at 12:06

2 Answers2

0

To anyone interested:

I was creating the variable using a SQL Query. In order to map the new values as desired, the variable must be of the type Custom.

Inside the Custom options it is required to map the values with the scheme myKey : myValue and if multiple, they need to be separated by a comma.

For example:

Name1 : 1, Name2: 2, Name3: 3, Name4: 4, Name5: 5

Now in the Dropdown of the Variable Name it will be shown a list consisting of Name1, Name2, Name3, Name4, Name5. But when the variable Name is used in the SQL query of the Dashboard (ex. ... WHERE name = ($Name) ...), the number corresponding to that Name will be selected (1 for Name1).

albdev
  • 43
  • 5
0

You variable can still be of type Query.

To create variable with different labels and values you need to return columns with names __text and __value.

SELECT mynumber __value, myname __text 
FROM mytable

This way if you use ${variable:value} you will get "1" for you example, and ${variable:text} will get you "Name1".

markalex
  • 8,623
  • 2
  • 7
  • 32