Expression Math divide not working as expected

In Grafana Cloud I have an Elasticsearch data source.

I want to get a success percentage value displayed from 2 queries:
A: count of all logs where the field ‘action’ equals ‘advance’ (total)
B: count of all logs where ‘action’ = ‘advance’ AND http.status_code = 200 (total success)

Then in the expression I want to do the basic Math: ($B / $A) * 100

I have also tried using a Binary Operation Transformation to add a new field from Calculation, but this has the same result.

I suspect that it could be influenced by the Value Options settings for my original 2 queries. Here I need to use a “Total” calculation to get the actual log counts for the queries:

And if we look at the table view it looks like the final calculation is just summed after being applied to each time series interval:

I need to find a way to isolate the totals for the original queries and then be able to get the “success percentage” for the totals, effectively bypassing the individual timeseries transform values.

Has anyone managed to get this to work?

Hi, it might be a sketch of a solution but you could go two ways:

  1. Remove grouping by daye histogram from the queries, so you won’t have time series and a total value (probably)
  2. Remove the expression from query tabs, use Reduce transformation to get total of the queries - the calculation Total in panel options is much simpler and made at the end of all the operations, hence those values
1 Like

Thanks Dawid.

I ended up using 3 Tranformations; Cumalative functions to Total the queries and then Binary operation for the divide. I also then set Unit to Percent (0.0-1.0). This worked great.

1 Like