<?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 display the data for the last 4 weeks by week number? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-display-the-data-for-the-last-4-weeks-by-week-number/m-p/366478#M166444</link>
    <description>&lt;P&gt;@DalJeanis, this is really great! I have learned a lot from this query &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Wed, 20 Dec 2017 04:45:52 GMT</pubDate>
    <dc:creator>auaave</dc:creator>
    <dc:date>2017-12-20T04:45:52Z</dc:date>
    <item>
      <title>How to display the data for the last 4 weeks by week number?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-display-the-data-for-the-last-4-weeks-by-week-number/m-p/366471#M166437</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;I want to create dashboard that displays the 4 weeks data by week number. The database normally have 3 months of data.&lt;/P&gt;

&lt;P&gt;What should I do to display only the last 4 weeks data by week #, sort the table based on the values of the most recent week?&lt;/P&gt;

&lt;P&gt;This is my statement for this week:&lt;/P&gt;

&lt;P&gt;| dedup IDEVENT &lt;BR /&gt;
| addinfo &lt;BR /&gt;
| eval weeknumber=strftime(_time,"%U") &lt;BR /&gt;
| chart count by DESCRIPTION weeknumber &lt;BR /&gt;
| sort - 32 limit=10 &lt;BR /&gt;
| fields - 27 28 &lt;BR /&gt;
| rename 29 as "Wk29", 30 as "Wk30", 31 as "Wk31" 32 as "Wk32"&lt;/P&gt;

&lt;P&gt;Thanks a lot!&lt;/P&gt;</description>
      <pubDate>Fri, 11 Aug 2017 09:15:24 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-display-the-data-for-the-last-4-weeks-by-week-number/m-p/366471#M166437</guid>
      <dc:creator>auaave</dc:creator>
      <dc:date>2017-08-11T09:15:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to display the data for the last 4 weeks by week number?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-display-the-data-for-the-last-4-weeks-by-week-number/m-p/366472#M166438</link>
      <description>&lt;P&gt;Hello there, try something like this.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | eval week_num=case((date_mday &amp;gt; 0 AND date_mday &amp;lt;= 7), "week #1", (date_mday &amp;gt; 7 AND date_mday &amp;lt;= 14), "week #2", (date_mday &amp;gt; 14 AND date_mday &amp;lt;= 21), "week #3", date_mday &amp;gt; 21, "week #4", 1==1, null())
        | chart count by DESCRIPTION week_num
        | sort - week_num
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Hope it helps.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Aug 2017 12:57:58 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-display-the-data-for-the-last-4-weeks-by-week-number/m-p/366472#M166438</guid>
      <dc:creator>alemarzu</dc:creator>
      <dc:date>2017-08-11T12:57:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to display the data for the last 4 weeks by week number?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-display-the-data-for-the-last-4-weeks-by-week-number/m-p/366473#M166439</link>
      <description>&lt;P&gt;We put together a couple of things for you.  &lt;/P&gt;

&lt;P&gt;Here's the beginning of the search ...&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| dedup IDEVENT 
| rename COMMENT as "Get rid of events from unneeded weeks"
| where _time&amp;gt;relative_time(now(),"-4w@w")
| rename COMMENT as "Bin the events by week, then count them up"
| bin _time span=1w
| stats count as eventcount by DESCRIPTION _time
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;... for testing, the above may be replaced with this run-anywhere code ...&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| gentimes start=12/25/16 end=1/19/17 increment=7d
| eval DESCRIPTION=mvappend("DESC11","DESC12","DESC13","DESC14","DESC15", 
                            "DESC16","DESC17123","DESC18","DESC19","DESC20ABCDEF",
                            "DESC01","DESC02","DESC03","DESC04","DESC0507123", 
                            "DESC06","DESC07","DESC08","DESC09","DESC10")
| bin starttime as _time span=1w
| table _time DESCRIPTION
| mvexpand DESCRIPTION
| eval eventcount = random()%100
| stats max(eventcount) as eventcount by _time DESCRIPTION
| rename COMMENT as "Everything above this creates sample data down to the stats command."
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;...then continue like this...&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| rename COMMENT as "Figure out which DESCRIPTIONS to retain, drop the rest, prepend sort order to description"
| appendpipe 
    [| stats latest(eventcount) as sortfield latest(_time) as _time by DESCRIPTION 
     | sort - sortfield limit=10 | streamstats count as descorder | eval descorder=descorder+100 | table DESCRIPTION descorder]
| eventstats max(descorder) as descorder by DESCRIPTION
| where isnotnull(descorder)
| eval DESCRIPTION = descorder." ".DESCRIPTION
| fields - descorder

| rename COMMENT as "Figure out if we are rolling over a year here"
| appendpipe 
    [| stats min(_time) as mintime max(_time) as maxtime 
     | eval rollover=if(strftime(mintime,"%Y")!=strftime(maxtime,"%Y"),"Y","N")
     | table rollover]
| eventstats max(rollover) as rollover

| rename COMMENT as "calculate the week name.  Putting an underscore before the number will move rollover weeks to the right"
| eval weeknumber=strftime(_time,"%U")     
| eval weeknumber=case(weeknumber&amp;lt;10 AND rollover="Y","Wk_".weeknumber,  
                        true(),"Wk".weeknumber)

| rename COMMENT as "Chart the data, then pull off the sort order off the front of the description."
| chart max(eventcount) as count over DESCRIPTION by weeknumber
| eval DESCRIPTION=substr(DESCRIPTION,5,len(DESCRIPTION)-4)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;... resulting in output that looks like this...&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;DESCRIPTION   Wk51   Wk52   Wk_01  Wk_02   

DESC19          59     83      67     99  
DESC07          27     19      15     91  
DESC16          28     88      96     88  
DESC09          37     25      73     81  
DESC02          38     22      58     78  
DESC11          51     95      87     75  
DESC17123       29     61      17     73  
DESC04          36     68      80     68  
DESC18          54     58      66     66  
DESC13           1      9      69     53  
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 11 Aug 2017 18:02:06 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-display-the-data-for-the-last-4-weeks-by-week-number/m-p/366473#M166439</guid>
      <dc:creator>DalJeanis</dc:creator>
      <dc:date>2017-08-11T18:02:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to display the data for the last 4 weeks by week number?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-display-the-data-for-the-last-4-weeks-by-week-number/m-p/366474#M166440</link>
      <description>&lt;P&gt;Hi Alemarzu, thanks a lot for your reply but I can't make it work &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;

&lt;P&gt;this is my query&lt;/P&gt;

&lt;P&gt;| dedup IDEVENT &lt;BR /&gt;
| addinfo &lt;BR /&gt;
| eval week_num=case((date_mday &amp;gt; 0 AND date_mday &amp;lt;= 7), "week #1", (date_mday &amp;gt; 7 AND date_mday &amp;lt;= 14), "week #2", (date_mday &amp;gt; 14 AND date_mday &amp;lt;= 21), "week #3", date_mday &amp;gt; 21, "week #4", 1==1, null())&lt;BR /&gt;
| chart count by DESCRIPTION week_num&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 15:19:09 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-display-the-data-for-the-last-4-weeks-by-week-number/m-p/366474#M166440</guid>
      <dc:creator>auaave</dc:creator>
      <dc:date>2020-09-29T15:19:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to display the data for the last 4 weeks by week number?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-display-the-data-for-the-last-4-weeks-by-week-number/m-p/366475#M166441</link>
      <description>&lt;P&gt;Thanks DalJeanis, thank you for your reply. The above output is what I'm looking for, however I am overwhelmed by the statement. I am just a newbie and never had previous experience of writing expressions / statements. I will try this out step by step and see if I can make it work.&lt;/P&gt;</description>
      <pubDate>Sun, 13 Aug 2017 09:21:39 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-display-the-data-for-the-last-4-weeks-by-week-number/m-p/366475#M166441</guid>
      <dc:creator>auaave</dc:creator>
      <dc:date>2017-08-13T09:21:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to display the data for the last 4 weeks by week number?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-display-the-data-for-the-last-4-weeks-by-week-number/m-p/366476#M166442</link>
      <description>&lt;P&gt;Can you explaing why ? What was the output ?&lt;/P&gt;</description>
      <pubDate>Mon, 14 Aug 2017 13:09:47 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-display-the-data-for-the-last-4-weeks-by-week-number/m-p/366476#M166442</guid>
      <dc:creator>alemarzu</dc:creator>
      <dc:date>2017-08-14T13:09:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to display the data for the last 4 weeks by week number?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-display-the-data-for-the-last-4-weeks-by-week-number/m-p/366477#M166443</link>
      <description>&lt;P&gt;Absolutely!&lt;/P&gt;

&lt;P&gt;What you can do for learning purposes is, copy the first chunk of code, then add lines one at a time, and see what each one does to the output.  &lt;/P&gt;

&lt;P&gt;When doing this, and looking at the &lt;CODE&gt;appendpipe&lt;/CODE&gt; parts with a subsearch in square brackets &lt;CODE&gt;[]&lt;/CODE&gt; after it, is to remove the &lt;CODE&gt;appendpipe&lt;/CODE&gt; and just run the data into the next command inside the brackets, until you get to the end of the brackets, then put it all back and continue.  This is because &lt;CODE&gt;appendpipe&lt;/CODE&gt; processes all the records through the subsearch, and then appends them onto the end of the current set of data.&lt;/P&gt;

&lt;P&gt;So, for example, to learn this section...&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| rename COMMENT as "Figure out which DESCRIPTIONS to retain, drop the rest, prepend sort order to description"
| appendpipe 
    [| stats latest(eventcount) as sortfield latest(_time) as _time by DESCRIPTION 
     | sort - sortfield limit=10 | streamstats count as descorder | eval descorder=descorder+100 | table DESCRIPTION descorder]
 | eventstats max(descorder) as descorder by DESCRIPTION
 | where isnotnull(descorder)
 | eval DESCRIPTION = descorder." ".DESCRIPTION
 | fields - descorder
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;...you would start with this... &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| rename COMMENT as "Figure out which DESCRIPTIONS to retain, drop the rest, prepend sort order to description"
| stats latest(eventcount) as sortfield latest(_time) as _time by DESCRIPTION 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;...then add this...&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;  | sort - sortfield limit=10 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;...then this...&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| streamstats count as descorder 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;... and so on...&lt;/P&gt;</description>
      <pubDate>Mon, 14 Aug 2017 15:37:42 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-display-the-data-for-the-last-4-weeks-by-week-number/m-p/366477#M166443</guid>
      <dc:creator>DalJeanis</dc:creator>
      <dc:date>2017-08-14T15:37:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to display the data for the last 4 weeks by week number?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-display-the-data-for-the-last-4-weeks-by-week-number/m-p/366478#M166444</link>
      <description>&lt;P&gt;@DalJeanis, this is really great! I have learned a lot from this query &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Dec 2017 04:45:52 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-display-the-data-for-the-last-4-weeks-by-week-number/m-p/366478#M166444</guid>
      <dc:creator>auaave</dc:creator>
      <dc:date>2017-12-20T04:45:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to display the data for the last 4 weeks by week number?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-display-the-data-for-the-last-4-weeks-by-week-number/m-p/366479#M166445</link>
      <description>&lt;P&gt;@DalJeanis, on the below query, the "sortfield" is looking at the latest events data. However if there's no error that occurred on latest week, let's say week 7,  it takes the error data of data of week 6.&lt;/P&gt;

&lt;P&gt;Is there anyway we can avoid this?&lt;BR /&gt;
Thanks!&lt;/P&gt;

&lt;P&gt;| appendpipe &lt;BR /&gt;
    [ stats latest(eventcount) as sortfield max(_time) as _time by Location &lt;BR /&gt;
    | sort - sortfield limit=10 &lt;BR /&gt;
    | streamstats count as descorder &lt;BR /&gt;
    | eval descorder=descorder+100 &lt;BR /&gt;
    | table Location sortfield descorder]&lt;/P&gt;</description>
      <pubDate>Fri, 23 Feb 2018 04:58:58 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-display-the-data-for-the-last-4-weeks-by-week-number/m-p/366479#M166445</guid>
      <dc:creator>auaave</dc:creator>
      <dc:date>2018-02-23T04:58:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to display the data for the last 4 weeks by week number?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-display-the-data-for-the-last-4-weeks-by-week-number/m-p/366480#M166446</link>
      <description>&lt;P&gt;sure - you can kill everything that is too old.  If everything should have the same date for each week, then you would use something like... &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| appendpipe   [
    | stats latest(eventcount) as sortfield max(_time) as _time by Location 
    | eventstats max(_time) as maxtime
    | where _time = maxtime
    | sort - sortfield limit=10 
    | streamstats count as descorder 
    | eval descorder=descorder+100 
    | table Location sortfield descorder]
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;On the other hand, if the _time figures are daily, and  "the last week" is anything with the last 7 days dates, then replace this line... &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;    | where _time = maxtime
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;...with this line...&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;    | where _time &amp;gt; maxtime - 604800
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Note - 604800 is the number of seconds in a week , 7*24*3600.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 18:10:43 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-display-the-data-for-the-last-4-weeks-by-week-number/m-p/366480#M166446</guid>
      <dc:creator>DalJeanis</dc:creator>
      <dc:date>2020-09-29T18:10:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to display the data for the last 4 weeks by week number?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-display-the-data-for-the-last-4-weeks-by-week-number/m-p/366481#M166447</link>
      <description>&lt;P&gt;@DalJeanis, the first one did the trick! You are the best! Thanks a lot for helping me. &lt;/P&gt;</description>
      <pubDate>Thu, 08 Mar 2018 04:12:01 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-display-the-data-for-the-last-4-weeks-by-week-number/m-p/366481#M166447</guid>
      <dc:creator>auaave</dc:creator>
      <dc:date>2018-03-08T04:12:01Z</dc:date>
    </item>
  </channel>
</rss>

