Knowledge Management

how to add a duration column for each record, using its timestamp subtract the timestamp of the previous record,

wangkevin1029
Communicator

Hi, Splunkers,

I have a splunk search table as below, I want to add a duration column for each record, using its timestamp subtract the timestamp of the previous record,  

for example, here for record 2, duration should be 2021-12-14 12:55:25.258  - 2021-12-14 12:55:03.339

_time                                               columna

2021-12-14 12:55:03.339   abc
2021-12-14 12:55:25.258   efg
2021-12-14 12:55:25.336   hij

any help would be appreciated.

Kevin

0 Karma
1 Solution

PickleRick
SplunkTrust
SplunkTrust

There's a bit easier solution relying on streamstats alone

<base search>| streamstats current=t window=2 range(_time) as duration

 

View solution in original post

wangkevin1029
Communicator

thx for your quick response.

my table has its own sort,  so, not sure extra sort 0 _time will mess up original order?

I will give a quick try.

 

Kevin

 

 

0 Karma

wangkevin1029
Communicator

btw, since I use sort _time asc,  the time on top is earlier than time at the bottom

0 Karma

johnhuang
Motivator

Here's a possible solution:

 

| sort 0 -_time
| streamstats last(_time) AS end_epoch current=f
| eval end_time=strftime(end_epoch, "%Y-%m-%d %H:%M:%S")
| eval start_time=strftime(_time, "%Y-%m-%d %H:%M:%S")
| eval time_range=start_time." - ".end_time
| eval duration_sec=end_epoch-_time
| sort 0 _time

 

0 Karma

PickleRick
SplunkTrust
SplunkTrust

There's a bit easier solution relying on streamstats alone

<base search>| streamstats current=t window=2 range(_time) as duration

 

wangkevin1029
Communicator

Thank you, PickleRick,  it works perfectly, and more efficient, 

really take some time to digest these amazing, also complicated parameters, syntax.... 

 

To johnhua,  your code also works beautifully, also learned new commands from your code.

 

Kevin

0 Karma

johnhuang
Motivator

Yep but he asked for this format "2021-12-14 12:55:25.258  - 2021-12-14 12:55:03.339"

0 Karma

PickleRick
SplunkTrust
SplunkTrust

You're right. I missed that. Just read "duration" 😉

0 Karma

wangkevin1029
Communicator

works perfectly,  thx you very much. you are a Splunk master

let me digest your code.... amazing.

Kevin

Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Splunk Lantern’s Guide to The Most Popular .conf25 Sessions

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...

Unlock What’s Next: The Splunk Cloud Platform at .conf25

In just a few days, Boston will be buzzing as the Splunk team and thousands of community members come together ...

Index This | How many sevens are there between 1 and 100?

August 2025 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with this ...