# Calculate time difference between two records

**URL:** <https://community.dynatrace.com/t/calculate-time-difference-between-two-records/47921>\
**Category:** DQL\
**Tags:** dql, timestamps\
**Created:** [September 23, 2024, 10:28pm UTC](https://community.dynatrace.com/t/calculate-time-difference-between-two-records/47921 "2024-09-23T22:28:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sona282](https://avatars.discourse-cdn.com/v4/letter/s/ee59a6/32.png) [@Sona282](https://community.dynatrace.com/u/Sona282)\
**Post date:** [September 23, 2024, 10:28pm UTC](https://community.dynatrace.com/t/calculate-time-difference-between-two-records/47921/1 "2024-09-23T22:28:30Z")

</div>

Hello,

I want to calculate the time difference the current record and previous record, until the end of the record.

example,

 ![Sona282_0-1727130396673.png](https://global.discourse-cdn.com/dynatrace/original/2X/d/d748418371f1cc44014960c73ca96810024699cf.png)

Thank you.

---

<div class="post-metadata">

**Author:** ![JeanBlanc](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/jeanblanc/32/25897_2.png) [@JeanBlanc](https://community.dynatrace.com/u/JeanBlanc)\
**Post date:** [September 24, 2024, 3:34pm UTC](https://community.dynatrace.com/t/calculate-time-difference-between-two-records/47921/2 "2024-09-24T15:34:18Z")

</div>

Hi.

The easiest way to do this is to convert your DateTime values and then subtract the start time from the end time.

```auto
| fieldsAdd myendtime = toTimestamp(myendtime), mystarttime = toTimestamp(mystarttime)
| fieldsAdd myduration = myendtime - mystarttime

```

You might need to perform the same subtraction multiple times until the end of the record.

Let me know if this works for you 🙂

---

<div class="post-metadata">

**Author:** ![krzysztof\_hoja](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/krzysztof_hoja/32/34625_2.png) [@krzysztof\_hoja](https://community.dynatrace.com/u/krzysztof_hoja)\
**Post date:** [September 25, 2024, 4:35pm UTC](https://community.dynatrace.com/t/calculate-time-difference-between-two-records/47921/3 "2024-09-25T16:35:40Z")

</div>

Window function are not available yet, but it is possible under certain limitations using&nbsp;_ **collectArray** _&nbsp;, array sorting, and&nbsp;_ **arrayDelta** _&nbsp;functions and your data set has well defined order:

```markup
data 
  record(timestamp=now(), tx="a"),
  record(timestamp=now()+500ms, tx="b"),
  record(timestamp=now()+2s, tx="c"),
  record(timestamp=now()+3s, tx="d"),
  record(timestamp=now()+4s, tx="e"),
  record(timestamp=now()+5s, tx="f"),
  record(timestamp=now()+13s, tx="g"),
  record(timestamp=now()+14s, tx="h"),
  record(timestamp=now()+15s, tx="i"),
  record(timestamp=now()+16s, tx="j")
| sort timestamp asc
| fieldsAdd d = record(t=timestamp, tx)
| summarize d = collectArray(d)
| fieldsAdd d =arraySort(d, direction:"ascending")
| fieldsAdd dt = arrayDelta(iCollectArray(unixNanosFromTimestamp(d[][t])))
| fields d = record( timestamp=d[][t], dt = duration(dt[], unit:"ns"), tx=d[][tx])
| expand d
| fields timestamp=d[timestamp], dt=d[dt], tx=d[tx]
| sort timestamp asc

```

Result looks like this:

 ![krzysztof_hoja_0-1727299526155.png](https://global.discourse-cdn.com/dynatrace/original/2X/e/ee68f235bf94e02dd66bc6eb350dcecedecc1e57.png)

max number of records processed using this method is 131072 according to documentation:&nbsp;[Aggregation functions — Dynatrace Docs](https://docs.dynatrace.com/docs/platform/grail/dynatrace-query-language/functions/aggregation-functions#collectArray)
