<?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: modify a query based on condition in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/modify-a-query-based-on-condition/m-p/302775#M91130</link>
    <description>&lt;P&gt;Hey try this run anywhere search,&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults | eval Date="04/04/2018",Start="04:04:04",End="05:05:05"|append [| makeresults | eval Date="04/03/2018",Start="04:03:48",End="05:15:05"]|append [| makeresults | eval Date="04/02/2018",Start="04:04:02",End="05:01:05"]|append [| makeresults | eval Date="03/30/2018",Start="04:08:04",End="05:05:25"]|append [| makeresults | eval Date="03/29/2018",Start="04:10:14",End="05:25:65"]|eval condition="04/02/2018"| eval condition1=strftime(strptime(condition,"%m/%d/%Y"),"%A")| eval condition2=if(condition1="Monday",relative_time(strptime(condition,"%m/%d/%Y"),"-3d@d"),relative_time(strptime(condition,"%m/%d/%Y"),"-1d@d")) | eval condition2=strftime(condition2,"%m/%d/%Y") | where Date=condition2 | table Date End Start
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;In your environment, you should try&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;lt;your query with Columns Date Start End&amp;gt;|eval condition="04/02/2018"| eval condition1=strftime(strptime(condition,"%m/%d/%Y"),"%A")| eval condition2=if(condition1="Monday",relative_time(strptime(condition,"%m/%d/%Y"),"-3d@d"),relative_time(strptime(condition,"%m/%d/%Y"),"-1d@d")) | eval condition2=strftime(condition2,"%m/%d/%Y") | where Date=condition2 | table Date End Start
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Put your date in the &lt;CODE&gt;conditon&lt;/CODE&gt; field and you will see the desired results. You can also make a &lt;CODE&gt;token&lt;/CODE&gt; to pass &lt;CODE&gt;date&lt;/CODE&gt; to &lt;CODE&gt;condition field&lt;/CODE&gt;.&lt;/P&gt;

&lt;P&gt;let me know if this helps!&lt;/P&gt;</description>
    <pubDate>Wed, 04 Apr 2018 08:07:58 GMT</pubDate>
    <dc:creator>mayurr98</dc:creator>
    <dc:date>2018-04-04T08:07:58Z</dc:date>
    <item>
      <title>modify a query based on condition</title>
      <link>https://community.splunk.com/t5/Splunk-Search/modify-a-query-based-on-condition/m-p/302773#M91128</link>
      <description>&lt;P&gt;Hi, &lt;/P&gt;

&lt;P&gt;I have created a query which gives me date, and start and end time of a job in the below format.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;   Date        Start          End
   04/04/2018  04:04:04       05:05:05
   04/03/2018  04:03:48       05:15:05
   04/02/2018  04:04:02       05:01:05
   03/30/2018  04:08:04       05:05:25
   03/29/2018  04:10:14       05:25:65
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I want to get the latest run of this set. example: on 04/05/2018, I should get 04/04 data. But for 04/02, I should be seeing 03/30 since that was the latest run at that point of time. This is because of Monday and other days of the job run.&lt;/P&gt;

&lt;P&gt;This is the output I am looking for :&lt;/P&gt;

&lt;P&gt;on 04/05:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;   Date        Start          End
   04/04/2018  04:04:04       05:05:05
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;on 04/02: &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;   Date        Start          End
   03/30/2018  04:08:04       05:05:25
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I know that I need to check if the day is Monday, then I need to look for last business day which would be Friday, if its not Monday, then look for previous Business day which would be the day prior. But I am not able to put this logic in Splunk. &lt;/P&gt;

&lt;P&gt;Here is what I have tried, but I am not sure how to get my condition checked.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; ..|eval epochtime=strptime(ProcessDate,"%Y/%m/%d")  | eval dayofweekfull=strftime(epochtime,"%A")  | eval checking=if(dayofweekfull="Monday","I am not sure how I can check this here as this is text and not a field that I can initialize","same here")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Please let me know if I am too confused for a easy problem&lt;/P&gt;</description>
      <pubDate>Wed, 04 Apr 2018 06:41:55 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/modify-a-query-based-on-condition/m-p/302773#M91128</guid>
      <dc:creator>dileepsri9</dc:creator>
      <dc:date>2018-04-04T06:41:55Z</dc:date>
    </item>
    <item>
      <title>Re: modify a query based on condition</title>
      <link>https://community.splunk.com/t5/Splunk-Search/modify-a-query-based-on-condition/m-p/302774#M91129</link>
      <description>&lt;P&gt;Try something like this, you might want to modify |where clause condition as per your validation&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;|inputlookup time.csv| eval Date1=Date |eval epoch=strptime(Date,"%m/%d/%Y")|eval check_date="04/04/2018"|eval check_date_epoch=strptime(check_date,"%m/%d/%Y")|where epoch=check_date_epoch |stats last(Start) last(End) by Date
&lt;/CODE&gt;&lt;/PRE&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/4668iF28FFAA2B24BF6D3/image-size/large?v=v2&amp;amp;px=999" role="button" title="alt text" alt="alt text" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Apr 2018 07:15:54 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/modify-a-query-based-on-condition/m-p/302774#M91129</guid>
      <dc:creator>splunker12er</dc:creator>
      <dc:date>2018-04-04T07:15:54Z</dc:date>
    </item>
    <item>
      <title>Re: modify a query based on condition</title>
      <link>https://community.splunk.com/t5/Splunk-Search/modify-a-query-based-on-condition/m-p/302775#M91130</link>
      <description>&lt;P&gt;Hey try this run anywhere search,&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults | eval Date="04/04/2018",Start="04:04:04",End="05:05:05"|append [| makeresults | eval Date="04/03/2018",Start="04:03:48",End="05:15:05"]|append [| makeresults | eval Date="04/02/2018",Start="04:04:02",End="05:01:05"]|append [| makeresults | eval Date="03/30/2018",Start="04:08:04",End="05:05:25"]|append [| makeresults | eval Date="03/29/2018",Start="04:10:14",End="05:25:65"]|eval condition="04/02/2018"| eval condition1=strftime(strptime(condition,"%m/%d/%Y"),"%A")| eval condition2=if(condition1="Monday",relative_time(strptime(condition,"%m/%d/%Y"),"-3d@d"),relative_time(strptime(condition,"%m/%d/%Y"),"-1d@d")) | eval condition2=strftime(condition2,"%m/%d/%Y") | where Date=condition2 | table Date End Start
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;In your environment, you should try&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;lt;your query with Columns Date Start End&amp;gt;|eval condition="04/02/2018"| eval condition1=strftime(strptime(condition,"%m/%d/%Y"),"%A")| eval condition2=if(condition1="Monday",relative_time(strptime(condition,"%m/%d/%Y"),"-3d@d"),relative_time(strptime(condition,"%m/%d/%Y"),"-1d@d")) | eval condition2=strftime(condition2,"%m/%d/%Y") | where Date=condition2 | table Date End Start
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Put your date in the &lt;CODE&gt;conditon&lt;/CODE&gt; field and you will see the desired results. You can also make a &lt;CODE&gt;token&lt;/CODE&gt; to pass &lt;CODE&gt;date&lt;/CODE&gt; to &lt;CODE&gt;condition field&lt;/CODE&gt;.&lt;/P&gt;

&lt;P&gt;let me know if this helps!&lt;/P&gt;</description>
      <pubDate>Wed, 04 Apr 2018 08:07:58 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/modify-a-query-based-on-condition/m-p/302775#M91130</guid>
      <dc:creator>mayurr98</dc:creator>
      <dc:date>2018-04-04T08:07:58Z</dc:date>
    </item>
    <item>
      <title>Re: modify a query based on condition</title>
      <link>https://community.splunk.com/t5/Splunk-Search/modify-a-query-based-on-condition/m-p/302776#M91131</link>
      <description>&lt;P&gt;@mayurr98, sorry maybe my statement was misunderstood, I want to check the current date and if it is Monday, then give Friday's value, if Tuesday give Monday's value, if Wednesday give Tuesday's value and so on, not that looking at the data from table&lt;/P&gt;</description>
      <pubDate>Wed, 04 Apr 2018 08:31:56 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/modify-a-query-based-on-condition/m-p/302776#M91131</guid>
      <dc:creator>dileepsri9</dc:creator>
      <dc:date>2018-04-04T08:31:56Z</dc:date>
    </item>
    <item>
      <title>Re: modify a query based on condition</title>
      <link>https://community.splunk.com/t5/Splunk-Search/modify-a-query-based-on-condition/m-p/302777#M91132</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;I have replicated your sample above with a csv so you can ignore the first line. &lt;BR /&gt;
In any case, instead of using string values indicating the day of the week, I am simply sort by epoch time (ascendant) and then using streamstats to display the previous value (window=1, current=false). Let me know if that helps or I didn't fully understand your request:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| inputcsv mycsv.csv
| eval epochtime=strptime(Date,"%m/%d/%Y")  
| eval dayofweekfull=strftime(epochtime,"%A")
| sort +epochtime
| streamstats window=1 current=f values(End) as LatestRun_End values(Start) as LatestRun_Start
| sort -epochtime
| table Date, Start, End, LatestRun_Start, LatestRun_End
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Output:&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/4670i615949334696BE89/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;Thanks,&lt;BR /&gt;
J&lt;/P&gt;</description>
      <pubDate>Wed, 04 Apr 2018 08:57:19 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/modify-a-query-based-on-condition/m-p/302777#M91132</guid>
      <dc:creator>javiergn</dc:creator>
      <dc:date>2018-04-04T08:57:19Z</dc:date>
    </item>
    <item>
      <title>Re: modify a query based on condition</title>
      <link>https://community.splunk.com/t5/Splunk-Search/modify-a-query-based-on-condition/m-p/302778#M91133</link>
      <description>&lt;P&gt;Hi J, as your output says, I don't need the full output. I just need the latest run details. So if I am running my query I just need to see 04/04 details as on 04/04/2018. my output should look something like this.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Date        Start          End
04/04/2018  04:04:04       05:05:05
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 04 Apr 2018 11:36:03 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/modify-a-query-based-on-condition/m-p/302778#M91133</guid>
      <dc:creator>dileepsri9</dc:creator>
      <dc:date>2018-04-04T11:36:03Z</dc:date>
    </item>
    <item>
      <title>Re: modify a query based on condition</title>
      <link>https://community.splunk.com/t5/Splunk-Search/modify-a-query-based-on-condition/m-p/302779#M91134</link>
      <description>&lt;P&gt;@dileepsri9, do you want to do this in Search or Dashboard?&lt;/P&gt;

&lt;P&gt;Refer to one of my previous answer on similar lines (with run anywhere example), which was for dashboard: &lt;A href="https://answers.splunk.com/answers/620104/how-to-set-my-default-time-to-previous-business-da-1.html"&gt;https://answers.splunk.com/answers/620104/how-to-set-my-default-time-to-previous-business-da-1.html&lt;/A&gt; &lt;/P&gt;</description>
      <pubDate>Wed, 04 Apr 2018 13:01:45 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/modify-a-query-based-on-condition/m-p/302779#M91134</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2018-04-04T13:01:45Z</dc:date>
    </item>
    <item>
      <title>Re: modify a query based on condition</title>
      <link>https://community.splunk.com/t5/Splunk-Search/modify-a-query-based-on-condition/m-p/302780#M91135</link>
      <description>&lt;P&gt;Then you can simply limit the latest sort to only return the top 1 and adjust the table command to display the fields you want, as in:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; | sort limit=1 -epochtime
 | table YOUR LIST OF FIELDS HERE
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Other than that, is the logic in my previous comment what you were expecting to see?&lt;/P&gt;</description>
      <pubDate>Wed, 04 Apr 2018 13:21:53 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/modify-a-query-based-on-condition/m-p/302780#M91135</guid>
      <dc:creator>javiergn</dc:creator>
      <dc:date>2018-04-04T13:21:53Z</dc:date>
    </item>
    <item>
      <title>Re: modify a query based on condition</title>
      <link>https://community.splunk.com/t5/Splunk-Search/modify-a-query-based-on-condition/m-p/302781#M91136</link>
      <description>&lt;P&gt;But if the job is not started yet on Tuesday morning, it will be returning Friday's run where it should be waiting for Monday's run.&lt;/P&gt;</description>
      <pubDate>Wed, 04 Apr 2018 15:18:19 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/modify-a-query-based-on-condition/m-p/302781#M91136</guid>
      <dc:creator>dileepsri9</dc:creator>
      <dc:date>2018-04-04T15:18:19Z</dc:date>
    </item>
    <item>
      <title>Re: modify a query based on condition</title>
      <link>https://community.splunk.com/t5/Splunk-Search/modify-a-query-based-on-condition/m-p/302782#M91137</link>
      <description>&lt;P&gt;@niketnilay,I am looking for this exactly, but this one looks very complex for me as I am a starter. Could you please explain me a bit more so that I can incorporate this for my requirement. On what variables should I be using and how should these need to be changed&lt;/P&gt;</description>
      <pubDate>Wed, 04 Apr 2018 15:22:13 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/modify-a-query-based-on-condition/m-p/302782#M91137</guid>
      <dc:creator>dileepsri9</dc:creator>
      <dc:date>2018-04-04T15:22:13Z</dc:date>
    </item>
    <item>
      <title>Re: modify a query based on condition</title>
      <link>https://community.splunk.com/t5/Splunk-Search/modify-a-query-based-on-condition/m-p/302783#M91138</link>
      <description>&lt;P&gt;@Splunker12er, I am not looking for a hardcoded date, I am looking to check if today is Monday, retrieve Friday's run details, if its non Monday retrieve the previous day details.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Apr 2018 07:32:03 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/modify-a-query-based-on-condition/m-p/302783#M91138</guid>
      <dc:creator>dileepsri9</dc:creator>
      <dc:date>2018-04-05T07:32:03Z</dc:date>
    </item>
  </channel>
</rss>

