# How to create a running average in Timelion?

**URL:** <https://discuss.elastic.co/t/how-to-create-a-running-average-in-timelion/207888>\
**Category:** Kibana\
**Tags:** timelion\
**Created:** [November 14, 2019, 12:32pm UTC](https://discuss.elastic.co/t/how-to-create-a-running-average-in-timelion/207888 "2019-11-14T12:32:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Koen\_Wouters](https://avatars.discourse-cdn.com/v4/letter/k/97f17d/32.png) [@Koen\_Wouters](https://discuss.elastic.co/u/Koen_Wouters)\
**Post date:** [November 14, 2019, 12:32pm UTC](https://discuss.elastic.co/t/how-to-create-a-running-average-in-timelion/207888/1 "2019-11-14T12:32:55Z")

</div>

Bit struggling with the timelion syntax so any help would be appreciated. I'm trying to get a running average (not moving average) over time so basically what I like to achieve is a calculation that would provide me the accumulated sum of a value divided by the total amount of documents at the defined timeframes.

Take below as simplified example, I get a few docs per day, each having a value. These need to be aggregated by day and then I want to divide the sum of accumulated values by the count of total accumulated documents. This running average (or accumulated average, not sure about the correct terminology) is what I want to plot.

day |doc | value | running avg  
1 | 1 | 4 | -  
1 | 2 | 3 | 3.5 \<\< day 1 = (4 +3)/2 docs  
2 | 1 | 3 | -  
2 | 2 | 5 | 3.75 \<\< day 2 = (4 + 3 + 3 + 5) / 4 docs  
3 | 1 | 2 | 3.4 \<\< day 3 = (4 + 3 + 3 + 5 + 2) / 5 docs

Any advice?

---

<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:** [November 14, 2019, 4:27pm UTC](https://discuss.elastic.co/t/how-to-create-a-running-average-in-timelion/207888/2 "2019-11-14T16:27:11Z")

</div>

I found similar discuss posts - may be you can refer here: [Simple moving average in Kibana over a date histogram help](https://discuss.elastic.co/t/simple-moving-average-in-kibana-over-a-date-histogram-help/77249) if this answers your question..if not, please ask again

Thanks  
Rashmi

---

<div class="post-metadata">

**Author:** ![Koen\_Wouters](https://avatars.discourse-cdn.com/v4/letter/k/97f17d/32.png) [@Koen\_Wouters](https://discuss.elastic.co/u/Koen_Wouters)\
**Post date:** [November 14, 2019, 4:44pm UTC](https://discuss.elastic.co/t/how-to-create-a-running-average-in-timelion/207888/3 "2019-11-14T16:44:56Z")

</div>

Hi Rashmi, thanks already but this is not what I look for. A moving average is basically just smoothing over a given window, what I need is an accumulated average from day one till now. This will therefore not be impacted as much by fluctuations and is more like a flattened trendline.

---

<div class="post-metadata">

**Author:** ![wylie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wylie/32/81794_2.png) [@wylie](https://discuss.elastic.co/u/wylie)\
**Post date:** [November 14, 2019, 5:44pm UTC](https://discuss.elastic.co/t/how-to-create-a-running-average-in-timelion/207888/4 "2019-11-14T17:44:01Z")

</div>

You can get a cumulative sum, and then divide it. For example:

`.es(index=kibana_sample_data_logs, metric='sum:bytes').cusum()`

That gives me:

 ![57](https://us1.discourse-cdn.com/elastic/original/3X/f/9/f99053664e71368b98d4a0f9251395aa1c995a6d.png)

This is then something you can divide, for example to divide the average bytes/document:

`.es(index=kibana_sample_data_logs, metric='sum:bytes').cusum().divide(.es(index=kibana_sample_data_logs))`

---

<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:** [December 12, 2019, 5:44pm UTC](https://discuss.elastic.co/t/how-to-create-a-running-average-in-timelion/207888/5 "2019-12-12T17:44:16Z")

</div>

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