# How to calculate difference between to cumulative sums?

**URL:** https://discuss.elastic.co/t/how-to-calculate-difference-between-to-cumulative-sums/107812
**Category:** Kibana
**Created:** [November 15, 2017, 7:22pm UTC](https://discuss.elastic.co/t/how-to-calculate-difference-between-to-cumulative-sums/107812 "2017-11-15T19:22:03Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![MrRedAmber](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mrredamber/32/24290_2.png) [@MrRedAmber](https://discuss.elastic.co/u/MrRedAmber)
#### Post date: [November 15, 2017, 7:22pm UTC](https://discuss.elastic.co/t/how-to-calculate-difference-between-to-cumulative-sums/107812/1 "2017-11-15T19:22:03Z")

</div>

So in my project I have mock bank account transaction data and I'm trying to calculate the difference between two cumulative sums. I don't know if I'm going about this the right way but what I'm doing is for each day in the span of a year, I'm calculate the cumulative sum of two number fields, debit and credit. I want to figure out how to subtract the cumulative sum of debit from the cumulative sum of credit.

CumulativeDifference = CumulativeSumCredit - CumulativeSumDebit.

The main goal I'm trying to achieve is to see the balance of the bank account according to the analyzed transaction data, but I just can't seem to get there.

Any help would be appreciated, thanks in advance 🙂

 ![38 PM](https://us1.discourse-cdn.com/elastic/original/3X/d/3/d34d372bb1e5ef3bad96039a338aac02764c2253.png)

---

<div class="post-metadata">

### Author: ![Nathan\_Reese](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nathan_reese/32/84829_2.png) [@Nathan\_Reese](https://discuss.elastic.co/u/Nathan_Reese)
#### Post date: [November 16, 2017, 4:33pm UTC](https://discuss.elastic.co/t/how-to-calculate-difference-between-to-cumulative-sums/107812/2 "2017-11-16T16:33:40Z")

</div>

Use the [timelion visualization](https://www.elastic.co/guide/en/kibana/current/timelion.html) to graph arithmetic on aggregated results.

---

<div class="post-metadata">

### Author: ![Bargs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bargs/32/5429_2.png) [@Bargs](https://discuss.elastic.co/u/Bargs)
#### Post date: [November 16, 2017, 4:45pm UTC](https://discuss.elastic.co/t/how-to-calculate-difference-between-to-cumulative-sums/107812/3 "2017-11-16T16:45:59Z")

</div>

It's not possible in the core Kibana visualizations, but you could use Timelion or Time Series Visual Builder to accomplish this.

The Timelion expression would look like this:

```auto
.es(*, metric=sum:credit).cusum().subtract(.es(*, metric=sum:debit).cusum())

```

In TSVB it would look like this:

 ![32 AM](https://us1.discourse-cdn.com/elastic/original/3X/1/6/166fe85484e29e55a560af8d581d140af210ff75.png)

---

<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 14, 2017, 4:46pm UTC](https://discuss.elastic.co/t/how-to-calculate-difference-between-to-cumulative-sums/107812/4 "2017-12-14T16:46:09Z")

</div>

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