Grafana Expressions Doesn't Work

  • What Grafana version and what operating system are you using?
    V9.2.2
  • What are you trying to achieve?
    I Have a Expression Query
($2BAvailability + $2CAvailability) / 2

The expression is simply add the 2 query and then get the average by diving it by 2.

  • How are you trying to achieve it?
    Using the expression query in grafana panel.

  • What happened?
    When either of the Query $2BAvailability or $2CAvailability has no value, the expression doesn’t work.

  • What did you expect to happen?
    If there is no data either of $2BAvailability or $2CAvailability it should still show values, for example if $2BAvailability has no value but the $2CAvailability is 100, then it should equate to (0 + 100) / 2 = 50.

1 Like

“No data” value is not the same as 0 value, so expression is correct (from the math point of view). It is only your use case that no data = 0, so prepare it for expression, e. g. modify query/add transformatiom/… which will return 0 instead of no data

Hi thank you for clarification.

I am using influxdb with influxQL, so when there is no data introduced, the default influxdb datasource has no capability to make it zero (as far as I know), it can just fill null or no values in between.

If this is the case, how can I use transformation for this?

I don’t know. Be creative. Idea (so you must implement/improve/verify/abandon it): additional query, which will be always 0, transformation which will “merge” (I’m not meaning “merge” transformation exactly) that flux and 0 query - result must not a have “no data” but 0 values.

Grafana expressions may return null when a data point is missing. To handle this, use the default function to replace null values with 0. Try modifying your expression like this:

((default($2BAvailability, 0) + default($2CAvailability, 0)) / 2)

This ensures that if either query has no value, it is treated as 0, allowing the calculation to proceed correctly.

Hello thanks for this, but applying it gives me error on “No data query”.
I wonder where you get the

default($QueryVariable, Value:number) 

I am trying to look at its documentation, I never seen something like this.

Yeah, some people/bots post LLM generated answers, which are not correct.

Hi @rikutozumoko

Recall what Jan wrote:

So you need to find a way to convert No Data to 0. This could be done in InfluxQL or in Grafana. In Grafana, there are probably several different ways, but one easy one might be to have expression B and C, and then a third expression to obtain the mean of B and C.