# Table visualization: calculate percentage

**URL:** <https://discuss.elastic.co/t/table-visualization-calculate-percentage/290304>\
**Category:** Kibana\
**Created:** [November 26, 2021, 3:25pm UTC](https://discuss.elastic.co/t/table-visualization-calculate-percentage/290304 "2021-11-26T15:25:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![simonlucalandi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/simonlucalandi/32/14990_2.png) [@simonlucalandi](https://discuss.elastic.co/u/simonlucalandi)\
**Post date:** [November 26, 2021, 3:25pm UTC](https://discuss.elastic.co/t/table-visualization-calculate-percentage/290304/1 "2021-11-26T15:25:01Z")

</div>

Hi all.  
Let's say that I have the following documents in an index

```auto
{
	"user": "foo",
    "cost": 10,
    "revenue": 12
},
{
	"user": "bar",
    "cost": 20,
    "revenue": 25
}
{
	"user": "foo",
    "cost": 15,
    "revenue": 20
},
{
	"user": "bar",
    "cost": 15,
    "revenue": 30
}

```

I need to create a Table visualization to show the `margin` and the `margin_percent` per user, where `margin` is `sum of revenue - sum of cost` and `margin_percent` is `margin / sum of revenue`.

For this example I should have:

| user | cost | revenue | margin | margin\_percent |
| foo | 25 | 32 | 7 | 21.87 |
| bar | 35 | 55 | 20 | 36.36 |

I can easily add a calculated field in the index, `margin = doc[revenue].value - doc[cost].value` and then aggregate using a `sum` function, but how to calculate the `margin_percent`?

Any suggestion is appreciated.

---

<div class="post-metadata">

**Author:** ![preetish\_P](https://avatars.discourse-cdn.com/v4/letter/p/77aa72/32.png) [@preetish\_P](https://discuss.elastic.co/u/preetish_P)\
**Post date:** [November 26, 2021, 4:46pm UTC](https://discuss.elastic.co/t/table-visualization-calculate-percentage/290304/2 "2021-11-26T16:46:23Z")

</div>

Hi @simonlucalandi ,

Create an enhanced table with 2 metrics: `Sum of cost` and `Sum of revenue`, then in the options tab create two computed columns to calculate : `(sum of cost) - (sum of revenue)` and `(new column)%(Sum of cost)`

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [November 26, 2021, 4:47pm UTC](https://discuss.elastic.co/t/table-visualization-calculate-percentage/290304/3 "2021-11-26T16:47:51Z")

</div>

Perhaps use Lens ... this is very simple using Formulas

 ![Screen Shot 2021-11-26 at 8.45.55 AM](https://us1.discourse-cdn.com/elastic/original/3X/c/f/cf096b11d17f8a0f495c35e396139c97a2851369.jpeg)

 ![Screen Shot 2021-11-26 at 8.46.59 AM](https://us1.discourse-cdn.com/elastic/original/3X/9/1/91e1d8d593871e72e1f94979d50e7c365fcb010e.png)

---

<div class="post-metadata">

**Author:** ![simonlucalandi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/simonlucalandi/32/14990_2.png) [@simonlucalandi](https://discuss.elastic.co/u/simonlucalandi)\
**Post date:** [November 26, 2021, 4:49pm UTC](https://discuss.elastic.co/t/table-visualization-calculate-percentage/290304/4 "2021-11-26T16:49:34Z")

</div>

thank you! I'll give it a try!

---

<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:** [December 24, 2021, 4:49pm UTC](https://discuss.elastic.co/t/table-visualization-calculate-percentage/290304/5 "2021-12-24T16:49:59Z")

</div>

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