# In time series data in ElasticSearch, need to find the count of documents till a point at which the value changes

**URL:** https://discuss.elastic.co/t/in-time-series-data-in-elasticsearch-need-to-find-the-count-of-documents-till-a-point-at-which-the-value-changes/193343
**Category:** Elasticsearch
**Created:** [August 1, 2019, 2:17pm UTC](https://discuss.elastic.co/t/in-time-series-data-in-elasticsearch-need-to-find-the-count-of-documents-till-a-point-at-which-the-value-changes/193343 "2019-08-01T14:17:36Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![matterixn](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@matterixn](https://discuss.elastic.co/u/matterixn)
#### Post date: [August 1, 2019, 2:17pm UTC](https://discuss.elastic.co/t/in-time-series-data-in-elasticsearch-need-to-find-the-count-of-documents-till-a-point-at-which-the-value-changes/193343/1 "2019-08-01T14:17:36Z")

</div>

Lets say there are documents such as below, with these fields

```auto
"time": X, "value": 1, "key": M
"time": X-1, "value": 1, "key": M
"time": X-2, "value": 1, "key": M
"time": X-3, "value": 1, "key": M
"time": X-4, "value": 1, "key": M
"time": X-5, "value": 0, "key": M
"time": X-6, "value": 1, "key": M
"time": X-6, "value": 1, "key": M

```

There are multiple such documents for other "key".

I want to find all documents that currently at time X have the "value": 1 and the number of times they have "value" as "1", before it changed to anything else, grouped by "key".

In this case, I would like to see something in the line of:

```auto
{
 "key": M,
 "count": 5
}

```

---

<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: [August 29, 2019, 2:17pm UTC](https://discuss.elastic.co/t/in-time-series-data-in-elasticsearch-need-to-find-the-count-of-documents-till-a-point-at-which-the-value-changes/193343/2 "2019-08-29T14:17:57Z")

</div>

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