If/else condition in query for dashboard variables

  • What Grafana version and what operating system are you using?
    Grafana v10.3.1 (00a22ff8b2)

  • What are you trying to achieve?
    By query, I’m trying to filter values from a variable based on a constant variable defined.

  • How are you trying to achieve it?

SHOW TAG VALUES 
FROM <measurement> 
WITH KEY = "animal" 
WHERE 
  (
    ("animal" =~ /"catXX.*/ AND /^$mamiffer$/ = "cat")
    OR 
    ("animal" =~ /"dogXX.*/ AND /^$mamiffer$/ = "dog")
  )
  • What happened?
    Empty output.

  • What did you expect to happen?
    I’m expecting that in the animal dropdown menu only certain animals are shown based on the value selected from the mamiffer dropdown menu (which is a custom variable with manually defined values), with a specific regex string.

The logic could be summarized as: select animal values from the database:
- if the “animal” starts with “dogXX” and the value of “mamiffer” is exactly “dog”.
- else if the “animal” starts with “catXX” and the value of “mamiffer” is exactly “cat”.

Thank you in advance for the help.

whats the output from the query inspector in the explorer?

The query returned no results… I managed to resolve the issue, which was primarily due to syntax errors.

Solution:

SHOW TAG VALUES 
FROM <measurement> 
WITH KEY = "animal" 
WHERE 
  (
    ('$mamiffer' = 'dog' AND "animal" =~ /^"dogXX/)
    OR 
    ('$mamiffer' = 'cat' AND "animal" =~ /^"catXX/)
  )

Thank you.