# Negative time shift for moving average?

**URL:** <https://discuss.elastic.co/t/negative-time-shift-for-moving-average/305073>\
**Category:** Kibana\
**Created:** [May 18, 2022, 12:54pm UTC](https://discuss.elastic.co/t/negative-time-shift-for-moving-average/305073 "2022-05-18T12:54:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rusLukasRath](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ruslukasrath/32/105843_2.png) [@rusLukasRath](https://discuss.elastic.co/u/rusLukasRath)\
**Post date:** [May 18, 2022, 12:54pm UTC](https://discuss.elastic.co/t/negative-time-shift-for-moving-average/305073/1 "2022-05-18T12:54:54Z")

</div>

Hello together,

I'm having a little problem with one of the panels I have created.

On this panel I am showing a line with an daily availability percentage (green line). Additionally I have created an moving average (blue line) with a 30 day window size. Is it possible to time shift the moving average into the negative, e.g. -1d, in any way, because I want the moving average to have the drop on the same day the green line drops down.

Here with my current test data I have an availability drop on May 10th, but the moving average only reflects it the day later, May 11th.

 ![brave_2022-05-18_14-50-33](https://us1.discourse-cdn.com/elastic/original/3X/3/1/31d8fb3b21a4e6e2c0e757e3d855bf548f0ced9e.png)

Thank you already in advance!

---

<div class="post-metadata">

**Author:** ![flash1293](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flash1293/32/41227_2.png) [@flash1293](https://discuss.elastic.co/u/flash1293)\
**Post date:** [May 18, 2022, 5:03pm UTC](https://discuss.elastic.co/t/negative-time-shift-for-moving-average/305073/2 "2022-05-18T17:03:29Z")

</div>

Unfortunately it's not possible to shift a series negatively. It seems like the underlying issue is that the moving average in Kibana is trailing, which means the current value is not taken into account. However you should be able to fix this with a formula like this:

```auto
moving_average(count(), window=5)*(5/6) + count()/6

```

Basically take the moving average and then add another value to it. Does that work for your case?

---

<div class="post-metadata">

**Author:** ![rusLukasRath](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ruslukasrath/32/105843_2.png) [@rusLukasRath](https://discuss.elastic.co/u/rusLukasRath)\
**Post date:** [May 19, 2022, 11:10am UTC](https://discuss.elastic.co/t/negative-time-shift-for-moving-average/305073/3 "2022-05-19T11:10:32Z")

</div>

I've just applied it to my panel and it seems to be working 😃 But it smoothened out the line and it seems to not be dropping as hard as it was previously.

 ![brave_2022-05-19_13-05-24](https://us1.discourse-cdn.com/elastic/original/3X/9/b/9b1eaa16594c43b083ad56ac078cb0d945dfa62f.png)

```auto
moving_average(average(myvalue), window=30) * (30/31) + average(myvalue)/31

```

I hope that I have translated the values correctly for my panel ^^

---

<div class="post-metadata">

**Author:** ![flash1293](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flash1293/32/41227_2.png) [@flash1293](https://discuss.elastic.co/u/flash1293)\
**Post date:** [May 20, 2022, 7:07am UTC](https://discuss.elastic.co/t/negative-time-shift-for-moving-average/305073/4 "2022-05-20T07:07:04Z")

</div>

If you had a window of 30 before, then this effectively extended the window to 31 -

```auto
moving_average(average(myvalue), window=29) * (29/30) + average(myvalue)/30

```

would be a moving average with a window of 30 values including the current value and the 29 preceding ones.

---

<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:** [June 17, 2022, 7:07am UTC](https://discuss.elastic.co/t/negative-time-shift-for-moving-average/305073/5 "2022-06-17T07:07:20Z")

</div>

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