# Bucket script aggregation in kibana visualization

**URL:** <https://discuss.elastic.co/t/bucket-script-aggregation-in-kibana-visualization/178336>\
**Category:** Elasticsearch\
**Created:** [April 24, 2019, 10:14pm UTC](https://discuss.elastic.co/t/bucket-script-aggregation-in-kibana-visualization/178336 "2019-04-24T22:14:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josip\_Cagalj](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/josip_cagalj/32/42899_2.png) [@Josip\_Cagalj](https://discuss.elastic.co/u/Josip_Cagalj)\
**Post date:** [April 24, 2019, 10:14pm UTC](https://discuss.elastic.co/t/bucket-script-aggregation-in-kibana-visualization/178336/1 "2019-04-24T22:14:34Z")

</div>

Hi, in my index I'm holding transaction data and I'm storing (among other stuff) fields `transactionType` and `amount`. I'm interested in Transaction types 1 (withdraw) and 2 (deposit). I'm trying to visualize the following data (percentage):  
`[(SUM of amount where transactionType is 1) / (SUM of amount where transactionType is 2)] * 100`  
I was thinking of creating a metric type of visualization (SUM on field `amount`) and then split it using filters on `transactionType` (1 and 2). Next, I need a new field with formula from above.  
I know of [Bucket Script Aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-script-aggregation.html) (didn't try it yet, need it in visualization) but how can it be used in a visualization?  
Any help is appreciated, thanks in advance.

EDIT here is the query which returns what I expect:

```
{
  "aggs": {
    "all_matching_docs": {
            "filters": {
        "filters": {
          "all": {
            "match_all": {}
          }
        }
      },
      "aggs": {
        "withdraws": {
          "filter": {
            "term": {"transactionType": 1}
          },
          "aggs": {
            "amount": {
              "sum": {"field": "amount"}
            }
          }
        },
        "deposits": {
          "filter": {
            "term": {"transactionType": 2}
          },
          "aggs": {
            "amount": {
              "sum": {"field": "amount"}
            }
          }
        },
        "prt": {
          "bucket_script": {
            "buckets_path": {
              "withdraws_sum": "withdraws>amount",
              "deposits_sum": "deposits>amount" 
           },
            "script": "(params.deposits_sum / params.withdraws_sum) * 100",
            "format": "0,00 %"
          }
        }
      }                
    }                  
  },
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "eventTime": {
              "format": "strict_date_optional_time",
              "gte": "2019-04-01T22:00:00.000Z",
              "lte": "2019-05-01T22:00:00.000Z"
            }
          }
        }
      ],
      "filter": [
        {
          "bool": {
            "should": [
              {
                "match": {
                  "status": 0
                }
              }
            ],
            "minimum_should_match": 1
          }
        }
      ]
    }
  }
}
```

---

<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:** [May 22, 2019, 10:14pm UTC](https://discuss.elastic.co/t/bucket-script-aggregation-in-kibana-visualization/178336/2 "2019-05-22T22:14:40Z")

</div>

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