Can i create a multi column variable in grafana?

405 Views Asked by At

I want to create a query variable in grafana that contains data of more than one column. The database i use is Postgresql. In my dashboard i use this query a bunch o times so i would like to obtain the data as a variable once in order to make faster my dashboard.

Imagine the query for the variable is: SELECT id, name FROM country;

The variable saves the information as a collection of the data. For example: 1,2,3,spain,germany,usa. But not as a table.

How can i access to the data of the variable as if it was a table? Is it posible?

I have tried to make a normal query and treat it as table but i couldnt. i have treated the variable as ( $variable) , ${variable}:raw, $(variable.name), ${variable:raw}.name

2

There are 2 best solutions below

0
On BEST ANSWER

Grafana doesn't support multifield variables.

Closest to this supported feature would be separation of variable name (or key) and value.
How to achieve named variables within query is described in step 4 here:

Make sure that the query returns values named __text and __value as appropriate in your query syntax. For example, in SQL, you can use a query such as SELECT hostname AS __text, id AS __value FROM MyTable. Queries for other languages will vary depending on syntax.

So in your case it will be SELECT id as __value, name as __text FROM country. This will result in values from column name being shown in drop-down. And value from column id being substituted when using variable $variable. *

But this approach will only allow you to have two columns in use. If you need any more, you'll need to use some other approaches, as chained variables.


* : name of variable can still be used in other queries with syntax ${variable:text}, as described here.

1
On

=TRIM(MID(A2,FIND("",SUBSTITUTE(A2,",","",3))+1,255)), is the right way to extract the second text after the first underscore in each cell. Here's a breakdown of what each part of the formula does:

TRIM( ): This function removes any leading or trailing spaces from the extracted text. MID(A2, ... ): This function extracts a substring from cell A2. FIND("",SUBSTITUTE(A2,",","",3)): This part finds the position of the first underscore in cell A2, after replacing all commas with underscores. +1: We add 1 to the position of the underscore to skip over it and start extracting the text after it. 255: This specifies the maximum number of characters to extract. Here's why your formula works:

SUBSTITUTE(A2,",","",3): This replaces the first 3 occurrences of commas (",") with underscores ("") in cell A2. This ensures that we only find the first underscore and not any subsequent ones. FIND("_", ...): This finds the position of the first underscore in the modified string. MID(A2, ... ): This extracts the substring starting from the character after the found underscore and including 255 characters. TRIM( ... ): This removes any leading or trailing spaces from the extracted text, leaving only the desired second text.