<?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: Help with conditional event count. in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Help-with-conditional-event-count/m-p/708858#M239645</link>
    <description>&lt;P&gt;Had I not chosen the solution already I would have given it to you for a more comprehensive answer &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Wed, 15 Jan 2025 10:24:55 GMT</pubDate>
    <dc:creator>AFKunc</dc:creator>
    <dc:date>2025-01-15T10:24:55Z</dc:date>
    <item>
      <title>Help with conditional event count.</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Help-with-conditional-event-count/m-p/708549#M239604</link>
      <description>&lt;P&gt;Hi, I have json data structured as follows:&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;{
  "payload": {
    "status": "ok", # or "degraded"
  }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to use the &lt;EM&gt;stats&lt;/EM&gt; command to count the "ok" and "degraded" events separately. I am using the following query:&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;index=whatever | eval is_ok=if(payload.status=="ok", 1, 0) | stats count as total, count(is_ok) as ok_count&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have tried passing it through&amp;nbsp;&lt;EM&gt;spath&lt;/EM&gt;, , with "=" in the&amp;nbsp;&lt;EM&gt;if&lt;/EM&gt; condition,&amp;nbsp; and several other approaches changes. What always happens is that both counts contain all elements, despite there being different numbers of them. Please help!&lt;/P&gt;</description>
      <pubDate>Sun, 12 Jan 2025 15:17:51 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Help-with-conditional-event-count/m-p/708549#M239604</guid>
      <dc:creator>AFKunc</dc:creator>
      <dc:date>2025-01-12T15:17:51Z</dc:date>
    </item>
    <item>
      <title>Re: Help with conditional event count.</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Help-with-conditional-event-count/m-p/708555#M239605</link>
      <description>&lt;P&gt;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/275496"&gt;@AFKunc&lt;/a&gt;&lt;/P&gt;&lt;P&gt;Kindly verify if the JSON data has been onboarded correctly. I tested it using the same data you provided. Could you confirm if this is the data you were expecting?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="kiran_panchavat_0-1736697625278.png" style="width: 400px;"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/34112iCD147995D7F2CFC0/image-size/medium?v=v2&amp;amp;px=400" role="button" title="kiran_panchavat_0-1736697625278.png" alt="kiran_panchavat_0-1736697625278.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="kiran_panchavat_1-1736697711660.png" style="width: 400px;"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/34113i78FEDE407BE6EFF3/image-size/medium?v=v2&amp;amp;px=400" role="button" title="kiran_panchavat_1-1736697711660.png" alt="kiran_panchavat_1-1736697711660.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I hope this helps, if any reply helps you, you could add your upvote/karma points to that reply, thanks.&lt;/P&gt;</description>
      <pubDate>Sun, 12 Jan 2025 16:02:23 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Help-with-conditional-event-count/m-p/708555#M239605</guid>
      <dc:creator>kiran_panchavat</dc:creator>
      <dc:date>2025-01-12T16:02:23Z</dc:date>
    </item>
    <item>
      <title>Re: Help with conditional event count.</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Help-with-conditional-event-count/m-p/708558#M239606</link>
      <description>&lt;P&gt;Try using sum rather than count&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index=whatever | eval is_ok=if(payload.status=="ok", 1, 0) | stats count as total, sum(is_ok) as ok_count&lt;/LI-CODE&gt;</description>
      <pubDate>Sun, 12 Jan 2025 16:28:51 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Help-with-conditional-event-count/m-p/708558#M239606</guid>
      <dc:creator>ITWhisperer</dc:creator>
      <dc:date>2025-01-12T16:28:51Z</dc:date>
    </item>
    <item>
      <title>Re: Help with conditional event count.</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Help-with-conditional-event-count/m-p/708561#M239607</link>
      <description>&lt;P&gt;It still fails in that it appears that the&amp;nbsp;&lt;EM&gt;if(payload.status==...)&lt;/EM&gt; always evaluates to false, despite there being both &lt;EM&gt;"ok"&lt;/EM&gt; and &lt;EM&gt;"degraded"&lt;/EM&gt; events, so the sum is equal to the count of all events.&lt;/P&gt;</description>
      <pubDate>Sun, 12 Jan 2025 19:32:28 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Help-with-conditional-event-count/m-p/708561#M239607</guid>
      <dc:creator>AFKunc</dc:creator>
      <dc:date>2025-01-12T19:32:28Z</dc:date>
    </item>
    <item>
      <title>Re: Help with conditional event count.</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Help-with-conditional-event-count/m-p/708562#M239608</link>
      <description>&lt;P&gt;Worked like a charm. This line seems to be making all the difference:&amp;nbsp;&lt;EM&gt;| spath path=payload.status output=status&lt;/EM&gt;.&lt;/P&gt;</description>
      <pubDate>Sun, 12 Jan 2025 19:43:41 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Help-with-conditional-event-count/m-p/708562#M239608</guid>
      <dc:creator>AFKunc</dc:creator>
      <dc:date>2025-01-12T19:43:41Z</dc:date>
    </item>
    <item>
      <title>Re: Help with conditional event count.</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Help-with-conditional-event-count/m-p/708563#M239609</link>
      <description>&lt;P&gt;Two problems with the search.&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;In an evaluation function, deep path&amp;nbsp;payload.status needs to be single quoted (i.e., 'payload.status') to dereference its value. &amp;nbsp;Otherwise bare word&amp;nbsp;&lt;U&gt;payload.status&lt;/U&gt; evaluates to null.&lt;/LI&gt;&lt;LI&gt;If you want to use&amp;nbsp;count(is_ok), you should make the "other" value disappear, i.e., make it be a null, not a "real" value of 0. &amp;nbsp;If you think 0 is a better representation for "other", use sum as&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/225168"&gt;@ITWhisperer&lt;/a&gt;&amp;nbsp;suggests.&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;In other words, on mock event sequence&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;_raw&lt;/TD&gt;&lt;TD&gt;payload.status&lt;/TD&gt;&lt;TD&gt;seq&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;{"seq":1,"payload":{"status":"ok"}}&lt;/TD&gt;&lt;TD&gt;ok&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;{"seq":2,"payload":{"status":"degraded"}}&lt;/TD&gt;&lt;TD&gt;degraded&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;{"seq":3,"payload":{"status":"ok"}}&lt;/TD&gt;&lt;TD&gt;ok&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;either&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| eval is_ok=if('payload.status'=="ok", 1, null())
| stats count as total, count(is_ok) as ok_count&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;or&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| eval is_ok=if('payload.status'=="ok", 1, 0)
| stats count as total, sum(is_ok) as ok_count&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;or even&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| eval is_ok=if('payload.status'=="ok", 1, 0)
| stats count as total, count(eval(is_ok = 1)) as ok_count&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;should give you&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;total&lt;/TD&gt;&lt;TD&gt;ok_count&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;This is an emulation you can play with and compare with real data&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults format=json data="[
{
  \"seq\": 1,
  \"payload\": {
    \"status\": \"ok\",
  }
},
{
  \"seq\": 2,
  \"payload\": {
    \"status\": \"degraded\",
  }
},
{
  \"seq\": 3,
  \"payload\": {
    \"status\": \"ok\",
  }
}
]"
| fields - payload, seq, _time
| spath
``` data emulation above ```&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 12 Jan 2025 19:57:47 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Help-with-conditional-event-count/m-p/708563#M239609</guid>
      <dc:creator>yuanliu</dc:creator>
      <dc:date>2025-01-12T19:57:47Z</dc:date>
    </item>
    <item>
      <title>Re: Help with conditional event count.</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Help-with-conditional-event-count/m-p/708858#M239645</link>
      <description>&lt;P&gt;Had I not chosen the solution already I would have given it to you for a more comprehensive answer &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jan 2025 10:24:55 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Help-with-conditional-event-count/m-p/708858#M239645</guid>
      <dc:creator>AFKunc</dc:creator>
      <dc:date>2025-01-15T10:24:55Z</dc:date>
    </item>
  </channel>
</rss>

