<?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 create an If/Else statement inside an eval statement? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-an-If-Else-statement-inside-an-eval-statement/m-p/351297#M103979</link>
    <description>&lt;P&gt;You can use case statement:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;|eval fieldA = case(Project=="projectA","14-n",Project=="projectB","n-3",1==1,"15-n")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;now &lt;CODE&gt;fieldA&lt;/CODE&gt; has required output ...You can use as per requirement&lt;/P&gt;</description>
    <pubDate>Wed, 31 Jan 2018 15:26:15 GMT</pubDate>
    <dc:creator>493669</dc:creator>
    <dc:date>2018-01-31T15:26:15Z</dc:date>
    <item>
      <title>How to create an If/Else statement inside an eval statement?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-an-If-Else-statement-inside-an-eval-statement/m-p/351296#M103978</link>
      <description>&lt;P&gt;Hello All,&lt;/P&gt;

&lt;P&gt;I am running the following search:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index="ledata_2017" NOT Project="60*"
| stats sum(ActualPTDCostsAMT) , sum(LEThisMthCostsAMT) 
| eval ActualTotal=-ActualTotal, LETotal=-LETotal, n=max(1,2,3,4,5,6,7,8,9,10,11,0,MonthNum)
| eval YTDAvg=(ActualTotal/n), YTGAvg=(LETotal-ActualTotal)/(15-n)
| table Project,Ratio,Number
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I want to be able to include an if-else statement inside line 4, where I can indicate:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;If projectA, then 14-n
else if projectB then n-3,
else if 15-n (for the rest of the projects) 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Is this possible?&lt;/P&gt;

&lt;P&gt;Thank you all!&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2018 15:15:15 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-create-an-If-Else-statement-inside-an-eval-statement/m-p/351296#M103978</guid>
      <dc:creator>tonahoyos</dc:creator>
      <dc:date>2018-01-31T15:15:15Z</dc:date>
    </item>
    <item>
      <title>Re: How to create an If/Else statement inside an eval statement?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-an-If-Else-statement-inside-an-eval-statement/m-p/351297#M103979</link>
      <description>&lt;P&gt;You can use case statement:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;|eval fieldA = case(Project=="projectA","14-n",Project=="projectB","n-3",1==1,"15-n")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;now &lt;CODE&gt;fieldA&lt;/CODE&gt; has required output ...You can use as per requirement&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2018 15:26:15 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-create-an-If-Else-statement-inside-an-eval-statement/m-p/351297#M103979</guid>
      <dc:creator>493669</dc:creator>
      <dc:date>2018-01-31T15:26:15Z</dc:date>
    </item>
    <item>
      <title>Re: How to create an If/Else statement inside an eval statement?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-an-If-Else-statement-inside-an-eval-statement/m-p/351298#M103980</link>
      <description>&lt;P&gt;@tonahoyos, you ca try the following, however keep in mind the following:&lt;/P&gt;

&lt;P&gt;1) All fields to be used after the stats pipe must be included in the stats command like Project MonthNum etc.&lt;BR /&gt;
2) &lt;CODE&gt;Project!="60*"&lt;/CODE&gt; and &lt;CODE&gt;NOT Project="60*"&lt;/CODE&gt; are different. Make sure you use correct one in your base search.&lt;BR /&gt;
3) &lt;CODE&gt;Ratio&lt;/CODE&gt; and &lt;CODE&gt;Number&lt;/CODE&gt; fields in the final &lt;CODE&gt;table&lt;/CODE&gt; pipe are not calculated in previous pipes.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index="ledata_2017" Project!="60*"
| stats sum(ActualPTDCostsAMT) as ActualTotal , sum(LEThisMthCostsAMT) as LETotal by Project MonthNum
| eval ActualTotal=-ActualTotal, LETotal=-LETotal, n=max(1,2,3,4,5,6,7,8,9,10,11,0,MonthNum)
| eval divisor = case(Project=="projectA",14-n,Project=="projectB",n-3,true(),15-n)
| eval YTDAvg=(ActualTotal/n), YTGAvg=(LETotal-ActualTotal)/divisor
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Following is the run anywhere search based on Splunk's _internal index on similar lines as per the question:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index="_internal" log_level!="INFO"
| stats sum(date_second) as ActualTotal , sum(date_hour) as LETotal by log_level date_month
| eval ActualTotal=-ActualTotal, LETotal=-LETotal, n=max(1,2,3,4,5,6,7,8,9,10,11,0,date_mday)
| eval divisor = case(log_level=="ERROR",14-n,log_level=="WARN",n-3,true(),15-n)
| eval YTDAvg=(ActualTotal/n), YTGAvg=(LETotal-ActualTotal)/divisor
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 31 Jan 2018 15:52:11 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-create-an-If-Else-statement-inside-an-eval-statement/m-p/351298#M103980</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2018-01-31T15:52:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to create an If/Else statement inside an eval statement?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-an-If-Else-statement-inside-an-eval-statement/m-p/351299#M103981</link>
      <description>&lt;P&gt;What does the 1==1 do?&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2018 15:53:40 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-create-an-If-Else-statement-inside-an-eval-statement/m-p/351299#M103981</guid>
      <dc:creator>tonahoyos</dc:creator>
      <dc:date>2018-01-31T15:53:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to create an If/Else statement inside an eval statement?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-an-If-Else-statement-inside-an-eval-statement/m-p/351300#M103982</link>
      <description>&lt;P&gt;if it does not match first two conditions then else condition is specified by 1==1 &lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2018 15:56:45 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-create-an-If-Else-statement-inside-an-eval-statement/m-p/351300#M103982</guid>
      <dc:creator>493669</dc:creator>
      <dc:date>2018-01-31T15:56:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to create an If/Else statement inside an eval statement?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-an-If-Else-statement-inside-an-eval-statement/m-p/351301#M103983</link>
      <description>&lt;P&gt;This is how I included your recommendation, thank you! I will double check my results and see if there is anything wrong. Let me know if you see any inconsistencies in the code. Thanks again!&lt;/P&gt;

&lt;P&gt;| stats sum(ActualPTDCostsAMT) as ActualTotal, sum(LEThisMthCostsAMT) as LETotal by Project &lt;BR /&gt;
| eval ActualTotal=-ActualTotal, LETotal=-LETotal, n=max(1,2,3,4,5,6,7,8,9,10,11,0,MonthNum)&lt;BR /&gt;
| eval divisor1= case(Project=="1405688",14-n, true(),15-n), &lt;BR /&gt;
           divisor2= case(Project=="1408525",n-3,Project=="1410522",n-4,Project=="1404501",n-4,&lt;BR /&gt;&lt;BR /&gt;
           Project=="1409599",n-3, true(),n)&lt;BR /&gt;
| eval YTDAvg=(ActualTotal/divisor2), YTGAvg=(LETotal-ActualTotal)/divisor1&lt;BR /&gt;
| eval Ratio=YTGAvg/YTDAvg&lt;BR /&gt;
| eval Number=1&lt;BR /&gt;
| table Project,Ratio,Number&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2018 16:22:50 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-create-an-If-Else-statement-inside-an-eval-statement/m-p/351301#M103983</guid>
      <dc:creator>tonahoyos</dc:creator>
      <dc:date>2018-01-31T16:22:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to create an If/Else statement inside an eval statement?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-an-If-Else-statement-inside-an-eval-statement/m-p/351302#M103984</link>
      <description>&lt;P&gt;@tonahoyos, slight correction in your stats command &lt;CODE&gt;| stats .... by Project MonthNum&lt;/CODE&gt;, since &lt;CODE&gt;MonthNum&lt;/CODE&gt; is used in deciding &lt;CODE&gt;n&lt;/CODE&gt; in subsequent eval. I think rest looks fine. Let us know if anything does not work!&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2018 16:38:49 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-create-an-If-Else-statement-inside-an-eval-statement/m-p/351302#M103984</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2018-01-31T16:38:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to create an If/Else statement inside an eval statement?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-an-If-Else-statement-inside-an-eval-statement/m-p/351303#M103985</link>
      <description>&lt;P&gt;Thank you! Everything is acting well, so far!&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2018 16:46:11 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-create-an-If-Else-statement-inside-an-eval-statement/m-p/351303#M103985</guid>
      <dc:creator>tonahoyos</dc:creator>
      <dc:date>2018-01-31T16:46:11Z</dc:date>
    </item>
  </channel>
</rss>

