# Cumulative Sum - DQL

**URL:** <https://community.dynatrace.com/t/cumulative-sum-dql/42601>\
**Category:** DQL\
**Tags:** dql, dashboards\
**Created:** [November 14, 2023, 9:18am UTC](https://community.dynatrace.com/t/cumulative-sum-dql/42601 "2023-11-14T09:18:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidrodriguez](https://avatars.discourse-cdn.com/v4/letter/d/34f0e0/32.png) [@davidrodriguez](https://community.dynatrace.com/u/davidrodriguez)\
**Post date:** [November 14, 2023, 9:18am UTC](https://community.dynatrace.com/t/cumulative-sum-dql/42601/1 "2023-11-14T09:18:33Z")

</div>

Is it possible to plot a cumulative sum in DQL?

Let’s take for example a graph that shows the count of a metric for the given time. How could we add a second line in the graph, that shows the cumulative sum of the previous metric?

For the first metric (M1) we would see, for each data point a value (M1V1). On the other hand, for the cumulative metric (M2), we would see a sum of all the values previous to that data point.

_ **M2V5 =** _ _M1V1 + … + M1V4_

Would this be possible? Thank you in advance

---

<div class="post-metadata">

**Author:** ![kauanschumacher](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/kauanschumacher/32/17742_2.png) [@kauanschumacher](https://community.dynatrace.com/u/kauanschumacher)\
**Post date:** [January 2, 2024, 5:44pm UTC](https://community.dynatrace.com/t/cumulative-sum-dql/42601/2 "2024-01-02T17:44:41Z")

</div>

Any news?  
My company is migrating from DataDog to Dynatrace, and we have so many dashboards to migrate. Some dashboards uses the “cumulative sum” to create linear growth charts to identify system flow anomallies

The cumulative sum it is so useful to resolve this problem…

Someone can help us?

---

<div class="post-metadata">

**Author:** ![kauanschumacher](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/kauanschumacher/32/17742_2.png) [@kauanschumacher](https://community.dynatrace.com/u/kauanschumacher)\
**Post date:** [January 2, 2024, 6:58pm UTC](https://community.dynatrace.com/t/cumulative-sum-dql/42601/3 "2024-01-02T18:58:12Z")

</div>

[@davidrodriguez](https://community.dynatrace.com/user/58991)&nbsp;maybe i found a workaround to do that!

In my use case, I need count logs by a message pattern and do a cummulative summarize to have a linear growth chart…

To do that I change my chart to “code chart” and implement the query using “@dynatrace-sdk” + response manipulation

See this example, and check if helps you

```javascript
/*
* This function will run in the DYNATRACE JavaScript runtime.
* For information visit https://dt-url.net/functions-help
*/
import { queryExecutionClient } from '@dynatrace-sdk/client-query';

export default async function () {
  const env = $Environment;
  const from = $dt_timeframe_from;
  const to = $dt_timeframe_to;

  const timeout = 60;
  const query = `
    fetch logs, from:"${from}", to:"${to}"
    | filter dt.host_group.id == "${env}"
    | parse content, "JSON:contentjson"
    | filter contains(contentjson[message], "TradeCreationRequested") or 
             contains(contentjson[messageTemplate], "Trade created") or 
    | summarize Requested = countIf(contains(contentjson[message], "TradeCreationRequested")), 
                Created = countIf(contains(contentjson[messageTemplate], "Trade created")), 
                by: delta = bin(timestamp,10m)
    | sort delta desc
  `;

  const response = await queryExecutionClient.queryExecute({ body: { query, requestTimeoutMilliseconds: timeout * 1000, fetchTimeoutSeconds: timeout } });
  response.logs = null;
  for (let i = 0; i < response.result.records.length; i++) 
  {
    response.result.records[i].Requested = response.result.records.filter((_, index) => index >= i).reduce((a, b) => a + (b.Requested * 1), 0);
    response.result.records[i].Created = response.result.records.filter((_, index) => index >= i).reduce((a, b) => a + (b.Created * 1), 0);
  }

  return response.result
}

```

---

<div class="post-metadata">

**Author:** ![sinisa\_zubic](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/sinisa_zubic/32/32498_2.png) [@sinisa\_zubic](https://community.dynatrace.com/u/sinisa_zubic)\
**Post date:** [January 12, 2024, 3:18pm UTC](https://community.dynatrace.com/t/cumulative-sum-dql/42601/4 "2024-01-12T15:18:00Z")

</div>

hi&nbsp;[@davidrodriguez](https://community.dynatrace.com/user/58991)

With the just released function [arrayMovingSum](https://docs.dynatrace.com/docs/platform/grail/dynatrace-query-language/functions/array-functions#arrayMovingSum) it is no possible to create cumulative sum timeseries. some example from [discover dynatrace](https://wkf10640.apps.dynatrace.com/ui/intent/dynatrace.notebooks/view-query#%7B%22dt.query%22%3A%22fetch%20bizevents%5Cn%7C%20filter%20event.type%20%3D%3D%20%5C%22com.easytrade.deposit.start%5C%22%5Cn%7C%20makeTimeseries%20deposits%20%3D%20sum(amount)%2C%20bins%3A60%5Cn%7C%20fieldsAdd%20deposits_cumulative%3DarrayMovingSum(deposits%2C60)%22%2C%22dt.timeframe%22%3A%7B%22from%22%3A%22now-2h%22%2C%22to%22%3A%22now%22%7D%2C%22hideInput%22%3Afalse%2C%22sourceApplication%22%3A%22dynatrace.notebooks%22%2C%22title%22%3A%22cumulative%20sum%22%2C%22visualization%22%3A%22lineChart%22%2C%22visualizationSettings%22%3A%7B%22thresholds%22%3A%5B%5D%2C%22chartSettings%22%3A%7B%22gapPolicy%22%3A%22connect%22%2C%22circleChartSettings%22%3A%7B%22groupingThresholdType%22%3A%22relative%22%2C%22groupingThresholdValue%22%3A0%2C%22valueType%22%3A%22relative%22%7D%2C%22categoryOverrides%22%3A%7B%7D%7D%2C%22singleValue%22%3A%7B%22showLabel%22%3Atrue%2C%22label%22%3A%22%22%2C%22prefixIcon%22%3A%22%22%2C%22autoscale%22%3Atrue%2C%22alignment%22%3A%22center%22%2C%22isBackgroundThresholdActive%22%3Afalse%7D%2C%22table%22%3A%7B%22rowDensity%22%3A%22condensed%22%2C%22enableSparklines%22%3Afalse%2C%22hiddenColumns%22%3A%5B%5D%2C%22lineWrapIds%22%3A%5B%5D%2C%22firstVisibleRowIndex%22%3A0%2C%22columnWidths%22%3A%7B%7D%7D%7D%7D)

```markup
fetch bizevents
| filter event.type == "com.easytrade.deposit.start"
| makeTimeseries deposits = sum(amount), bins:60
| fieldsAdd deposits_cumulative=arrayMovingSum(deposits,60)

```

 ![sinisa_zubic_0-1705072654513.png](https://global.discourse-cdn.com/dynatrace/original/2X/c/c253a2470f40dc0f5c6598581c7e4a0804c5a39c.png)

Best,  
Sini

---

<div class="post-metadata">

**Author:** ![zietho](https://avatars.discourse-cdn.com/v4/letter/z/94ad74/32.png) [@zietho](https://community.dynatrace.com/u/zietho)\
**Post date:** [April 15, 2024, 8:12am UTC](https://community.dynatrace.com/t/cumulative-sum-dql/42601/5 "2024-04-15T08:12:49Z")

</div>

check out an&nbsp;[example](https://wkf10640.apps.dynatrace.com/ui/document/v0/#share=b2414393-2ab2-429b-9cc9-0900595c5f75) on our playground environment.

 ![zietho_0-1713168715483.png](https://global.discourse-cdn.com/dynatrace/original/3X/a/a/aab618714f2f44382513ca5e6245b9b05cb316ba.png)
