Grafana math Expression where one of the operands is an empty Query

  • What Grafana version and what operating system are you using?

v11.3.2 on Linux

  • What are you trying to achieve?

I want to use an Expression to perform a calculation from a Sum Query which might have yielded empty results: $A / ($A + $B), where $B may have no data.

It’s an Elasticsearch query, so I can’t specify COALESCE or UNION. I don’t think there’s any trick I can do in the query.

  • How are you trying to achieve it?

I’m trying two Sum Queries A and B which both look approximately like this:

  • Query type > Metrics
  • Lucene Query > field_a : true
  • Metric > Sum > field_x
  • Group By > Terms > field_y

I have an Expression $Exp1 to identify whether $B is empty:

  • Operation > Classic condition (legacy)
  • WHEN count() OF $B HAS NO VALUE

And I have another Expression $Exp2, which I want to display in a Gauge visualization:
$A / ($A + ( ( 1 - $Exp1 ) * $B ) )

  • What happened?

When displaying $Exp2, the Gauge shows ‘no data’. However, when displaying $Exp1, the Gauge shows 1.

  • What did you expect to happen?

I expected (1 - $Exp) to evaluate to 0 in this case and make the expression equal $A / ($A + 0), and therefore display a value of 1 in the Gauge.

  • Can you copy/paste the configuration(s) that you are having problems with?

(I can’t share this. It’s for my employer, and we don’t share anything that references our database or code.)

  • Did you receive any errors in the Grafana UI or in related logs? If so, please tell us exactly what they were.

I get a red warning icon. Hovering over it says ‘Query error: 500’. I’ve checked and re-checked this so many times. I’ve typed it correctly; I just can’t reference my ‘Classic condition’ expression within another expression without getting this error.

  • Did you follow any online instructions? If so, what is the URL?

No, I have no instructions


You might advise me to just change the ‘No value’ option to the value that I want like so many related pages have advised. But that won’t satisfy my use case because I actually need to set this up for additional Queries as operands which might be empty, and I need to use different behaviour for cases where different operands are empty.