# Scripted field Avg

**URL:** <https://discuss.elastic.co/t/scripted-field-avg/244904>\
**Category:** Kibana\
**Tags:** elastic-stack-monitoring\
**Created:** [August 13, 2020, 3:10pm UTC](https://discuss.elastic.co/t/scripted-field-avg/244904 "2020-08-13T15:10:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shyrine](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shyrine/32/73833_2.png) [@Shyrine](https://discuss.elastic.co/u/Shyrine)\
**Post date:** [August 13, 2020, 3:10pm UTC](https://discuss.elastic.co/t/scripted-field-avg/244904/1 "2020-08-13T15:10:13Z")

</div>

Hi,

I'd like to calculate a scripted field that makes me do this :

Count(nb\_request)/UniqueCount(nb\_request)

But i have no idea of how the language paineless works.

Thanks

---

<div class="post-metadata">

**Author:** ![markov00](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/markov00/32/33316_2.png) [@markov00](https://discuss.elastic.co/u/markov00)\
**Post date:** [August 13, 2020, 3:20pm UTC](https://discuss.elastic.co/t/scripted-field-avg/244904/2 "2020-08-13T15:20:10Z")

</div>

Hi @Shyrine  
I'm not sure you can create a scripted field that use aggregation like counts in it. Scripted fields are meant to be to add a new field on each single document based on a script that can at least involve calculation over other fields on the same document.

If you need to do that kind of operation, the only way is to create a visualization in TSVB where you can create two aggregregations (count of nb\_request) and (unique count of bnb\_request) and use a bucket script to compute the ratio.

---

<div class="post-metadata">

**Author:** ![Shyrine](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shyrine/32/73833_2.png) [@Shyrine](https://discuss.elastic.co/u/Shyrine)\
**Post date:** [August 13, 2020, 3:31pm UTC](https://discuss.elastic.co/t/scripted-field-avg/244904/3 "2020-08-13T15:31:11Z")

</div>

Hi @markov00

Thank you for your respond. However, I can't find the unique count in the aggregations in TSVB 🙄

Actually what I'm trying to do is this :

 ![Capture](https://us1.discourse-cdn.com/elastic/original/3X/c/6/c6ba5fa75e2fc62e56786deccd6bf0a01e968d59.png)

I want to compare the correlation between 3 values, the pink and yellow ones have the same "scale" but the green one needs to be reduced in order to compare it with the two others, so i had this idea of calculating its average...

---

<div class="post-metadata">

**Author:** ![markov00](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/markov00/32/33316_2.png) [@markov00](https://discuss.elastic.co/u/markov00)\
**Post date:** [August 13, 2020, 3:35pm UTC](https://discuss.elastic.co/t/scripted-field-avg/244904/4 "2020-08-13T15:35:52Z")

</div>

You can probably use `Cardinality` on TSVB that will give you the unique count per bucket of a specific field

---

<div class="post-metadata">

**Author:** ![Shyrine](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shyrine/32/73833_2.png) [@Shyrine](https://discuss.elastic.co/u/Shyrine)\
**Post date:** [August 13, 2020, 3:47pm UTC](https://discuss.elastic.co/t/scripted-field-avg/244904/5 "2020-08-13T15:47:11Z")

</div>

I found how to do it, I had just to add an Average Bucket Aggregation. 😉

 ![Capture](https://us1.discourse-cdn.com/elastic/original/3X/8/9/89599eed180e940a9543f5f631d3a47a94d9f117.png)

---

<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:** [September 10, 2020, 3:47pm UTC](https://discuss.elastic.co/t/scripted-field-avg/244904/6 "2020-09-10T15:47:22Z")

</div>

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