# Write a Scripted field that calculates a rate by dividing the count of 2 text fields

**URL:** <https://discuss.elastic.co/t/write-a-scripted-field-that-calculates-a-rate-by-dividing-the-count-of-2-text-fields/46966>\
**Category:** Kibana\
**Created:** [April 11, 2016, 9:37am UTC](https://discuss.elastic.co/t/write-a-scripted-field-that-calculates-a-rate-by-dividing-the-count-of-2-text-fields/46966 "2016-04-11T09:37:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeremy\_Colton](https://avatars.discourse-cdn.com/v4/letter/j/51bf81/32.png) [@Jeremy\_Colton](https://discuss.elastic.co/u/Jeremy_Colton)\
**Post date:** [April 11, 2016, 9:37am UTC](https://discuss.elastic.co/t/write-a-scripted-field-that-calculates-a-rate-by-dividing-the-count-of-2-text-fields/46966/1 "2016-04-11T09:37:20Z")

</div>

Hi,  
I collect many events from websites and visualize them using Kibana. The events include page loaded", "image clicked". My Kibana index contains an 'event' field that stores these event text values.

I want to show a graph of CTR on the images, so:  
total "image clicked" events / total number of all events.

Can I do this using a scripted field?

Many thanks.

---

<div class="post-metadata">

**Author:** ![LeeDr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leedr/32/9289_2.png) [@LeeDr](https://discuss.elastic.co/u/LeeDr)\
**Post date:** [April 11, 2016, 2:17pm UTC](https://discuss.elastic.co/t/write-a-scripted-field-that-calculates-a-rate-by-dividing-the-count-of-2-text-fields/46966/2 "2016-04-11T14:17:15Z")

</div>

Hi Jeremy,

I don't think you can do that with a scripted field in Kibana. Scripted fields can only use the numeric values in each individual document and it sounds like you want to use an aggregation of all the documents.

You might get what you are looking for by using the "Percentage" Chart Mode in the Options of your chart.

Also, I think the Timelion plugin for Kibana could probably show the ratio of the values to the aggregate count. Here's some links if you're not familiar with it;

> **[Timelion: The time series composer for Kibana
	  	 | Elastic](https://www.elastic.co/blog/timelion-timeline)**
>
> Update: The install commands towards the end of this post have been updated to include Kibana 5. You can also try Timelion through Elastic Cloud, our hosted & managed Elasticsearch and Kibana ser...

  
[https://www.elastic.co/videos/time-series-analysis-using-timelion](https://www.elastic.co/videos/time-series-analysis-using-timelion)

The later the Kibana build you're on, the better the Kibana to Timelion integration is.

Regards,  
Lee

---

<div class="post-metadata">

**Author:** ![Jeremy\_Colton](https://avatars.discourse-cdn.com/v4/letter/j/51bf81/32.png) [@Jeremy\_Colton](https://discuss.elastic.co/u/Jeremy_Colton)\
**Post date:** [April 11, 2016, 2:23pm UTC](https://discuss.elastic.co/t/write-a-scripted-field-that-calculates-a-rate-by-dividing-the-count-of-2-text-fields/46966/3 "2016-04-11T14:23:00Z")

</div>

Hi Lee,

Thanks for your reply...where do I find "percentage" chart mode eg for a histogram or line chart?

I've asked the creator of TimeLion how to do what I want as well since his video and page don't explain his new querying language.

---

<div class="post-metadata">

**Author:** ![LeeDr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leedr/32/9289_2.png) [@LeeDr](https://discuss.elastic.co/u/LeeDr)\
**Post date:** [April 11, 2016, 7:29pm UTC](https://discuss.elastic.co/t/write-a-scripted-field-that-calculates-a-rate-by-dividing-the-count-of-2-text-fields/46966/4 "2016-04-11T19:29:05Z")

</div>

Not all chart types have Percentage in their Options. A Line chart does not. For the chart types that do have it, you can find it in the options tab.

 ![](https://us1.discourse-cdn.com/elastic/original/2X/c/c01b2b9d30570f83e9f37105d802eccaad6f55a2.png)

---

<div class="post-metadata">

**Author:** ![Jeremy\_Colton](https://avatars.discourse-cdn.com/v4/letter/j/51bf81/32.png) [@Jeremy\_Colton](https://discuss.elastic.co/u/Jeremy_Colton)\
**Post date:** [April 13, 2016, 11:05am UTC](https://discuss.elastic.co/t/write-a-scripted-field-that-calculates-a-rate-by-dividing-the-count-of-2-text-fields/46966/5 "2016-04-13T11:05:40Z")

</div>

Hi,

Or even a pie chart will do - it provides percentages per slice!

Can TimeLion visuals be saved and used in a dashboard like regular visuals?

Many thanks.

---

<div class="post-metadata">

**Author:** ![LeeDr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leedr/32/9289_2.png) [@LeeDr](https://discuss.elastic.co/u/LeeDr)\
**Post date:** [April 13, 2016, 3:26pm UTC](https://discuss.elastic.co/t/write-a-scripted-field-that-calculates-a-rate-by-dividing-the-count-of-2-text-fields/46966/6 "2016-04-13T15:26:01Z")

</div>

Hi Jeremy,

You can use Timelion charts in Kibana 4.5 dashboards. After you install the TimeLion plugin into Kibana you can create a new "Timeseries" visualization (on the Kibana Visualize tab). And you can add that to your Dashboard.

When you're in the TimeLion plugin you can create charts and save sheets, but you cannot put those in a Kibana Dashboard.

---

<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:** [July 6, 2017, 1:56pm UTC](https://discuss.elastic.co/t/write-a-scripted-field-that-calculates-a-rate-by-dividing-the-count-of-2-text-fields/46966/7 "2017-07-06T13:56:12Z")

</div>


