<?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: How to find the time difference between events to calculate the average duration? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-find-the-time-difference-between-events-to-calculate-the/m-p/273020#M82242</link>
    <description>&lt;P&gt;Did you try &lt;CODE&gt;| transaction PREMISE startswith="ACPwrLoss" endswith="ACPwrLossRes" | timechart avg(duration) by PREMISE&lt;/CODE&gt;&lt;/P&gt;</description>
    <pubDate>Wed, 25 May 2016 23:54:54 GMT</pubDate>
    <dc:creator>sk314</dc:creator>
    <dc:date>2016-05-25T23:54:54Z</dc:date>
    <item>
      <title>How to find the time difference between events to calculate the average duration?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-find-the-time-difference-between-events-to-calculate-the/m-p/273018#M82240</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;I have a data set that looks like this:&lt;/P&gt;

&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="alt text"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/1414i89F5910114ED83BF/image-size/large?v=v2&amp;amp;px=999" role="button" title="alt text" alt="alt text" /&gt;&lt;/span&gt;&lt;/P&gt;

&lt;P&gt;I need to calculate the avg duration of the power loss (event where &lt;CODE&gt;EVENT_SUB_TYPE=ACpwrLoss&lt;/CODE&gt; is the beginning and event where &lt;CODE&gt;EVENT_SUB_TYPE=ACpwrLossRes&lt;/CODE&gt; is the end).  The time that the transaction begins/ends is in the &lt;STRONG&gt;CPE_CONVERTED&lt;/STRONG&gt; field and this needs to be by &lt;STRONG&gt;PREMISE&lt;/STRONG&gt;.&lt;/P&gt;

&lt;P&gt;I've tried several things, but none have worked out yet.  This one is the most promising but still no luck&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| transaction PREMISE EVENT_SUB_TYPE startswith="ACPwrLoss" endswith="ACPwrLossRes" | timechart avg(duration) by PREMISE
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Any thoughts?&lt;/P&gt;</description>
      <pubDate>Wed, 25 May 2016 22:18:20 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-find-the-time-difference-between-events-to-calculate-the/m-p/273018#M82240</guid>
      <dc:creator>dbcase</dc:creator>
      <dc:date>2016-05-25T22:18:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the time difference between events to calculate the average duration?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-find-the-time-difference-between-events-to-calculate-the/m-p/273019#M82241</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;Try removing the field "EVENT_SUB_TYPE" from the field list.&lt;/P&gt;

&lt;P&gt;The transaction command uses the field list to group events together and in your example, the contents of this field are actually different so you are getting 2 transactions with only a single event in them.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 09:47:31 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-find-the-time-difference-between-events-to-calculate-the/m-p/273019#M82241</guid>
      <dc:creator>michael_bates_1</dc:creator>
      <dc:date>2020-09-29T09:47:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the time difference between events to calculate the average duration?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-find-the-time-difference-between-events-to-calculate-the/m-p/273020#M82242</link>
      <description>&lt;P&gt;Did you try &lt;CODE&gt;| transaction PREMISE startswith="ACPwrLoss" endswith="ACPwrLossRes" | timechart avg(duration) by PREMISE&lt;/CODE&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 25 May 2016 23:54:54 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-find-the-time-difference-between-events-to-calculate-the/m-p/273020#M82242</guid>
      <dc:creator>sk314</dc:creator>
      <dc:date>2016-05-25T23:54:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the time difference between events to calculate the average duration?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-find-the-time-difference-between-events-to-calculate-the/m-p/273021#M82243</link>
      <description>&lt;P&gt;This works for the very last outage of each premise, but doesnt show all outages if there were multiples:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; index=top10 | eval powerOff=if(EVENT_SUB_TYPE="ACPwrLoss",_time,null()) | eval powerRestored=if(EVENT_SUB_TYPE="ACPwrLossRes",_time,null()) | stats last(powerRestored) AS lastPowerRestored last(powerOff) AS lastPowerOff by PREMISE | eval outageDuration=lastPowerRestored-lastPowerOff | table PREMISE lastPowerOff lastPowerRestored outageDuration
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This works for all outages of each premise, but can get wonky if you have multiple ACPwrLoss'es for one premise before having a ACPwrLossRes... can also get weird if you have the loss but no restoration.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; index=top10 | sort PREMISE | eval powerLost=if(EVENT_SUB_TYPE="ACPwrLoss",_time,null()) | eval powerRestored=if(EVENT_SUB_TYPE="ACPwrLossRes",_time,null()) | filldown powerRestored | table PREMISE powerLost powerRestored | where isnotnull(powerLost) | eval duration=powerRestored-powerLost | stats avg(duration) as duration by PREMISE
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;duration is in seconds in above.&lt;/P&gt;

&lt;P&gt;Here's the same as above but evals for minutes, max, mins, avg, and counts:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; index=top10 | sort PREMISE | eval powerLost=if(EVENT_SUB_TYPE="ACPwrLoss",_time,null()) | eval powerRestored=if(EVENT_SUB_TYPE="ACPwrLossRes",_time,null()) | filldown powerRestored| table PREMISE powerLost powerRestored | where isnotnull(powerLost) | eval duration=powerRestored-powerLost | stats count(duration) as total_outage_count max(eval(duration/60)) as maximum_duration_minutes min(eval(duration/60)) as minumum_duration_minute avg(eval(duration/60)) as average_duration_minutes by PREMISE
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Sorry for so many answers, but I hope one of them fits the bill.&lt;/P&gt;

&lt;P&gt;Cheers,&lt;BR /&gt;
JKat54&lt;/P&gt;</description>
      <pubDate>Thu, 26 May 2016 00:02:58 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-find-the-time-difference-between-events-to-calculate-the/m-p/273021#M82243</guid>
      <dc:creator>jkat54</dc:creator>
      <dc:date>2016-05-26T00:02:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the time difference between events to calculate the average duration?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-find-the-time-difference-between-events-to-calculate-the/m-p/273022#M82244</link>
      <description>&lt;P&gt;Why not use stats instead of timechart?  I'm not sure what default span is on timechart but it might be messing up your results.&lt;/P&gt;</description>
      <pubDate>Thu, 26 May 2016 12:53:13 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-find-the-time-difference-between-events-to-calculate-the/m-p/273022#M82244</guid>
      <dc:creator>sjohnson_splunk</dc:creator>
      <dc:date>2016-05-26T12:53:13Z</dc:date>
    </item>
  </channel>
</rss>

