How to use a MySQL Query result as a variable in DisplayName

What Grafana version and what operating system are you using?
Grafana v10.3.3 (252761264e)

What are you trying to achieve?
I want to use part of my MySQL Result as a display name.

For example, I have a simple MySQL query that returns the last ID and Date of a record like so:

app_id app_date
1234 10th April, 12:40pm

I would like to use a “Stat” graph because of it’s style. I want to use the actual date as the label, by default it shows “app_id 1234”, I would like to replace “app_id” with the date from the query.

How are you trying to achieve it?
I’ve tried every variable and suggestion from ${__value} variations to “cell” etc. Cannot figure it out.

What happened?
Can’t get the date to show up as the label

Any help would be appreciated!

I have figured out a way to do this, I still think the whole DisplayName and variables is awful to work with… But I suppose with a ton of data sources it’s very general. If there is a way to basically filter down the “Query Inspector” result to get to it, that would be ideal…

How far I’ve done this so far is using Dashboard Variables, set one up then just follow Variable syntax | Grafana documentation

Pivot might be the way to go for this so that the column name is the dat and the row value is the numeric value



SELECT *
FROM  
(
  select 478195 as app_id, '2024-04-10' as [app_date]
) AS SourceTable  
PIVOT  
(  
  MAX(app_id)  
  FOR [app_date] IN ([2024-04-10])  
) AS PivotTable;

1 Like

Hi Yosiasz,

I’m not entirely sure how that would work, my basic query is this:

SELECT id AS app_id, created_at AS app_date
FROM db.table
ORDER BY id DESC
LIMIT 1

In the pivot, you have FOR [app_date] IN ([2024-04-10]) but i wouldn’t know the date, the date is dynamic from the table itself, it’s the time the record was created. So what would I have as my “in” part?

It would be so powerful if Grafana let you select a field using the query finder response object.

For example when I run my basic query and click on “Query Inspector”, when I nav down to: response > results > A > frames > 0 > data > values, I can see that values[0] is the ID and values[1] is the date string, but I can’t seem to grab these using any kind of ${__values[1]} etc.

You can then uae dynamic query.

Or try group by matrix transfornation

1 Like

This is a really nice clean solution, works for this situation, thank you!

1 Like