# Time difference between two timestamps separated by a unique ID using scripted fields

**URL:** <https://discuss.elastic.co/t/time-difference-between-two-timestamps-separated-by-a-unique-id-using-scripted-fields/231539>\
**Category:** Kibana\
**Created:** [May 7, 2020, 12:42pm UTC](https://discuss.elastic.co/t/time-difference-between-two-timestamps-separated-by-a-unique-id-using-scripted-fields/231539 "2020-05-07T12:42:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![muntazirabbas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/muntazirabbas/32/65416_2.png) [@muntazirabbas](https://discuss.elastic.co/u/muntazirabbas)\
**Post date:** [May 7, 2020, 12:42pm UTC](https://discuss.elastic.co/t/time-difference-between-two-timestamps-separated-by-a-unique-id-using-scripted-fields/231539/1 "2020-05-07T12:42:03Z")

</div>

Hi guyz,

I am new to scripted fields. I have logs with entries 'RequestExecuted' and RequestReceived' with their timestamps and connected by unique ID called 'TransactionId'. I am trying to get the time difference between RequestReceived' and 'RequestExecuted' . Below is my logic  
´int timetaken = 0;  
int start = 0;  
String TransactionId;  
int end = 0;

```
for (def dissect_item : params._source['dissect']) {
    if (dissect_item['MessageIdentifier'] == 'RequestReceived') {          
        start = dissect_item['timestamp'].value.millis;          
        TransactionId = dissect_item['TransactionId'];
    }

    if( dissect_item['MessageIdentifier'] =='RequestExecuted' && dissect_item['TransactionId'] == TransactionId) {          
        end = dissect_item['dissect.timestamp'].value.millis;          
        timetaken = end - start; 
        return timetaken;
    }`

```

and the data in the kibana looks like this:

![image](https://us1.discourse-cdn.com/elastic/original/3X/e/2/e2d065171c0b9e472509afee9ce9e075e821a797.png)  
Please help me as I have tried many ways but always getting an error.

---

<div class="post-metadata">

**Author:** ![Marius\_Dragomir](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marius_dragomir/32/42087_2.png) [@Marius\_Dragomir](https://discuss.elastic.co/u/Marius_Dragomir)\
**Post date:** [May 11, 2020, 4:39pm UTC](https://discuss.elastic.co/t/time-difference-between-two-timestamps-separated-by-a-unique-id-using-scripted-fields/231539/2 "2020-05-11T16:39:58Z")

</div>

Scripted fields in Kibana work only in the context of a single document so it cannot look in another document in order to do the diff. What I would suggest would be to create a scripted field that checks if any of the fields exists in the doc and then returnds that field's value.  
Then you can do a serial diff aggregation, split by Transaction ID

---

<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:** [June 8, 2020, 4:40pm UTC](https://discuss.elastic.co/t/time-difference-between-two-timestamps-separated-by-a-unique-id-using-scripted-fields/231539/3 "2020-06-08T16:40:00Z")

</div>

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