# Adding a new column to a data table from a calculation based on two different columns in the same row

**URL:** <https://discuss.elastic.co/t/adding-a-new-column-to-a-data-table-from-a-calculation-based-on-two-different-columns-in-the-same-row/202022>\
**Category:** Kibana\
**Created:** [October 2, 2019, 5:41pm UTC](https://discuss.elastic.co/t/adding-a-new-column-to-a-data-table-from-a-calculation-based-on-two-different-columns-in-the-same-row/202022 "2019-10-02T17:41:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amphagory](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amphagory/32/46127_2.png) [@Amphagory](https://discuss.elastic.co/u/Amphagory)\
**Post date:** [October 2, 2019, 5:41pm UTC](https://discuss.elastic.co/t/adding-a-new-column-to-a-data-table-from-a-calculation-based-on-two-different-columns-in-the-same-row/202022/1 "2019-10-02T17:41:06Z")

</div>

I was wondering if it was possible to add a new column to a data table in a Visualize from a calculation based on two different columns in the same row

Say I have two columns `Calls A` and `Calls B` I want to create a third column say `Total Calls` which is the sum of `Calls A + Calls B`.

`Call A` is a Metric sum of calls\_a  
`Call B` is a Metric sum of calls\_b

i.e.

```auto
       Calls A | Calls B | Total Calls
Ottawa 1 2 3
Toronto 5 4 9
Nunavik 0 0 0

```

---

<div class="post-metadata">

**Author:** ![vishal\_M](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vishal_m/32/50559_2.png) [@vishal\_M](https://discuss.elastic.co/u/vishal_M)\
**Post date:** [October 2, 2019, 6:12pm UTC](https://discuss.elastic.co/t/adding-a-new-column-to-a-data-table-from-a-calculation-based-on-two-different-columns-in-the-same-row/202022/2 "2019-10-02T18:12:51Z")

</div>

You can create a new script field ( type: painless ) . under Management / index patterns

Select the index pattern u want to write the script field...

def totalCalls = doc['Calls A'].value + doc ['Calls B'].value; return totalCalls;

---

<div class="post-metadata">

**Author:** ![Amphagory](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amphagory/32/46127_2.png) [@Amphagory](https://discuss.elastic.co/u/Amphagory)\
**Post date:** [October 2, 2019, 7:39pm UTC](https://discuss.elastic.co/t/adding-a-new-column-to-a-data-table-from-a-calculation-based-on-two-different-columns-in-the-same-row/202022/3 "2019-10-02T19:39:19Z")

</div>

Hello,

Thanks for your quick reply and it work perfectly. I have a follow up question.

If I wanted to create another new column call `Call A Percentage`, what would the script look like for that?

I think I'm close, but don't know what syntax to put around 'totalCalls ':  
def call\_a\_percentage = doc['Calls A'].value / 'totalCalls '; return call\_a\_percentage ;

Thanks again for your help!

---

<div class="post-metadata">

**Author:** ![vishal\_M](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vishal_m/32/50559_2.png) [@vishal\_M](https://discuss.elastic.co/u/vishal_M)\
**Post date:** [October 2, 2019, 8:10pm UTC](https://discuss.elastic.co/t/adding-a-new-column-to-a-data-table-from-a-calculation-based-on-two-different-columns-in-the-same-row/202022/4 "2019-10-02T20:10:11Z")

</div>

So, u want to format the value of "Call A Percentage" field ?

just select the field (under fields) and click pencil icon and change the format to percentage then click save field..

---

<div class="post-metadata">

**Author:** ![Amphagory](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amphagory/32/46127_2.png) [@Amphagory](https://discuss.elastic.co/u/Amphagory)\
**Post date:** [October 2, 2019, 9:12pm UTC](https://discuss.elastic.co/t/adding-a-new-column-to-a-data-table-from-a-calculation-based-on-two-different-columns-in-the-same-row/202022/5 "2019-10-02T21:12:21Z")

</div>

Actually I want to create another new column like the following, where I divide `Calls A` by `Total Calls`

```auto
       Calls A | Calls B | Total Calls | Call A Percentage
Ottawa 1 2 3 33.3%
Toronto 5 4 9 55.6%
Nunavik 0 0 0 0.0%

```

---

<div class="post-metadata">

**Author:** ![vishal\_M](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vishal_m/32/50559_2.png) [@vishal\_M](https://discuss.elastic.co/u/vishal_M)\
**Post date:** [October 3, 2019, 5:04pm UTC](https://discuss.elastic.co/t/adding-a-new-column-to-a-data-table-from-a-calculation-based-on-two-different-columns-in-the-same-row/202022/6 "2019-10-03T17:04:42Z")

</div>

Create a new script field "Call A Percentage" (Type: painless)

def call\_a\_percentage = doc['Calls A'].value / 'totalCalls ';  
click save

Then just select the " call\_a\_percentage "field (under fields) and click pencil icon and change the format to percentage then click save field..This should give you the Call A Percentage..

---

<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:** [October 31, 2019, 5:05pm UTC](https://discuss.elastic.co/t/adding-a-new-column-to-a-data-table-from-a-calculation-based-on-two-different-columns-in-the-same-row/202022/7 "2019-10-31T17:05:19Z")

</div>

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