# Time difference DQL

**URL:** <https://community.dynatrace.com/t/time-difference-dql/50750>\
**Category:** DQL\
**Tags:** dql\
**Created:** [March 17, 2025, 10:31am UTC](https://community.dynatrace.com/t/time-difference-dql/50750 "2025-03-17T10:31:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![RAKESHNREDDY](https://avatars.discourse-cdn.com/v4/letter/r/c2a13f/32.png) [@RAKESHNREDDY](https://community.dynatrace.com/u/RAKESHNREDDY)\
**Post date:** [March 17, 2025, 10:31am UTC](https://community.dynatrace.com/t/time-difference-dql/50750/1 "2025-03-17T10:31:32Z")

</div>

Hello Everyone,

Good day,

I have a requirement to calculate the time difference between the minimum and first maximum points for all the available data within the timeframe selected. In this example, the time taken for the value to reach 98 from 0.&nbsp; I’m able to get the latest output using the below DQL. Is there any trick to getting the output of the complete timeframe we selected?

DQL Used

timeseries count = max(appl.completed), timestamp=start()  
| fieldsAdd d=record(count=count[], timestamp=timestamp[])  
| fieldsAdd d = arrayRemoveNulls( iCollectArray( if(d[][count]\>97, d[] )))  
| expand d  
| fieldsAdd e=record(count1=count[], timestamp1=timestamp[])  
| fieldsAdd e = arrayRemoveNulls( iCollectArray( if(e[][count1]\<1 AND e[][timestamp1] \< d[timestamp], e[] )))  
| expand e  
| summarize d=takeFirst(d), e=takeLast(e)  
| fieldsAdd m = d[timestamp], n = e[timestamp1]  
| fieldsAdd A = m - n  
| fields A

 ![image.png](https://global.discourse-cdn.com/dynatrace/original/2X/6/6f6cadfa53c776acc9933ac775c70883cf463d56.png)

---

<div class="post-metadata">

**Author:** ![mark\_bley](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/mark_bley/32/20954_2.png) [@mark\_bley](https://community.dynatrace.com/u/mark_bley)\
**Post date:** [March 19, 2025, 8:25am UTC](https://community.dynatrace.com/t/time-difference-dql/50750/2 "2025-03-19T08:25:36Z")

</div>

Here is an example calculating duration, hope it helps:

```markup
data 
record(content="79895474545006\tPreProcessing\tstart\t1\t\t2023-04-27 04:23:12.000.153"),
record(content="79895474545006\tPreProcessing\tend\t1\t\t2023-04-27 04:30:38.000.153"),
record(content="79895474545005\tPreProcessing\tstart\t1\t\t2023-04-26 05:30:38.000.1538"),
record(content="79895474545005\tPreProcessing\tend\t1\t\t2023-04-27 04:30:38.000.153")
| parse content, """ALNUM:batch.id '\t' STRING:stage '\t' LDATA:stage_status '\t' INT?:valid '\t' FLOAT?:result PUNCT? '\t' TIMESTAMP('yyyy-MM-DD HH:mm:ss'):timestamp"""
| filter stage == "PreProcessing"
| summarize {start=takeFirst(if(stage_status=="start",timestamp)), end=takeFirst(if(stage_status=="end",timestamp))}, by:{batch.id, stage} 
| fieldsAdd duration = end - start

```

 ![mark_bley_1-1742372696814.png](https://global.discourse-cdn.com/dynatrace/original/3X/6/e/6e5c3d3ce6b7678b8745d9e03d5fc5e1c7d01dd4.png)

---

<div class="post-metadata">

**Author:** ![RAKESHNREDDY](https://avatars.discourse-cdn.com/v4/letter/r/c2a13f/32.png) [@RAKESHNREDDY](https://community.dynatrace.com/u/RAKESHNREDDY)\
**Post date:** [March 19, 2025, 9:54am UTC](https://community.dynatrace.com/t/time-difference-dql/50750/3 "2025-03-19T09:54:59Z")

</div>

Thanks Mark, tried including the ‘By: Day’ with a few modifications in the summarize. Works fine now.

---

<div class="post-metadata">

**Author:** ![marco\_irmer](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/marco_irmer/32/3038_2.png) [@marco\_irmer](https://community.dynatrace.com/u/marco_irmer)\
**Post date:** [March 19, 2025, 11:52pm UTC](https://community.dynatrace.com/t/time-difference-dql/50750/4 "2025-03-19T23:52:02Z")

</div>

Perhaps something like this would work for you:

```javascript
timeseries count = max(appl.completed)
| fieldsAdd minVal = arrayMin(count), maxVal = arrayMax(count)
| fieldsAdd minIndex = arrayLastIndexOf(count, minVal), 
            maxIndex = (arraySize(count) -1) - arrayLastIndexOf(arrayReverse(count), maxVal)
| fieldsAdd timeDistance =(maxIndex - minIndex) * interval

```
