<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Calculating Duration of Extracted Fields in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Calculating-Duration-of-Extracted-Fields/m-p/660590#M228079</link>
    <description>&lt;P&gt;Hey you may need to change them to a string. Can&amp;nbsp; you try this and let me know if it works ( I am new to Splunk lol)&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;MySearchCriteria index=MyIndex source=MySource
| stats list(ExtractedFieldStartTime) as MyStartTime, list(ExtractedFieldEndTime) as MyEndTime by AnotherField
| eval MyStartUnix=strptime(MyStartTime, "%Y-%m-%dT%H:%M:%S")
| eval MyEndUnix=strptime(MyEndTime, "%Y-%m-%dT%H:%M:%S")
| eval diff= tostring((MyEndUnix - MyStartUnix),"duration")
| table MyStartTime MyEndTime MyStartUnix MyEndUnix diff&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 12 Oct 2023 21:51:14 GMT</pubDate>
    <dc:creator>Law2</dc:creator>
    <dc:date>2023-10-12T21:51:14Z</dc:date>
    <item>
      <title>Calculating Duration of Extracted Fields</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Calculating-Duration-of-Extracted-Fields/m-p/660582#M228078</link>
      <description>&lt;P&gt;I'm having trouble getting a duration between two timestamps from some extracted fields.&lt;BR /&gt;&lt;BR /&gt;My search looks like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;MySearchCriteria index=MyIndex source=MySource
| stats list(ExtractedFieldStartTime) as MyStartTime, list(ExtractedFieldEndTime) as MyEndTime by AnotherField
| eval MyStartUnix=strptime(MyStartTime, "%Y-%m-%dT%H:%M:%S")
| eval MyEndUnix=strptime(MyEndTime, "%Y-%m-%dT%H:%M:%S")
| eval diff=MyEndUnix-MyStartUnix
| table MyStartTime MyEndTime MyStartUnix MyEndUnix diff&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;And my table is returned as:&lt;/P&gt;&lt;TABLE border="1" width="100%"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD width="20%"&gt;MyStartTime&lt;/TD&gt;&lt;TD width="20%"&gt;MyEndTime&lt;/TD&gt;&lt;TD width="20%"&gt;MyStartUnix&lt;/TD&gt;&lt;TD width="20%"&gt;MyEndUnix&lt;/TD&gt;&lt;TD width="20%"&gt;diff&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="20%"&gt;&lt;DIV class=""&gt;2023-10-10T14:48:39&lt;/DIV&gt;&lt;/TD&gt;&lt;TD width="20%"&gt;&lt;DIV class=""&gt;2023-10-10T14:15:15&lt;/DIV&gt;&lt;/TD&gt;&lt;TD width="20%"&gt;&lt;DIV class=""&gt;1696963719.000000&lt;/DIV&gt;&lt;/TD&gt;&lt;TD width="20%"&gt;&lt;DIV class=""&gt;1696961715.000000&lt;/DIV&gt;&lt;/TD&gt;&lt;TD width="20%"&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="20%"&gt;2023-10-10T14:57:50&lt;/TD&gt;&lt;TD width="20%"&gt;2023-10-10T13:56:53&lt;/TD&gt;&lt;TD width="20%"&gt;1696964270.000000&lt;/TD&gt;&lt;TD width="20%"&gt;1696960613.000000&lt;/TD&gt;&lt;TD width="20%"&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Thu, 12 Oct 2023 19:24:53 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Calculating-Duration-of-Extracted-Fields/m-p/660582#M228078</guid>
      <dc:creator>pgates</dc:creator>
      <dc:date>2023-10-12T19:24:53Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Duration of Extracted Fields</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Calculating-Duration-of-Extracted-Fields/m-p/660590#M228079</link>
      <description>&lt;P&gt;Hey you may need to change them to a string. Can&amp;nbsp; you try this and let me know if it works ( I am new to Splunk lol)&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;MySearchCriteria index=MyIndex source=MySource
| stats list(ExtractedFieldStartTime) as MyStartTime, list(ExtractedFieldEndTime) as MyEndTime by AnotherField
| eval MyStartUnix=strptime(MyStartTime, "%Y-%m-%dT%H:%M:%S")
| eval MyEndUnix=strptime(MyEndTime, "%Y-%m-%dT%H:%M:%S")
| eval diff= tostring((MyEndUnix - MyStartUnix),"duration")
| table MyStartTime MyEndTime MyStartUnix MyEndUnix diff&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Oct 2023 21:51:14 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Calculating-Duration-of-Extracted-Fields/m-p/660590#M228079</guid>
      <dc:creator>Law2</dc:creator>
      <dc:date>2023-10-12T21:51:14Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Duration of Extracted Fields</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Calculating-Duration-of-Extracted-Fields/m-p/660591#M228080</link>
      <description>&lt;P&gt;With Splunk usually if something looks like a number but doesn't behave like a number it means that it's not a number but a string representation of a number and you have to tonumber() it. But in this case since strptime should give you a number it would be a bit surprising.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Oct 2023 22:11:06 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Calculating-Duration-of-Extracted-Fields/m-p/660591#M228080</guid>
      <dc:creator>PickleRick</dc:creator>
      <dc:date>2023-10-12T22:11:06Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Duration of Extracted Fields</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Calculating-Duration-of-Extracted-Fields/m-p/660665#M228091</link>
      <description>&lt;P&gt;No, that doesn't work.&amp;nbsp; I believe the reason it doesn't work is because it is just attempting to change the value of the equation (end - start) to a string, and that value appears to be empty for some reason.&lt;/P&gt;&lt;P&gt;I appreciate the try though.&lt;/P&gt;</description>
      <pubDate>Fri, 13 Oct 2023 12:54:29 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Calculating-Duration-of-Extracted-Fields/m-p/660665#M228091</guid>
      <dc:creator>pgates</dc:creator>
      <dc:date>2023-10-13T12:54:29Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Duration of Extracted Fields</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Calculating-Duration-of-Extracted-Fields/m-p/660777#M228141</link>
      <description>&lt;P&gt;I have a strong suspicion that your mock output is misleading. &amp;nbsp;The correct mock output most likely look like this instead:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;DIV class=""&gt;MyStartTime&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV class=""&gt;MyEndTime&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV class=""&gt;MyStartUnix&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV class=""&gt;MyEndUnix&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV class=""&gt;diff&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;DIV class=""&gt;2023-10-10T14:48:39&lt;/DIV&gt;&lt;DIV class=""&gt;2023-10-10T14:57:50&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV class=""&gt;2023-10-10T14:15:15&lt;/DIV&gt;&lt;DIV class=""&gt;2023-10-10T13:56:53&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV class=""&gt;1696974519.000000&lt;/DIV&gt;&lt;DIV class=""&gt;1696975070.000000&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV class=""&gt;1696972515.000000&lt;/DIV&gt;&lt;DIV class=""&gt;1696971413.000000&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;In other words, instead of the two start and end pairs in two rows, they are in the same row for a given value of AnotherField which you didn't show.&lt;/P&gt;&lt;P&gt;This is because you use list function by AnotherField. &amp;nbsp;Very likely there are more than one start-end pairs per AnotherField. &amp;nbsp;These multivalued fields cannot be used in arithmetic operations directly. &amp;nbsp;Before I describe a method to handle multivalue fields, let me first get some clarifications.&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Is it really important to use list function? &amp;nbsp;Are there overlapping MyStartTime, overlapping MyEndTime, or overlapping intervals that magically end up in the correct sequence? &amp;nbsp;If not, using values is a lot cheaper and you won't be subject to memory limitations. (Because we are looking at ISO timestamps, values with order them correctly.)&lt;/LI&gt;&lt;LI&gt;Is it really important to calculate diff after stats? &amp;nbsp;If you are listing/tallying values of every start-end pair, it is actually cheaper to calculate diff before stats. (If MyStartTime and MyEndTime don't appear in the same event, of course, you don't have a choice.)&lt;/LI&gt;&lt;LI&gt;I cannot see real importance of listing MyStartUnix and MyEndUnix in final results, so the following will simply ignore them.&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;With these caveats, you can use &lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/MultivalueEvalFunctions#mvmap.28.26lt.3Bmv.26gt.3B.2C.26lt.3Bexpression.26gt.3B.29" target="_blank" rel="noopener"&gt;mvmap&lt;/A&gt; to handle multivalued field after stats. &amp;nbsp;In the following, I assume that each start is paired with an end.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;MySearchCriteria index=MyIndex source=MySource
| stats list(ExtractedFieldStartTime) as MyStartTime, list(ExtractedFieldEndTime) as MyEndTime by AnotherField
| eval idx = mvrange(0, mvcount(MyStartTime))
| eval diff=mvmap(idx, strptime(mvindex(MyEndTime, idx), "%Y-%m-%dT%H:%M:%S")-strptime(mvindex(MyStartTime, idx), "%Y-%m-%dT%H:%M:%S"))
| fields - idx&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This will give you&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;AnotherField&lt;/TD&gt;&lt;TD&gt;&lt;DIV class=""&gt;MyStartTime&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV class=""&gt;MyEndTime&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV class=""&gt;diff&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;another&lt;/TD&gt;&lt;TD&gt;&lt;DIV class=""&gt;2023-10-10T14:48:39&lt;/DIV&gt;&lt;DIV class=""&gt;2023-10-10T14:57:50&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV class=""&gt;2023-10-10T14:15:15&lt;/DIV&gt;&lt;DIV class=""&gt;2023-10-10T13:56:53&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV class=""&gt;-2004.000000&lt;/DIV&gt;&lt;DIV class=""&gt;-3657.000000&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;(Your samples have ends before starts, hence negative diffs.)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 14 Oct 2023 10:06:54 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Calculating-Duration-of-Extracted-Fields/m-p/660777#M228141</guid>
      <dc:creator>yuanliu</dc:creator>
      <dc:date>2023-10-14T10:06:54Z</dc:date>
    </item>
  </channel>
</rss>

