# Cumulative sum with range limited date histogram

**URL:** https://discuss.elastic.co/t/cumulative-sum-with-range-limited-date-histogram/222564
**Category:** Elasticsearch
**Created:** [March 7, 2020, 3:04pm UTC](https://discuss.elastic.co/t/cumulative-sum-with-range-limited-date-histogram/222564 "2020-03-07T15:04:27Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![aksdb](https://avatars.discourse-cdn.com/v4/letter/a/6bbea6/32.png) [@aksdb](https://discuss.elastic.co/u/aksdb)
#### Post date: [March 7, 2020, 3:04pm UTC](https://discuss.elastic.co/t/cumulative-sum-with-range-limited-date-histogram/222564/1 "2020-03-07T15:04:27Z")

</div>

I have a timeseries that consists of events like "user has visited page x". If I want to know the total number of visits for a page, I can use a `cardinality` aggregation.

However, I want to (for example) see the total views of a specific page per day over the last week. If I use a `date_histogram`, I get much more buckets than I need. If I, however, restrict the date range using a `range` filter on the `query`, the cumulative sum would no longer be correct (since it no longer counts the views outside of the given range).

A `bucket_selector` might work, but I think due to the usage of a script that isn't a good idea.

Or maybe execute a multi search with two queries: one for the buckets I want, and one for the cumulative views until the beginning of the first bucket, then adding that value to all the buckets clientside.

So: what would be the "best" way to get only buckets for the last week (in that example), without loosing the cumulative sum outside of the desired range?

---

<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: [April 4, 2020, 3:04pm UTC](https://discuss.elastic.co/t/cumulative-sum-with-range-limited-date-histogram/222564/2 "2020-04-04T15:04:30Z")

</div>

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