# Show column as percentage of another column

**URL:** https://discuss.elastic.co/t/show-column-as-percentage-of-another-column/113149
**Category:** Kibana
**Created:** [December 25, 2017, 12:05pm UTC](https://discuss.elastic.co/t/show-column-as-percentage-of-another-column/113149 "2017-12-25T12:05:10Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![EatYourCake](https://avatars.discourse-cdn.com/v4/letter/e/c4cdca/32.png) [@EatYourCake](https://discuss.elastic.co/u/EatYourCake)
#### Post date: [December 25, 2017, 12:05pm UTC](https://discuss.elastic.co/t/show-column-as-percentage-of-another-column/113149/1 "2017-12-25T12:05:10Z")

</div>

Hi,  
I am trying to build a visualization with an aggregate percentage, and I can't find a way to show the percentages.  
Each document has three fields that are relevant to my visualization:  
day : date  
number\_of\_tokens : int  
tokens\_matched: list of strings

I'd like to have a table with one row per day, the sum of number\_of\_tokens for all documents on that day, and the percentage of tokens\_matched out of the sum of number\_of\_tokens.

For example, for these 2 documents:  
{day = 2017-12-25, number\_of\_tokens = 10, tokens\_matched = ["a", "big", "cat"]}  
{day = 2017-12-25, number\_of\_tokens = 5, tokens\_matched = ["nice", "dog"]}

The table would look like:  
2017-12-25, 15, 33.3%

I can't figure out how to get the percentage column, I can get the actual number (in this case 5) by summing the size of the lists, but I can't get it as a percentage of the total column.

Any help would be greatly appreciated!  
Thanks 🙂

---

<div class="post-metadata">

### Author: ![tsullivan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tsullivan/32/31077_2.png) [@tsullivan](https://discuss.elastic.co/u/tsullivan)
#### Post date: [December 26, 2017, 9:14pm UTC](https://discuss.elastic.co/t/show-column-as-percentage-of-another-column/113149/2 "2017-12-26T21:14:13Z")

</div>

In the example result you gave, there's a column calculated as `33.3%` and it looks like that comes from:

- sum of tokens: 15 (10 + 5)
- number of tokens matched: 5 (3 + 2)
- percentage: 33.3 (5 out of 15)

In order to get the inputs for that calculation, I think your index needs a field for `number_of_tokens_matched` which will be an integer, because you won't be able to just use the length of the `tokens_matched` field, as far as I know.

I just wanted to give you that advice, but I'll keep exploring this question a bit to see what you can do once you have a "numerator" field for the percentage.

---

<div class="post-metadata">

### Author: ![tsullivan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tsullivan/32/31077_2.png) [@tsullivan](https://discuss.elastic.co/u/tsullivan)
#### Post date: [December 26, 2017, 9:47pm UTC](https://discuss.elastic.co/t/show-column-as-percentage-of-another-column/113149/3 "2017-12-26T21:47:50Z")

</div>

Hi again,

Unfortunately it looks like this might not be feasible to do as a table with the tools available in Kibana, but I think you would be able to achieve it as a line chart in either Time Series Visual Builder or Timelion.

In TSVB, you'll set up the 2 metrics to do the ratio on, and then use a Bucket Script aggregation:

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

For Timelion, you can refer to this discussion: [https://discuss.elastic.co/t/how-to-show-ratio-of-2-fields-which-are-result-of-2-filters-in-a-bar-chart](https://discuss.elastic.co/t/how-to-show-ratio-of-2-fields-which-are-result-of-2-filters-in-a-bar-chart). Basically you'll want to use an expression like:

```auto
.es(*, timefield="day", metric=sum:number_of_tokens_matched).divide(.es(*, timefield="day", metric=sum:number_of_tokens))

```

---

<div class="post-metadata">

### Author: ![EatYourCake](https://avatars.discourse-cdn.com/v4/letter/e/c4cdca/32.png) [@EatYourCake](https://discuss.elastic.co/u/EatYourCake)
#### Post date: [December 27, 2017, 2:28pm UTC](https://discuss.elastic.co/t/show-column-as-percentage-of-another-column/113149/4 "2017-12-27T14:28:49Z")

</div>

Hi Tim,  
Thanks for the quick response! Both options worked well, so I will go with one of them.

---

<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 24, 2018, 2:29pm UTC](https://discuss.elastic.co/t/show-column-as-percentage-of-another-column/113149/5 "2018-01-24T14:29:06Z")

</div>

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