"cannot divide by 0" TinyMath error in Visual builder

Hello Elastic community,

I am trying to build a metric in visual builder with 2 aggregations and a Math aggregation below them. But I am getting "Cannot divide by 0".

Below is my structure of data:

sr_number some_other_column
sr1 abcd
sr1 abcd
sr1 abcd
sr1 abcd
sr2 abcd
sr2 abcd

I want to calculate : (Count of docs / unique of sr_number) .i.e. average per sr.

Below is the error I get when using a visual builder:

Surprisingly the two variables: params.count_of_events and params.count_of_srs have the correct values and work when used in other math functions.

E.g: when using add(a,b)

When using mutitply(a,b)

The variables show values individually too:

Has anyone faced such issues ? Any advice/workarounds is appreciated.

Thank you

It's not possible to divide by zero in math. As the visual builder calculates this expression for every value in the date histogram, there seems to be a section of time where the document count was zero for the query.

That said, which version of Kibana are you using? I am unable to reproduce this in 7.12.

In the mean-time, you could potentially use the clamp function to prevent this from being zero.

Thank you @tsullivan

I am running ES v6.8.3. I wasn't aware that visual builder first creates a date histogram and then combines the result.

Using clamp function solved the error and now I am able to see the result of division.

However I have concerns with the accuracy of cardinality function as JSON input cannot be provided to visual builder as per this : Support json_input for visual builder or shard_size · Issue #23399 · elastic/kibana · GitHub

Hence I have decided to use Canvas dashboard for my implementation.