<?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 stats count for different days in separate fields in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/stats-count-for-different-days-in-separate-fields/m-p/508134#M142003</link>
    <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I’m trying to get product count for yesterday and 7 days ago from yesterday in two separate fields, results are coming back correct for yesterday but for the second field all the results are zero. I wanted to know if my logic is correct. &amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is what I have:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index = something host = something 
| where ResponseCode = “Success”
| stats count as “Product Count Yesterday”, 
        count (eval (relative_time(now(), “-8d@d”))) as “Product Count 7 days ago” by product
| sort product desc &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 08 Jul 2020 15:26:21 GMT</pubDate>
    <dc:creator>maxmukimov</dc:creator>
    <dc:date>2020-07-08T15:26:21Z</dc:date>
    <item>
      <title>stats count for different days in separate fields</title>
      <link>https://community.splunk.com/t5/Splunk-Search/stats-count-for-different-days-in-separate-fields/m-p/508134#M142003</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I’m trying to get product count for yesterday and 7 days ago from yesterday in two separate fields, results are coming back correct for yesterday but for the second field all the results are zero. I wanted to know if my logic is correct. &amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is what I have:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index = something host = something 
| where ResponseCode = “Success”
| stats count as “Product Count Yesterday”, 
        count (eval (relative_time(now(), “-8d@d”))) as “Product Count 7 days ago” by product
| sort product desc &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Jul 2020 15:26:21 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/stats-count-for-different-days-in-separate-fields/m-p/508134#M142003</guid>
      <dc:creator>maxmukimov</dc:creator>
      <dc:date>2020-07-08T15:26:21Z</dc:date>
    </item>
    <item>
      <title>Re: stats count for different days in separate fields</title>
      <link>https://community.splunk.com/t5/Splunk-Search/stats-count-for-different-days-in-separate-fields/m-p/508153#M142005</link>
      <description>&lt;P&gt;If &lt;FONT face="courier new,courier"&gt;stats count&lt;/FONT&gt; is returning the right value for yesterday then your time picker must be set to yesterday.&amp;nbsp; That means no events will be read for last week so the second number will be zero.&lt;/P&gt;&lt;P&gt;Changing the time window to 8 days ago will break the first count.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Try this query&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index = something host = something earliest=-8d@d
| where ResponseCode = “Success”
| case period=case(_time&amp;lt;relative_time(now(), "-1d@d"), "yesterday", 1==1, "last week")
| stats count(eval(period="yesterday")) as “Product Count Yesterday”, 
        count(eval(period="last week")) as “Product Count 7 days ago” by product
| sort product desc&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 08 Jul 2020 17:00:52 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/stats-count-for-different-days-in-separate-fields/m-p/508153#M142005</guid>
      <dc:creator>richgalloway</dc:creator>
      <dc:date>2020-07-08T17:00:52Z</dc:date>
    </item>
    <item>
      <title>Re: stats count for different days in separate fields</title>
      <link>https://community.splunk.com/t5/Splunk-Search/stats-count-for-different-days-in-separate-fields/m-p/508205#M142016</link>
      <description>&lt;P&gt;Just for fun to show you how many ways there are to achieve the same goal with Splunk, here are two ways you can also do it - there is a performance consideration - see comments at end&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index = something host = something ResponseCode="Success" earliest=-8d@d latest=@d
| bin _time span=1d
| stats count as ProductCount by product, _time
| where _time=relative_time(now(),"-d@d") OR _time=relative_time(now(),"-8d@d")
| eval when=if(_time=relative_time(now(),"-d@d"), "Yesterday", "Last Week")
| eval "Product Count {when}"=ProductCount
| fields - _time ProductCount when
| stats values(*) as * by product
| sort - product&lt;/LI-CODE&gt;&lt;P&gt;I have included the date ranges in the search itself. This will bin the counts be each day of the week for the last 8 days and then filter only yesterday and 8 days ago before then doing the field naming for the counts at the end.&lt;/P&gt;&lt;P&gt;This is a similar way, which does the count evaluation in the stats command itself&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index = something host = something ResponseCode="Success" earliest=-8d@d latest=@d
| bin _time span=1d
| stats count(eval(_time=relative_time(now(),"-d@d"))) as "Product Count Yesterday" count(eval(_time=relative_time(now(),"-8d@d"))) as "Product Count Last Week" by product, _time
| where _time=relative_time(now(),"-d@d") OR _time=relative_time(now(),"-8d@d")
| fields - _time 
| stats values(*) as * by product
| eval "Product Count Yesterday"=mvfilter('Product Count Yesterday'&amp;gt;0), "Product Count Last Week"=mvfilter('Product Count Last Week'&amp;gt;0)
| sort - product&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;From a performance point of view, the second is less efficient, as it is evaluating relative_time for every event in the stats, which is not necessary, as that can be delayed until after the stats.&lt;/P&gt;&lt;P&gt;Also, the second will ignore any product counts where the count is 0 as it is removing 0 from the counts in the final eval&lt;/P&gt;&lt;P&gt;In my tests, the first example consistently takes ~9 seconds for 1.3m events, whereas the second takes ~13 for the same.&lt;/P&gt;</description>
      <pubDate>Wed, 08 Jul 2020 23:39:43 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/stats-count-for-different-days-in-separate-fields/m-p/508205#M142016</guid>
      <dc:creator>bowesmana</dc:creator>
      <dc:date>2020-07-08T23:39:43Z</dc:date>
    </item>
    <item>
      <title>Re: stats count for different days in separate fields</title>
      <link>https://community.splunk.com/t5/Splunk-Search/stats-count-for-different-days-in-separate-fields/m-p/508366#M142047</link>
      <description>&lt;P&gt;Thank you.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jul 2020 17:53:47 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/stats-count-for-different-days-in-separate-fields/m-p/508366#M142047</guid>
      <dc:creator>maxmukimov</dc:creator>
      <dc:date>2020-07-09T17:53:47Z</dc:date>
    </item>
    <item>
      <title>Re: stats count for different days in separate fields</title>
      <link>https://community.splunk.com/t5/Splunk-Search/stats-count-for-different-days-in-separate-fields/m-p/508371#M142048</link>
      <description>&lt;P&gt;Thank you&amp;nbsp;&lt;SPAN class="UserName lia-user-name lia-user-rank-Motivator lia-component-message-view-widget-author-username"&gt;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/6367"&gt;@bowesmana&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;First option seems to be working, however, I'm not getting separate columns for Yesterday and Last week.&amp;nbsp; Results for both days are getting displayed in one column.&amp;nbsp; &amp;nbsp;Attached the screenshot.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="1.PNG" style="width: 400px;"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/9592i18DF34B2624248C1/image-size/medium?v=v2&amp;amp;px=400" role="button" title="1.PNG" alt="1.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt; &lt;/P&gt;</description>
      <pubDate>Thu, 09 Jul 2020 18:09:04 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/stats-count-for-different-days-in-separate-fields/m-p/508371#M142048</guid>
      <dc:creator>maxmukimov</dc:creator>
      <dc:date>2020-07-09T18:09:04Z</dc:date>
    </item>
    <item>
      <title>Re: stats count for different days in separate fields</title>
      <link>https://community.splunk.com/t5/Splunk-Search/stats-count-for-different-days-in-separate-fields/m-p/508410#M142060</link>
      <description>&lt;P&gt;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/223362"&gt;@maxmukimov&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can you post your exact search. I can see that 'Product Count' field name is there, which means the field assignation is happening, but without the value of {when}. In my example, when cannot be empty, so it shouldn't happen...&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jul 2020 21:57:54 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/stats-count-for-different-days-in-separate-fields/m-p/508410#M142060</guid>
      <dc:creator>bowesmana</dc:creator>
      <dc:date>2020-07-09T21:57:54Z</dc:date>
    </item>
    <item>
      <title>Re: stats count for different days in separate fields</title>
      <link>https://community.splunk.com/t5/Splunk-Search/stats-count-for-different-days-in-separate-fields/m-p/508527#M142092</link>
      <description>&lt;P&gt;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/6367"&gt;@bowesmana&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;There was a typo, instead of:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| stats count as ProductCount by product, _time&lt;/LI-CODE&gt;&lt;P&gt;I typed&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| stats count as "Product Count" by product, _time&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Jul 2020 17:02:37 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/stats-count-for-different-days-in-separate-fields/m-p/508527#M142092</guid>
      <dc:creator>maxmukimov</dc:creator>
      <dc:date>2020-07-10T17:02:37Z</dc:date>
    </item>
  </channel>
</rss>

