Dashboard variable: how to separate display name and value?

  • What Grafana version and what operating system are you using?
    Latest Grafana on Windows 11
  • What are you trying to achieve?
    I want to get a dashboard variable display name from a query in a dropdown box.
    But the selected value should not be the display name but other part from query result
  • How are you trying to achieve it?
    I see the regex section on the variable settings but don’t know how to use.
    Even the online help (provided by the link in the regex) does not help me.
  • What happened?
    Up to now nothing
  • What did you expect to happen?
    From my query I get a resultset like this:
    ‘ProdA 123’,
    ‘ProdB 2345’,
    ‘ProdC 45611’

Now the dropdownbox on the dashboard for this variable should show excatly this result as display name / selectable items:
‘ProdA 123’,
‘ProdB 2345’,
‘ProdC 45611’

But the selected values shoud be only a part of the dataset (means I need only the number) like this:
‘123’,
‘2345’,
‘45611’

This number I need for subsequent mysql queries.
So how to get display name and value separated?

I think the regex field in the variable options should make it, but I don’t know how to create the correct regex to separate display name and value.

Could anyone assist please? Thanks a lot!

key : value, 'show : hide

@yosiasz: thanks but I don’t know how this can help me.
I don’t get a dataset in key / value pairs.
I get exactly one column:

‘ProdA 123’,
‘ProdB 2345’,
‘ProdC 45611’

I think the regex in the variable settings is the right way, correct me if I am wrong.
As you can see here (from [Add variables | Grafana documentation]):

It seems one can separate display name from value. But I don’t know the correct regex to achieve this.

what kind of datasource is it? mysql, are these values in one column or separate columns?

It is mysql database.
But I cannot change the query itself since I myself don’t have acces to the database.
So I have to deal with the result as it is and use the regex option if that is a solution.

BTW:
What if I have access to the database, can I change the query to separate display name from value? (Just to improve my knowledge)

still not clear if these values are in one column or two columns

Sorry, I get exactly this result:

‘ProdA 123’,
‘ProdB 2345’,
‘ProdC 45611’

This means I get only one column (VARCHAR(20)) with 3 rows.

if you could change the sql query it would be

key = __text
value = __value

mocking your data in my grafana

select 'ProdA' as __text, 123 as __value union 
select 'ProdB',  2345 union
select 'ProdC',  45611 

image

have them change it to that, instead of fiddling with fragile regex

@yosiasz Thanks but since I haven’t access to the database I need to deal with the regex.

Does anybody know how the regex needs to be? Thanks!