# \[Help\] Dividing two columns without Aggregation

**URL:** <https://discuss.elastic.co/t/help-dividing-two-columns-without-aggregation/162498>\
**Category:** Kibana\
**Created:** [December 31, 2018, 10:24am UTC](https://discuss.elastic.co/t/help-dividing-two-columns-without-aggregation/162498 "2018-12-31T10:24:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chapost1](https://avatars.discourse-cdn.com/v4/letter/c/7feea3/32.png) [@chapost1](https://discuss.elastic.co/u/chapost1)\
**Post date:** [December 31, 2018, 10:24am UTC](https://discuss.elastic.co/t/help-dividing-two-columns-without-aggregation/162498/1 "2018-12-31T10:24:19Z")

</div>

Hello all,

i'm enjoying using kibana but in the last few days i'm struggling in achiving a task which I thought is going to be simple.

I have an index which have couple of fields indexed: ['revenue', 'spend'].. in order to create profit field to display in data table I just had to create a scripted field to calc revenue - spend..  
so far so good. but when i'm trying to get margin.. which is profit / revenue, I can't do that.

is there any approach to achive a new field which will calc :

((total\_revenue\_for\_all\_docs - total\_spend\_for\_all\_docs) / total\_revenue\_for\_all\_docs ) \* 100  
and not aggregating anything after that.

best is going to be in data table. any another approach will be blessed.

sample dev tools query which succeed but I cannot manage it to happen in visualization:

```
GET /data_collector/_search
{ 
    "size": 0,
              "query": {
            "bool": {
              "must": [
                {
                  "match": {
                    "campaign.keyword": "censored"
                  }
                },
                {
                  "range": {
"date": {
            "gte": "now/d",
            "lt": "now+1d/d"
          }
                  }
                }
              ]
            }
          },
    "aggs" : {
        "sales_per_month" : {
            "date_histogram" : {
                "field" : "date",
                "interval" : "day"
            },
            "aggs": {
                "total_revenue": {
                    "sum": {
                        "field": "revenue"
                    }
                },
                                "total_spend": {
                    "sum": {
                        "field": "spend"
                    }
                },
                "margin": {
                    "bucket_script": {
                        "buckets_path": {
                          "totalRevenue": "total_revenue",
                          "totalSpend": "total_spend"
                        },
                        "script": "def margin = (params.totalRevenue - params.totalSpend) / params.totalRevenue * 100; return (double)Math.round(margin * 100) / 100;"
                    }
                }
            }
        }
    }
}

```

thanks!

---

<div class="post-metadata">

**Author:** ![Marius\_Dragomir](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marius_dragomir/32/42087_2.png) [@Marius\_Dragomir](https://discuss.elastic.co/u/Marius_Dragomir)\
**Post date:** [January 7, 2019, 2:51pm UTC](https://discuss.elastic.co/t/help-dividing-two-columns-without-aggregation/162498/2 "2019-01-07T14:51:46Z")

</div>

You can use bucket scripts and math by using Visual Builder. I'd recommend using the Math aggregation to calculate the margin.

---

<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:** [February 4, 2019, 2:51pm UTC](https://discuss.elastic.co/t/help-dividing-two-columns-without-aggregation/162498/3 "2019-02-04T14:51:47Z")

</div>

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