# Kibana Calculations Give Wrong Results

**URL:** <https://discuss.elastic.co/t/kibana-calculations-give-wrong-results/339778>\
**Category:** Kibana\
**Tags:** lens\
**Created:** [August 1, 2023, 9:12am UTC](https://discuss.elastic.co/t/kibana-calculations-give-wrong-results/339778 "2023-08-01T09:12:05Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nabeel\_Ahmed\_NAK](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nabeel_ahmed_nak/32/133673_2.png) [@Nabeel\_Ahmed\_NAK](https://discuss.elastic.co/u/Nabeel_Ahmed_NAK)\
**Post date:** [August 1, 2023, 9:12am UTC](https://discuss.elastic.co/t/kibana-calculations-give-wrong-results/339778/1 "2023-08-01T09:12:05Z")

</div>

I'm Getting wrong counts in version 8.5.0  
Time range is selected absolute values.  
The count of records are **102,118** hits  
When I create it's widget metrics of count it's same as expected: **102118**  
When I create unique count widget i got **102274**.  
How It can be true where as total documents are **102118** and it's unique count can not be greater than it's count of documents. Time range is selected same (fixed).Is anyone also getting same issue of calculation?

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/a/8/a88d6c43e50e446fede7b23b6aad1ced77a0fee1.png)  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/1/6/16315490946880fa04dfc4be7fa4629865ab495a.png)  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/4/8/48a61a7bdc5b0b91d5db497b58414953374e413d.png)

---

<div class="post-metadata">

**Author:** ![Marco\_Liberati](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marco_liberati/32/82953_2.png) [@Marco\_Liberati](https://discuss.elastic.co/u/Marco_Liberati)\
**Post date:** [August 1, 2023, 9:21am UTC](https://discuss.elastic.co/t/kibana-calculations-give-wrong-results/339778/2 "2023-08-01T09:21:39Z")

</div>

Hi @Nabeel_Ahmed_NAK

one possible answer to that could be that the unique count operation is applied to a field with array values in it.  
Can you check that there are no array values within the `fields.transaction_id` (use Discover for that)?

If the field contains array values then there are more values than documents, hence the divergence of the two results.

---

<div class="post-metadata">

**Author:** ![Nabeel\_Ahmed\_NAK](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nabeel_ahmed_nak/32/133673_2.png) [@Nabeel\_Ahmed\_NAK](https://discuss.elastic.co/u/Nabeel_Ahmed_NAK)\
**Post date:** [August 1, 2023, 9:27am UTC](https://discuss.elastic.co/t/kibana-calculations-give-wrong-results/339778/3 "2023-08-01T09:27:48Z")

</div>

> [@Marco\_Liberati](#):
>
> one possible answer to that could be that the unique count operation is applied to a field with array values in it.

It does not contain array. it's type is long. It works correctly if I narrow down it's time range where as if I increase time range I got wrong calculation not even this calculation but more widgets same like this in different indices.

---

<div class="post-metadata">

**Author:** ![Nabeel\_Ahmed\_NAK](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nabeel_ahmed_nak/32/133673_2.png) [@Nabeel\_Ahmed\_NAK](https://discuss.elastic.co/u/Nabeel_Ahmed_NAK)\
**Post date:** [August 1, 2023, 9:30am UTC](https://discuss.elastic.co/t/kibana-calculations-give-wrong-results/339778/4 "2023-08-01T09:30:34Z")

</div>

Even It's latest transform index where I use fields.transaction\_id as a unique key.

---

<div class="post-metadata">

**Author:** ![Marco\_Liberati](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marco_liberati/32/82953_2.png) [@Marco\_Liberati](https://discuss.elastic.co/u/Marco_Liberati)\
**Post date:** [August 1, 2023, 9:41am UTC](https://discuss.elastic.co/t/kibana-calculations-give-wrong-results/339778/5 "2023-08-01T09:41:27Z")

</div>

Another factor to keep in mind is that the ES `Cardinality` aggregation used under the hood for the unique count in Lens provides "an approximate count of distinct values" as documented: [Cardinality aggregation | Elasticsearch Guide [8.9] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-cardinality-aggregation.html#_counts_are_approximate)

---

<div class="post-metadata">

**Author:** ![Nabeel\_Ahmed\_NAK](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nabeel_ahmed_nak/32/133673_2.png) [@Nabeel\_Ahmed\_NAK](https://discuss.elastic.co/u/Nabeel_Ahmed_NAK)\
**Post date:** [August 1, 2023, 9:48am UTC](https://discuss.elastic.co/t/kibana-calculations-give-wrong-results/339778/6 "2023-08-01T09:48:20Z")

</div>

More explanation:  
AS you can see 30 mints data give correct result. time range: Jul 31, 2023 @ 00:00:00.000 to Jul 31, 2023 @ 01:00:00.000

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/c/5/c5033032ae8d60a5e03da6012b69abe9679a1b43.png)  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/7/d/7d1d7fcfb585d491ea5337e2ccd43fa7f06ac7da.png)

AND Jul 31, 2023 @ 00:00:00.000 To Jul 31, 2023 @ 00:30:00.000

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/5/6/56601d0a1d5fd74a1297e489016019fd65354584.png)  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/4/4/4497be46dcb0c030812ba4ae85bce6894c5893f3.png)

AS you can see answer is correct on 30 mints interval.  
now I will compile both time 30 mints time interval and create widget of 1 hour. time Jul 31, 2023 @ 00:00:00.000 to Jul 31, 2023 @ 01:00:00.000

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/b/1/b1961ffd8aebe9d0cc5225caa93790e0374349da.png)

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/c/5/c5cfca5227e6d4ba9883237997dd68bbf503f868.png)

answer is different 4 document extra.

---

<div class="post-metadata">

**Author:** ![Nabeel\_Ahmed\_NAK](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nabeel_ahmed_nak/32/133673_2.png) [@Nabeel\_Ahmed\_NAK](https://discuss.elastic.co/u/Nabeel_Ahmed_NAK)\
**Post date:** [August 2, 2023, 7:45am UTC](https://discuss.elastic.co/t/kibana-calculations-give-wrong-results/339778/7 "2023-08-02T07:45:29Z")

</div>

@Marco_Liberati can you look into it?

---

<div class="post-metadata">

**Author:** ![Marco\_Liberati](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marco_liberati/32/82953_2.png) [@Marco\_Liberati](https://discuss.elastic.co/u/Marco_Liberati)\
**Post date:** [August 2, 2023, 8:07am UTC](https://discuss.elastic.co/t/kibana-calculations-give-wrong-results/339778/8 "2023-08-02T08:07:16Z")

</div>

Try to open the `Inspect`, then go to the Request tab and click on `Open in Console`.

 ![Screenshot 2023-08-02 at 10.04.07](https://us1.discourse-cdn.com/elastic/original/3X/7/9/793a68856de89805f18fc9000d112dc0848bbd7c.jpeg)

In the request, can you add a `"precision_threshold": 15000` to the cardinality agg?

```auto
"cardinality": {
  "field": "fields.transaction_id.keyword",
  "precision_threshold": 15000
}

```

Then hit the send request and check the result: is that correct? If so, it's an approximation issue of the `cardinality` agg.

---

<div class="post-metadata">

**Author:** ![Nabeel\_Ahmed\_NAK](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nabeel_ahmed_nak/32/133673_2.png) [@Nabeel\_Ahmed\_NAK](https://discuss.elastic.co/u/Nabeel_Ahmed_NAK)\
**Post date:** [August 2, 2023, 9:37am UTC](https://discuss.elastic.co/t/kibana-calculations-give-wrong-results/339778/9 "2023-08-02T09:37:36Z")

</div>

> [@Marco\_Liberati](#):
>
> `"precision_threshold": 15000`

Hi @Marco_Liberati  
It's giving the correct unique count after adding precision\_threshold.

So how do I resolve it in the lens widget?

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/c/b/cb37dda0419ea8a6a35447eb01c1453e1e0bcb1f.png)

Without precision\_threshold result is incorrect.

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/d/b/db21007467e84eea8ccfffd083464d4320dca744.png)

Any solution?

Thanks

---

<div class="post-metadata">

**Author:** ![Nabeel\_Ahmed\_NAK](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nabeel_ahmed_nak/32/133673_2.png) [@Nabeel\_Ahmed\_NAK](https://discuss.elastic.co/u/Nabeel_Ahmed_NAK)\
**Post date:** [August 2, 2023, 9:45am UTC](https://discuss.elastic.co/t/kibana-calculations-give-wrong-results/339778/10 "2023-08-02T09:45:41Z")

</div>

@Marco_Liberati

---

<div class="post-metadata">

**Author:** ![Marco\_Liberati](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marco_liberati/32/82953_2.png) [@Marco\_Liberati](https://discuss.elastic.co/u/Marco_Liberati)\
**Post date:** [August 2, 2023, 9:49am UTC](https://discuss.elastic.co/t/kibana-calculations-give-wrong-results/339778/11 "2023-08-02T09:49:42Z")

</div>

`"precision_threshold"` is not something currently supported in Lens.  
There's already a feature request to add it to the editor you can follow here: [[Lens] Unique count aggregation should have control for precision threshold and warning about estimates · Issue #69832 · elastic/kibana · GitHub](https://github.com/elastic/kibana/issues/69832)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 30, 2023, 9:49am UTC](https://discuss.elastic.co/t/kibana-calculations-give-wrong-results/339778/12 "2023-08-30T09:49:59Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
