# Average calculations not consistent

**URL:** <https://discuss.elastic.co/t/average-calculations-not-consistent/321739>\
**Category:** Kibana\
**Created:** [December 21, 2022, 9:01am UTC](https://discuss.elastic.co/t/average-calculations-not-consistent/321739 "2022-12-21T09:01:44Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wpq](https://avatars.discourse-cdn.com/v4/letter/w/3da27b/32.png) [@Wpq](https://discuss.elastic.co/u/Wpq)\
**Post date:** [December 21, 2022, 9:01am UTC](https://discuss.elastic.co/t/average-calculations-not-consistent/321739/1 "2022-12-21T09:01:44Z")

</div>

I have data of the type

```json
{
  "user": "john",
  "country": "japan",
  "applied": 1
},
{
  "user": "mary",
  "country": "japan",
  "applied": 1
},
{
  "user": "paul",
  "country": "japan",
  "applied": 0
},
{
  "user": "franck",
  "country": "france",
  "applied": 1
}

```

I would like to show a graph of countries with the percentage of applications.

To do so, I created a _Lens_ of type _Pie_ that I _Sliced by_ the _Top 50 values of `country`_, and _Sized by_ the average of `applied`.  
I used the top 50 despite having only about 10 countries in case there are new countries. When the number is above 10, adding more to the top does not change the result (which is logical).

So I have a pie that I thought was fine, except when I started to have a closer look at the numbers. Specifically, when clicking on the pie section for `japan`, I saw that it had a 67% of applications. This is the correct result (`(1 + 1 + 0)/3`).

The full pie, however, does not show 67% for `japan`, but a **smaller value**. With the data I have this is 25.08% (there are indeed 3 entries for `japan` in my data, but about 20k for other countries).

**What is the exact calculation used in _Lens_ of type _Pie_, _Sliced by_ the _Top N values of `X`_, and _Sized by_ the average of `Y`?**

---

<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:** [December 21, 2022, 9:42am UTC](https://discuss.elastic.co/t/average-calculations-not-consistent/321739/2 "2022-12-21T09:42:20Z")

</div>

Hi @Wpq

welcome to the Kibana community.

Do you have any advanced configuration enabled in the Top values?  
 ![Screenshot 2022-12-21 at 10.40.36](https://us1.discourse-cdn.com/elastic/original/3X/0/5/057c27e755cf699f6923083774d52a6aec188543.png)

If the Japan percentage value is lower, probably the `Other` bucket (if enabled) is taking up most of the space in the pie?

---

<div class="post-metadata">

**Author:** ![Wpq](https://avatars.discourse-cdn.com/v4/letter/w/3da27b/32.png) [@Wpq](https://discuss.elastic.co/u/Wpq)\
**Post date:** [December 21, 2022, 9:46am UTC](https://discuss.elastic.co/t/average-calculations-not-consistent/321739/3 "2022-12-21T09:46:59Z")

</div>

Thank you for your answer.

I did have "Group ... as Other" set up but it was redundant (I do no have "Other" as my Top 50 values cover all possible (10) values I have now). I tried to switch it on and off, as well as the "Accuracy" one but it did not change the problem.

---

<div class="post-metadata">

**Author:** ![Wpq](https://avatars.discourse-cdn.com/v4/letter/w/3da27b/32.png) [@Wpq](https://discuss.elastic.co/u/Wpq)\
**Post date:** [December 21, 2022, 9:55am UTC](https://discuss.elastic.co/t/average-calculations-not-consistent/321739/4 "2022-12-21T09:55:40Z")

</div>

Hmmm, going forward with your comment about Other, I picked a field where I have a large diversity (say, `age` which is not present in the example data above, but where I would have various values).

I chose Top 5 `age` and again sized by `applied` (which means "what is the % of people of age X that applied"). It gave a number.

I then tried to take more Top values, **and the relative % for the existing ones changed**. I do not understand this at all - how can changing the number of visible values change the ratio of `applied` (= the percentage, effectively) for a given value? It should always be the average of `applied` for that value, this is independent of anything else.

---

<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:** [December 21, 2022, 10:25am UTC](https://discuss.elastic.co/t/average-calculations-not-consistent/321739/5 "2022-12-21T10:25:32Z")

</div>

You mean having the `Other` option and increasing the Top value "value" makes an existing "slice" reduce its size?  
Or without the `Other` option enabled?

---

<div class="post-metadata">

**Author:** ![Wpq](https://avatars.discourse-cdn.com/v4/letter/w/3da27b/32.png) [@Wpq](https://discuss.elastic.co/u/Wpq)\
**Post date:** [December 21, 2022, 11:50am UTC](https://discuss.elastic.co/t/average-calculations-not-consistent/321739/6 "2022-12-21T11:50:22Z")

</div>

Both.

If I do not have `Other` and start at, say, Top 5 I have 5 slices in the pie. As I increase the number of slices, the percentage decreases (again just for clarity: this is the average of a `0` or `1` value, so it should be independent of anything else, including the number of displayed elements).

Same when I have `Other`, I just start with 5+1 slices (5 for the top ones, and `Other`) and changing the number of Top values changes the percentage.

* * *

_Note_: when you say

> reduce its size

I assume you mean what I mean above - that is that the displayed relative percentage is changed. I am not talking about the "visual size" of a slice (its angle if you wish). Sorry if this is obvious, I just wanted to make sure we are talking about the same thing.

---

<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:** [December 21, 2022, 1:45pm UTC](https://discuss.elastic.co/t/average-calculations-not-consistent/321739/7 "2022-12-21T13:45:45Z")

</div>

Can you post some pictures with the problem?  
I think I'm missing something obvious here, but maybe looking at it will make it clearer.

---

<div class="post-metadata">

**Author:** ![Wpq](https://avatars.discourse-cdn.com/v4/letter/w/3da27b/32.png) [@Wpq](https://discuss.elastic.co/u/Wpq)\
**Post date:** [December 21, 2022, 2:11pm UTC](https://discuss.elastic.co/t/average-calculations-not-consistent/321739/8 "2022-12-21T14:11:50Z")

</div>

Sure. Below is the general Pie with the top countries, sized by the average of a value `clicked` which is `0` or `1`

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

When looking at the details of "Kazakhstan", I see three entries:

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

As you can see, there are 2 x `1` and 1 x `0` so the average is 0.67 (or 67%).

---

<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:** [December 21, 2022, 2:51pm UTC](https://discuss.elastic.co/t/average-calculations-not-consistent/321739/9 "2022-12-21T14:51:29Z")

</div>

I think there's a misunderstanding of the pie visualization here: a pie visualization is used to represent portions (slices) to a whole. The total of a pie chart will be always 100%.  
Even if 67% of the time documents with `Kazakhstan` value have an `applied: 1` value, in the pie chart that will be normalized to the whole sum of all the slices represented.

It sounds like you are looking for something like a bar chart here to see how often `applied: 1` is set per country.  
I've reproduced something similar here with the Flight sample dataset where for each carrier I show the % of cancelled flights:

 ![Screenshot 2022-12-21 at 15.50.04](https://us1.discourse-cdn.com/elastic/original/3X/0/0/007dc69d5e05e2617a8111d67dbcce45a7f9c206.png)

As metric in this case I've used a formula to compute the %: `count(kql="applied: 1") / count()` would be in your case. As formatter I've set the percentage one.  
Would this work in your case?

---

<div class="post-metadata">

**Author:** ![Wpq](https://avatars.discourse-cdn.com/v4/letter/w/3da27b/32.png) [@Wpq](https://discuss.elastic.co/u/Wpq)\
**Post date:** [December 21, 2022, 2:58pm UTC](https://discuss.elastic.co/t/average-calculations-not-consistent/321739/10 "2022-12-21T14:58:43Z")

</div>

Thank you!

I actually did not want to use the pie visualization which is [usually the worst one to use](https://www.data-to-viz.com/caveat/pie.html). I tried with "Bar vertical **percentages**" and I was always getting 100% and gave up.

Looking at your example, I used the plain "Vertical bar" and changed the vertical display to % and it worked. **Thanks again**.

In retrospect, using the pie visualization made zero sense in my case as the percentages do not add up to 100% - I was kinda hoping that the angle would be a function of the value of the average.

---

<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:** [January 18, 2023, 2:58pm UTC](https://discuss.elastic.co/t/average-calculations-not-consistent/321739/11 "2023-01-18T14:58:44Z")

</div>

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