# Canvas and cumulative sum

**URL:** https://discuss.elastic.co/t/canvas-and-cumulative-sum/224685
**Category:** Kibana
**Tags:** canvas
**Created:** [March 23, 2020, 2:18pm UTC](https://discuss.elastic.co/t/canvas-and-cumulative-sum/224685 "2020-03-23T14:18:19Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![PaulSPG](https://avatars.discourse-cdn.com/v4/letter/p/3bc359/32.png) [@PaulSPG](https://discuss.elastic.co/u/PaulSPG)
#### Post date: [March 23, 2020, 2:18pm UTC](https://discuss.elastic.co/t/canvas-and-cumulative-sum/224685/1 "2020-03-23T14:18:19Z")

</div>

Would it be possible to calculate the cumulative sum of a column using f.i. mapColumn. For example the price column of demodata.

Something like:  
filters  
| demodata  
| mapColumn "CuSum Price" fn={math 'sum(price + CuSum[row-1]) '}  
| table  
| render

---

<div class="post-metadata">

### Author: ![tims](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tims/32/46750_2.png) [@tims](https://discuss.elastic.co/u/tims)
#### Post date: [March 23, 2020, 3:37pm UTC](https://discuss.elastic.co/t/canvas-and-cumulative-sum/224685/2 "2020-03-23T15:37:43Z")

</div>

Hey @PaulSPG right now there is not a cumulative sum function in the expression language. You would have to do something like this using the Timelion data source in Canvas.

Here is a link to the Timelion cusum function: [https://github.com/elastic/timelion/blob/master/FUNCTIONS.md#cusum](https://github.com/elastic/timelion/blob/master/FUNCTIONS.md#cusum)

---

<div class="post-metadata">

### Author: ![PaulSPG](https://avatars.discourse-cdn.com/v4/letter/p/3bc359/32.png) [@PaulSPG](https://discuss.elastic.co/u/PaulSPG)
#### Post date: [March 23, 2020, 3:51pm UTC](https://discuss.elastic.co/t/canvas-and-cumulative-sum/224685/3 "2020-03-23T15:51:26Z")

</div>

Thanks for the quick response. I already had a look at that one. But I haven't been able to get it working with more than 1 field. The documents retrieved from es have more than one field per row/doc. For example a table with columns: timestamp price1 price2. For the time being I will stick to the sum per time interval which already gives some insights.

---

<div class="post-metadata">

### Author: ![PaulSPG](https://avatars.discourse-cdn.com/v4/letter/p/3bc359/32.png) [@PaulSPG](https://discuss.elastic.co/u/PaulSPG)
#### Post date: [March 24, 2020, 8:29am UTC](https://discuss.elastic.co/t/canvas-and-cumulative-sum/224685/4 "2020-03-24T08:29:56Z")

</div>

Timelion is indeed the way to go. This script does what I need:

```auto
   filters
      | timelion 
        query="
        .es(index=*some_index*, metric=sum:Price1, timefield=@timestamp ).cusum().label('P1'),
        .es(index=*some_index*, metric=sum:Price2, timefield=@timestamp ).cusum().label('P2')" interval="1M"
      | pointseries x="@timestamp" y="value" color="label"
      | plot defaultStyle={seriesStyle lines=2 } legend="ne"
      | render

```

The timelion query results in a table with 3 columns where value represents the cusum of Price1 or Price2 each month and label represents the 'origin' field (P1 for Price1 or P2 for Price2):

```auto
        @timestamp, value, label
        2019-10-01, 120, P1
        2019-11-01, 240, P1
        2019-10-01, 124, P2
        2019-11-01, 338, P2

```

The point series creates series P1 and P2 using the label column to 'split'.

---

<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 21, 2020, 8:30am UTC](https://discuss.elastic.co/t/canvas-and-cumulative-sum/224685/5 "2020-04-21T08:30:04Z")

</div>

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