<?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: Timechart based on date and time string NOT the timestamp in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Timechart-based-on-date-and-time-string-NOT-the-timestamp/m-p/118579#M31668</link>
    <description>&lt;P&gt;Sure, the Close_Date format is:&lt;/P&gt;

&lt;P&gt;2015-05-20 09:15:30&lt;/P&gt;</description>
    <pubDate>Wed, 20 May 2015 22:22:16 GMT</pubDate>
    <dc:creator>JWBailey</dc:creator>
    <dc:date>2015-05-20T22:22:16Z</dc:date>
    <item>
      <title>Timechart based on date and time string NOT the timestamp</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Timechart-based-on-date-and-time-string-NOT-the-timestamp/m-p/118577#M31666</link>
      <description>&lt;P&gt;I have data and time information in a log stored as a string.  It is an additional field not the timestamp or _time.  &lt;/P&gt;

&lt;P&gt;How can I get a chart of these events based on this time information?  For example, I have extracted this sting into a field named Close_Date.  I need the count of events broken down into various windows:  Close_Date &amp;lt; 30 days ago, Close_Date between 30 and 60 days ago, Close_Date between 60 and 90 days ago, and Close_Date&amp;gt;90 days ago.  &lt;/P&gt;

&lt;P&gt;Close_Date has no correlation with the event timestamp.  &lt;/P&gt;

&lt;P&gt;What is the most efficient way to do this?  &lt;/P&gt;

&lt;P&gt;Thank you,&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 20:01:17 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Timechart-based-on-date-and-time-string-NOT-the-timestamp/m-p/118577#M31666</guid>
      <dc:creator>JWBailey</dc:creator>
      <dc:date>2020-09-28T20:01:17Z</dc:date>
    </item>
    <item>
      <title>Re: Timechart based on date and time string NOT the timestamp</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Timechart-based-on-date-and-time-string-NOT-the-timestamp/m-p/118578#M31667</link>
      <description>&lt;P&gt;Can i have the format of Close_Date? A sample data.&lt;BR /&gt;
Thanks&lt;/P&gt;</description>
      <pubDate>Wed, 20 May 2015 22:10:43 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Timechart-based-on-date-and-time-string-NOT-the-timestamp/m-p/118578#M31667</guid>
      <dc:creator>stephanefotso</dc:creator>
      <dc:date>2015-05-20T22:10:43Z</dc:date>
    </item>
    <item>
      <title>Re: Timechart based on date and time string NOT the timestamp</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Timechart-based-on-date-and-time-string-NOT-the-timestamp/m-p/118579#M31668</link>
      <description>&lt;P&gt;Sure, the Close_Date format is:&lt;/P&gt;

&lt;P&gt;2015-05-20 09:15:30&lt;/P&gt;</description>
      <pubDate>Wed, 20 May 2015 22:22:16 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Timechart-based-on-date-and-time-string-NOT-the-timestamp/m-p/118579#M31668</guid>
      <dc:creator>JWBailey</dc:creator>
      <dc:date>2015-05-20T22:22:16Z</dc:date>
    </item>
    <item>
      <title>Re: Timechart based on date and time string NOT the timestamp</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Timechart-based-on-date-and-time-string-NOT-the-timestamp/m-p/118580#M31669</link>
      <description>&lt;P&gt;Hello JWBiley. Your problem was not easy, but i think, i found the solution.   Let's go&lt;/P&gt;

&lt;P&gt;Here is the situation:&lt;BR /&gt;
In your events, you have a field called  &lt;STRONG&gt;Close_Date&lt;/STRONG&gt;,  containing dates  in the format  &lt;STRONG&gt;2015-05-20 09:15:30&lt;/STRONG&gt; &lt;STRONG&gt;(year-month-day hour:min:sec)&lt;/STRONG&gt;, and you want for example a search that will return events, &lt;STRONG&gt;there is 30 days&lt;/STRONG&gt;. To accomplish your work, you need an &lt;STRONG&gt;initial&lt;/STRONG&gt; date. For example, if  Close_Date is populated like this in your events:&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;Close_Date&lt;/STRONG&gt;&lt;BR /&gt;
&lt;STRONG&gt;2015-05-20 8:15:45&lt;/STRONG&gt;&lt;BR /&gt;
2015-04-21 9:15:00&lt;BR /&gt;
2015-03-22 9:15:55&lt;BR /&gt;
2015-04-21 9:15:30&lt;BR /&gt;
2015-04-22 14:15:45&lt;BR /&gt;
2015-04-21 15:30:30&lt;BR /&gt;
&lt;STRONG&gt;2015-04-21 8:15:45&lt;BR /&gt;
2015-04-21 8:15:45&lt;BR /&gt;
2015-04-21 8:15:45&lt;/STRONG&gt;&lt;/P&gt;

&lt;P&gt;By taking the last event's date (here, the first row) as your initial date, your search will have to return the first 3 events, because from &lt;STRONG&gt;2015-05-20 8:15:45&lt;/STRONG&gt; to &lt;STRONG&gt;2015-04-21 8:15:45&lt;/STRONG&gt;, there is exactly 30 days.&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;Algorithm&lt;/STRONG&gt;&lt;/P&gt;

&lt;OL&gt;
&lt;LI&gt;if your initial date is for example 2015-05-31 10:30:20, target events there is 30 days, are events with 2015-05-02 10:30:20 as a Close_Date&lt;/LI&gt;
&lt;LI&gt;if your initial date is for example 2015-05-30 10:30:20, target events there is 30 days, are events with 2015-05-01 10:30:20 as a Close_Date&lt;/LI&gt;
&lt;LI&gt;if your initial date is for example  Y-M-D 10:30:20 and the month is not january, with  D&amp;lt;30, target events there is 30 days, are events with &lt;STRONG&gt;Y-(M-1)-(D+1) 10:30:20&lt;/STRONG&gt; as a Close_Date&lt;/LI&gt;
&lt;/OL&gt;

&lt;P&gt;4 . if your initial date is for example &lt;STRONG&gt;Y-01-D 10:30:20&lt;/STRONG&gt; and the month is january,  with  D&amp;lt;30, target events there is 30 days, are events with &lt;STRONG&gt;(Y-1)-12-(D+1) 10:30:20&lt;/STRONG&gt; as a Close_Date&lt;/P&gt;

&lt;P&gt;Here is the implementation&lt;/P&gt;

&lt;OL&gt;
&lt;LI&gt;Retrive the last value of Close_Date, and take it as the initial date. Let's call it &lt;STRONG&gt;initial_date&lt;/STRONG&gt;&lt;/LI&gt;
&lt;LI&gt;Extract the &lt;STRONG&gt;year&lt;/STRONG&gt;, &lt;STRONG&gt;month&lt;/STRONG&gt;, &lt;STRONG&gt;day&lt;/STRONG&gt;, &lt;STRONG&gt;hour&lt;/STRONG&gt;, &lt;STRONG&gt;minutes&lt;/STRONG&gt;, and &lt;STRONG&gt;seconds&lt;/STRONG&gt; from &lt;STRONG&gt;initial_date&lt;/STRONG&gt;&lt;/LI&gt;
&lt;LI&gt;Start comparisions
    - case we are the 31  of the month 
    - case we are the 30  of the month 
    - case the day is between 1 and 29 and the month is not january
    - case the day is between 1 and 29 and the month is  january&lt;/LI&gt;
&lt;LI&gt;evaluate the &lt;STRONG&gt;target_Close_Date&lt;/STRONG&gt;&lt;/LI&gt;
&lt;/OL&gt;

&lt;P&gt;Here is the query:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;...|eventstats first(Close_Date) as initial_date|rex field=initial_date "^(?P&amp;lt;year&amp;gt;\d+)\-(?&amp;lt;month&amp;gt;\d+)\-(?&amp;lt;day&amp;gt;\d+)\s(?&amp;lt;hour&amp;gt;\d+)\:(?&amp;lt;min&amp;gt;\d+)\:(?&amp;lt;sec&amp;gt;\d+)"|eval target_day31=case(day=31,02)|eval target_day30=case(day=30,01)|eval target_day=case(day&amp;lt;30,day+1)|eval target_day=if(target_day&amp;lt;10,"0"+target_day,target_day)|eval target_month_day=month-01|eval target_month_day=if(target_month_day&amp;lt;10,"0"+target_month_day,target_month_day)|eval january_target_day=case(month=01,12)|eval january_target_year=year-1|eval target_Close_Date=case(day=31,year+"-"+month+"-"+target_day31+" "+hour+":"+min+":"+sec,day=30,year+"-"+month+"-"+target_day30+" "+hour+":"+min+":"+sec,day&amp;lt;30 AND month!=1,year+"-"+target_month_day+"-"+target_day+" "+hour+":"+min+":"+sec,day&amp;lt;30 AND month=1,january_target_year+"-"+january_target_day+"-"+target_day+" "+hour+":"+min+":"+sec)|where Close_Date=target_Close_Date|stats count by target_Close_Date
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Thanks.&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 20:02:36 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Timechart-based-on-date-and-time-string-NOT-the-timestamp/m-p/118580#M31669</guid>
      <dc:creator>stephanefotso</dc:creator>
      <dc:date>2020-09-28T20:02:36Z</dc:date>
    </item>
    <item>
      <title>Re: Timechart based on date and time string NOT the timestamp</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Timechart-based-on-date-and-time-string-NOT-the-timestamp/m-p/118581#M31670</link>
      <description>&lt;P&gt;Thank you for the response.  I also found another way to do this.  Here is the solution I used:&lt;/P&gt;

&lt;OL&gt;
&lt;LI&gt;I use strptime to convert Close_Date to epoc time.  &lt;CODE&gt;[ ...| eval CD=strptime(Close_Date, "%Y-%m-%d %H:%M:%S") |  ... ]&lt;/CODE&gt;&lt;/LI&gt;
&lt;LI&gt;I then use relative_time to calculate the epoc time of my benchmarks, 30 days, 60 days, and 90 days ago.  &lt;CODE&gt;[ ... | eval days30=relative_time(now(),"-30d@d") |  eval days60=relative_time(now(),"-60d@d") | eval days90=relative_time(now(),"-90d@d") | ... ]&lt;/CODE&gt;&lt;/LI&gt;
&lt;LI&gt;Use a nested if statement to calculate which window each event falls into.  &lt;CODE&gt;[ ... | eval Age=if(CDdays90,60,if(CDdays60,30,0))) | ... ]&lt;/CODE&gt;&lt;/LI&gt;
&lt;LI&gt;Get rid of the events that were closed in the last 30 days (different than my initial question... I know..).   &lt;CODE&gt;[ ... | where Age&amp;gt;1 | ... ]&lt;/CODE&gt;&lt;/LI&gt;
&lt;LI&gt;Display the results using stats.  &lt;CODE&gt;[ ... | stats count by Age ]&lt;/CODE&gt;&lt;/LI&gt;
&lt;/OL&gt;

&lt;P&gt;So, putting it all together:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;::my search here::  | eval CD=strptime(Close_Date, "%Y-%m-%d %H:%M:%S") | eval days30=relative_time(now(),"-30d@d") | eval days60=relative_time(now(),"-60d@d") | eval days90=relative_time(now(),"-90d@d") | eval Age=if(CD&amp;lt;days90,90,if(CD&amp;lt;days60 AND CD&amp;gt;days90,60,if(CD&amp;lt;days30 AND CD&amp;gt;days60,30,0))) | where Age&amp;gt;1 | stats count by Age
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 28 Sep 2020 20:09:49 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Timechart-based-on-date-and-time-string-NOT-the-timestamp/m-p/118581#M31670</guid>
      <dc:creator>JWBailey</dc:creator>
      <dc:date>2020-09-28T20:09:49Z</dc:date>
    </item>
  </channel>
</rss>

