# Compare current data with previous day's data based on time

**URL:** https://discuss.elastic.co/t/compare-current-data-with-previous-days-data-based-on-time/320197
**Category:** Elasticsearch
**Created:** [November 30, 2022, 9:14pm UTC](https://discuss.elastic.co/t/compare-current-data-with-previous-days-data-based-on-time/320197 "2022-11-30T21:14:07Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![viveks](https://avatars.discourse-cdn.com/v4/letter/v/ccd318/32.png) [@viveks](https://discuss.elastic.co/u/viveks)
#### Post date: [November 30, 2022, 9:14pm UTC](https://discuss.elastic.co/t/compare-current-data-with-previous-days-data-based-on-time/320197/1 "2022-11-30T21:14:07Z")

</div>

Hi, I have events that are generated throughout the day based on API processing. These events are available in Elastic and are generated throughout the day. I am trying to figure out a way to create an alert when the number of events generated for the last hour is less than 30% when compared to the events generated for the same time period on the previous day.  
For example:  
11/15/2022 10-11AM: 2000 events  
11/15/2022 11-12PM: 1500 events  
and so on  
11/16/2022 10-11AM: 2500 events  
11/16/2022 11-12PM: 150 events (I want to alert for this data as the events generated are far less when compared to the same time on 11/15 - only 10%)

I was thinking of using derivative aggregation but since the time ranges that are being compared are not immediate ones, that doesn't seem to be an option. I appreciate any help or pointers in resolving this. Many thanks!

---

<div class="post-metadata">

### Author: ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)
#### Post date: [December 27, 2022, 5:49pm UTC](https://discuss.elastic.co/t/compare-current-data-with-previous-days-data-based-on-time/320197/2 "2022-12-27T17:49:05Z")

</div>

Hi @viveks Welcome to the community.

A couple thoughts, although I'm not sure there's an easy way to do exactly what you want.

My first thought is this is exactly what a single metric anomaly detection job does... It will learn the normal behavior of your API events and then create an anomaly which you can turn into an alert. When the behavior is not normal as compared to its learned baseline.

So if you have it daily / weekly cycles etc it will learn that and then alert when it does not match.

> **[Anomaly detection | Machine Learning in the Elastic Stack \[8.5\] | Elastic](https://www.elastic.co/guide/en/machine-learning/8.5/ml-ad-overview.html)**
>
> This tutorial shows you how to create anomaly detection jobs, interpret the results, and forecast future behavior in Kibana.

In Lens visualization you can graph exactly the formula.. you can create a graph using the formula and shift function...

Unfortunately today you cannot turn that directly into an alert but you'd be able to see it.

I'm poking around to see if formulas are coming to alerts.

You could probably write a watcher to do this, but would probably be quite complex and not my area of expertise.

---

<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: [January 24, 2023, 5:49pm UTC](https://discuss.elastic.co/t/compare-current-data-with-previous-days-data-based-on-time/320197/3 "2023-01-24T17:49:16Z")

</div>

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