# Kibana Canvas , Calculate percentage using to aggregations

**URL:** <https://discuss.elastic.co/t/kibana-canvas-calculate-percentage-using-to-aggregations/190702>\
**Category:** Kibana\
**Tags:** canvas\
**Created:** [July 16, 2019, 9:52am UTC](https://discuss.elastic.co/t/kibana-canvas-calculate-percentage-using-to-aggregations/190702 "2019-07-16T09:52:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![adityaPsl](https://avatars.discourse-cdn.com/v4/letter/a/e47c2d/32.png) [@adityaPsl](https://discuss.elastic.co/u/adityaPsl)\
**Post date:** [July 16, 2019, 9:52am UTC](https://discuss.elastic.co/t/kibana-canvas-calculate-percentage-using-to-aggregations/190702/1 "2019-07-16T09:52:07Z")

</div>

Hello Team,

I am working on a use case where I have to calculate percentage of two count aggregations

1. Get count of data from index1 with date filters
2. Get count of data from index1 with date and field filters
3. Calculate percentage using count1 and count2

Could you please help me understand how can we achieve this in Kibana Canvas.

Thank you for your help and support.

Thank you,  
Aditya

---

<div class="post-metadata">

**Author:** ![rashmi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rashmi/32/16391_2.png) [@rashmi](https://discuss.elastic.co/u/rashmi)\
**Post date:** [July 16, 2019, 2:20pm UTC](https://discuss.elastic.co/t/kibana-canvas-calculate-percentage-using-to-aggregations/190702/2 "2019-07-16T14:20:22Z")

</div>

@Catherine_Liu can you plz help here?

Thanks  
Rashmi

---

<div class="post-metadata">

**Author:** ![adityaPsl](https://avatars.discourse-cdn.com/v4/letter/a/e47c2d/32.png) [@adityaPsl](https://discuss.elastic.co/u/adityaPsl)\
**Post date:** [July 17, 2019, 4:58pm UTC](https://discuss.elastic.co/t/kibana-canvas-calculate-percentage-using-to-aggregations/190702/3 "2019-07-17T16:58:13Z")

</div>

Hello Team,

If not possible using Canvas can we achieve this functionality using Vega.  
Kibana version :6.7

Thank you for your help and support

Thank you,  
Aditya

---

<div class="post-metadata">

**Author:** ![Catherine\_Liu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/catherine_liu/32/34294_2.png) [@Catherine\_Liu](https://discuss.elastic.co/u/Catherine_Liu)\
**Post date:** [July 18, 2019, 10:21pm UTC](https://discuss.elastic.co/t/kibana-canvas-calculate-percentage-using-to-aggregations/190702/4 "2019-07-18T22:21:41Z")

</div>

This is a little complex, but I believe what you want is possible in Canvas. Essentially you need to query your index twice in the same expression to get the two different counts, and it also utilizes filter groups which was introduced in Kibana 7.2.0. I assume you wanted both counts to be filtered by the same time filter to filter your date field.

Here's the example using the ecommerce sample data set:

![Jul-18-2019%2015-16-52](https://us1.discourse-cdn.com/elastic/original/3X/0/c/0c1808d28aa0d1341ab3bb9c30c96194b11f14c0.gif)

Time filter on `order_date`:

```auto
timefilterControl compact=true column="order_date" filterGroup="order_date"
| render

```

Dropdown filter on `customer_gender`:

```auto
esdocs index="kibana_sample_data_ecommerce" fields="customer_gender"
| dropdownControl valueColumn="customer_gender" filterColumn="customer_gender" filterGroup="gender"
| render

```

Metric that displays the percentage of orders made by the gender you're filtering by

```auto
filters group="order_date" group="gender" ungrouped=true
| essql 
    query="SELECT order_date, customer_gender FROM \"kibana_sample_data_ecommerce\""
    count=10000
| math { 
    string "count(customer_gender)/" 
      {filters group="order_date" ungrouped=true 
       | essql query="SELECT order_date, customer_gender FROM \"kibana_sample_data_ecommerce\""
           count=10000
       | math "count(customer_gender)"
      } 
  }
| formatnumber "0%"
| metric
  metricFont={font size=48 family="'Open Sans', Helvetica, Arial, sans-serif" color="#000000" align="center" lHeight=48} 
  labelFont={font size=14 family="'Open Sans', Helvetica, Arial, sans-serif" color="#000000" align="center"}
| render

```

---

<div class="post-metadata">

**Author:** ![adityaPsl](https://avatars.discourse-cdn.com/v4/letter/a/e47c2d/32.png) [@adityaPsl](https://discuss.elastic.co/u/adityaPsl)\
**Post date:** [July 20, 2019, 7:21am UTC](https://discuss.elastic.co/t/kibana-canvas-calculate-percentage-using-to-aggregations/190702/5 "2019-07-20T07:21:23Z")

</div>

Thank you very much Catherine\_Liu.  
It helps understand few capabilities of Canvas.  
However i am struggling to achieve the functionality, i will try to explain.  
Lets say we are processing admissions ,We have date wise admissions count, and i want to calculate per day admission percentage with last 4 days admissions average count.

so if in last 4 days average admissions are 100 and today we have 10 admissions then percentage is 10%

so the first query would be : average count of last 4 days admissions.  
second query would be : todays admission count

percentage = firstQueryResult\*100/secondQueryResult.

can we add group by query to above use case like if i want to calculate class wise admission percentage

Could you please help me understand which visualsation would help in canvas.

Kibana Version :6.7

Thanks for your help and support.

Thank you,  
Aditya

---

<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:** [August 17, 2019, 7:30am UTC](https://discuss.elastic.co/t/kibana-canvas-calculate-percentage-using-to-aggregations/190702/6 "2019-08-17T07:30:40Z")

</div>

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