Splunk Search

Calculate average time taken for transactions.

newbie77
Engager
Field1=Start

Field2=Finish

Field1 and Field2 have multiple events with values Start and Finish for a given uid respectively. I want to pick earliest event for Fiield1 and latest event for Field2 and find the duration.

Field3=uid which is the common field.

….| transaction uid startswith=“Start” ends with=“Finish” | stats avg(duration)

It’s not giving the expected result.

Labels (1)
0 Karma
1 Solution

tscroggins
Influencer

Hi @newbie77,

If an instance of Field1=Start is always the earliest event by uid and Field2=Finish is always the latest event by uid, you can use the stats range() function:

| stats range(_time) as duration by uid

Otherwise, use the stats min() and max() or earliest() and latest() functions with an eval expression:

| stats min(eval(case(Field1=="Start", _time))) as start_time max(eval(case(Field2=="Finish"))) as finish_time by uid
| eval duration=finish_time-start_time

View solution in original post

0 Karma

yuanliu
SplunkTrust
SplunkTrust

It’s not giving the expected result.

This is a lot better than a phrase we hear too often: "It doesn't work."

This said, what is the expected result?  To ask an answerable data analytics question, follow these golden rules; nay, call them the four commandments:

  • Illustrate data input (in raw text, anonymize as needed), whether they are raw events or output from a search that volunteers here do not have to look at.
  • Illustrate the desired output from illustrated data.
  • Explain the logic between illustrated data and desired output without SPL.
  • If you also illustrate attempted SPL, illustrate actual output and compare with desired output, explain why they look different to you if that is not painfully obvious.

tscroggins
Influencer

Hi @newbie77,

If an instance of Field1=Start is always the earliest event by uid and Field2=Finish is always the latest event by uid, you can use the stats range() function:

| stats range(_time) as duration by uid

Otherwise, use the stats min() and max() or earliest() and latest() functions with an eval expression:

| stats min(eval(case(Field1=="Start", _time))) as start_time max(eval(case(Field2=="Finish"))) as finish_time by uid
| eval duration=finish_time-start_time
0 Karma
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!

Can’t Make It to Boston? Stream .conf25 and Learn with Haya Husain

Boston may be buzzing this September with Splunk University and .conf25, but you don’t have to pack a bag to ...

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 ...