<?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: Nested Ifs and Cases in a Query in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-work-with-nested-Ifs-and-cases-in-a-query/m-p/603852#M210070</link>
    <description>&lt;P&gt;Thanks!&amp;nbsp; This worked perfectly, and the nested cases instead of ifs really makes it a lot more understandable for me.&amp;nbsp; I'm pretty sure I will need to go one step deeper, so a nested nested case, but should be doable with some trial and error!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for pointing me the right way.&lt;/P&gt;</description>
    <pubDate>Wed, 29 Jun 2022 19:20:51 GMT</pubDate>
    <dc:creator>mjon395</dc:creator>
    <dc:date>2022-06-29T19:20:51Z</dc:date>
    <item>
      <title>How to work with nested Ifs and cases in a query?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-work-with-nested-Ifs-and-cases-in-a-query/m-p/603472#M210005</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;It's possible that I've had too long of a day, but I can't wrap my head around nesting many ifs.&amp;nbsp; Is anyone willing to help me out?&amp;nbsp; I am really bad at writing out SPL queries to make it visually understanding with parentheses and commas.&amp;nbsp; Does anyone have some additional tips on that as well that would be useful for these nested scenarios?&lt;/P&gt;
&lt;P&gt;For example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;| eval new_field = if(pass_fail="fail",
  if(importance="0" OR importance="1",
    case(
        Days&amp;lt;7 OR State="Online","Gold",
	Days &amp;gt;=7 AND Days&amp;lt;14,"Orange",
	Days&amp;gt;=14,"Red"),
    if(importance="2",
        case(
	    Days&amp;lt;30 OR State="Online","Gold",
	    Days &amp;gt;=30 AND Days&amp;lt;60,"Orange",
	    Days&amp;gt;=60,"Red"),
        if(importance="3",
            case(
	      Days&amp;lt;60 OR State="Online","Gold",
	      Days &amp;gt;=60 AND Days&amp;lt;120,"Orange",
	      Days&amp;gt;=120,"Red"),
            "importance_3_false"),
        "importance_2_false"),
    "importance_1_0_false"),
"main_if_fail")&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The idea is to break out into a newfield by first looking at only the "fail" items, and then further breaking down the "fail" items by their importance (which can be 0, 1, 2, 3) where 0&amp;amp;1, 2, and 3 have their own case statements.&amp;nbsp; All the case statements and ifs should be true, and the "importance_3_false" (for example) are more for debugging and should never actually show in my output.&lt;/P&gt;
&lt;P&gt;I appreciate any help and thank you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;Error in 'eval' command: The arguments to the 'if' function are invalid.&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jun 2022 23:02:25 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-work-with-nested-Ifs-and-cases-in-a-query/m-p/603472#M210005</guid>
      <dc:creator>mjon395</dc:creator>
      <dc:date>2022-06-29T23:02:25Z</dc:date>
    </item>
    <item>
      <title>Re: Nested Ifs and Cases in a Query</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-work-with-nested-Ifs-and-cases-in-a-query/m-p/603477#M210007</link>
      <description>&lt;P&gt;I believe you may be looking for this syntax&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| eval new_field = if(pass_fail="fail",
  case(
    importance="0" OR importance="1", case(
        Days&amp;lt;7 OR State="Online","Gold",
	    Days &amp;gt;=7 AND Days&amp;lt;14,"Orange",
	    Days&amp;gt;=14,"Red",
	    1==1,"importance_1_0_false"),
    importance="2", case(
	    Days&amp;lt;30 OR State="Online","Gold",
	    Days &amp;gt;=30 AND Days&amp;lt;60,"Orange",
	    Days&amp;gt;=60,"Red",
	    1==1,"importance_2_false"),
    importance="3", case(
        Days&amp;lt;60 OR State="Online","Gold",
	    Days &amp;gt;=60 AND Days&amp;lt;120,"Orange",
	    Days&amp;gt;=120,"Red",
        1==1,"importance_3_false"),
     1==1,"importance_fail"),
     "main_if_fail")&lt;/LI-CODE&gt;&lt;P&gt;It can often be confusing to use nested ifs, so above has a single if checking for pass_fail="fail".&lt;/P&gt;&lt;P&gt;After that it checks importance value with CASE not IF and then has the nested case to check for the Days condition.&lt;/P&gt;&lt;P&gt;Then there is a1==1,"importance_fail", which is the case where pass_fail="fail" and importance does not match 0,1,2 or 3.&lt;/P&gt;&lt;P&gt;Hope this helps.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Jun 2022 23:31:22 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-work-with-nested-Ifs-and-cases-in-a-query/m-p/603477#M210007</guid>
      <dc:creator>bowesmana</dc:creator>
      <dc:date>2022-06-27T23:31:22Z</dc:date>
    </item>
    <item>
      <title>Re: Nested Ifs and Cases in a Query</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-work-with-nested-Ifs-and-cases-in-a-query/m-p/603852#M210070</link>
      <description>&lt;P&gt;Thanks!&amp;nbsp; This worked perfectly, and the nested cases instead of ifs really makes it a lot more understandable for me.&amp;nbsp; I'm pretty sure I will need to go one step deeper, so a nested nested case, but should be doable with some trial and error!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for pointing me the right way.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jun 2022 19:20:51 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-work-with-nested-Ifs-and-cases-in-a-query/m-p/603852#M210070</guid>
      <dc:creator>mjon395</dc:creator>
      <dc:date>2022-06-29T19:20:51Z</dc:date>
    </item>
  </channel>
</rss>

