<?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 search count by only business days? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-search-count-by-only-business-days/m-p/419035#M120467</link>
    <description>&lt;P&gt;@lewisgrantevans,&lt;/P&gt;

&lt;P&gt;I've tried to implement &lt;A href="https://stackoverflow.com/questions/4600034/calculate-number-of-weekdays-between-two-dates-in-java/44942039#answer-44942039"&gt;Ronald's second solution&lt;/A&gt;. Please try.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval Created2=floor(relative_time(strptime(Created,"%d/%m/%Y %H:%M"),"@d"))
| eval today = floor(relative_time(now(),"@d"))
| eval createdW = strftime(Created2,"%w")
| convert num(createdW)
| eval createdW = if(createdW == 0, 7, createdW)
| eval todayW = strftime(today,"%w")
| convert num(todayW)
| eval todayW = if(todayW == 0, 7, todayW)
| eval totalDays = (today-Created2)/86400
| eval Aging = totalDays - 2 * floor((totalDays/7))
| eval Aging = if(totalDays % 7 != 0, case(createdW = 7, Aging-1, todayW = 7, Aging-1, todayW &amp;lt; createdW, Aging-2, 1=1, Aging), Aging)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Hope this helps!!!!&lt;/P&gt;</description>
    <pubDate>Wed, 12 Jun 2019 08:42:40 GMT</pubDate>
    <dc:creator>VatsalJagani</dc:creator>
    <dc:date>2019-06-12T08:42:40Z</dc:date>
    <item>
      <title>How to search count by only business days?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-search-count-by-only-business-days/m-p/419032#M120464</link>
      <description>&lt;P&gt;Hello Splunk Ninjas,&lt;/P&gt;

&lt;P&gt;I have created an 'aging' field that counts the number of days since a certain date &amp;amp; time. I would like to remove weekends from this count.&lt;/P&gt;

&lt;P&gt;I am using:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval Created=strptime(Created,"%d/%m/%Y %H:%M")
| eval c_time=strftime(Created,"%d/%m/%Y %H:%M")
| eval aging=round((now()-strptime(c_time, "%d/%m/%Y %H:%M"))/86400,2)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Many thanks in advance.&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jun 2019 13:21:45 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-search-count-by-only-business-days/m-p/419032#M120464</guid>
      <dc:creator>lewisgrantevans</dc:creator>
      <dc:date>2019-06-11T13:21:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to search count by only business days?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-search-count-by-only-business-days/m-p/419033#M120465</link>
      <description>&lt;P&gt;You could do something like this in the beginning of your search:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;[BASE SEARCH HERE]
| eval daytype=if(date_wday in ("monday","tuesday","wednesday","thursday","friday"), "busday","weekend") 
| where daytype="busday"
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 11 Jun 2019 15:29:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-search-count-by-only-business-days/m-p/419033#M120465</guid>
      <dc:creator>kmorris_splunk</dc:creator>
      <dc:date>2019-06-11T15:29:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to search count by only business days?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-search-count-by-only-business-days/m-p/419034#M120466</link>
      <description>&lt;P&gt;Thanks guys, but i should have made it clear that i'm only using a .csv file for this query so i don't have any time fields except for the one i've created below -&lt;/P&gt;

&lt;P&gt;| eval Created=strptime(Created,"%d/%m/%Y %H:%M")&lt;BR /&gt;
| eval c_time=strftime(Created,"%d/%m/%Y %H:%M")&lt;BR /&gt;
| eval aging=round((now()-strptime(c_time, "%d/%m/%Y %H:%M"))/86400,2)&lt;/P&gt;

&lt;P&gt;Cheers,&lt;BR /&gt;
Lewis&lt;/P&gt;</description>
      <pubDate>Wed, 30 Sep 2020 00:53:26 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-search-count-by-only-business-days/m-p/419034#M120466</guid>
      <dc:creator>lewisgrantevans</dc:creator>
      <dc:date>2020-09-30T00:53:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to search count by only business days?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-search-count-by-only-business-days/m-p/419035#M120467</link>
      <description>&lt;P&gt;@lewisgrantevans,&lt;/P&gt;

&lt;P&gt;I've tried to implement &lt;A href="https://stackoverflow.com/questions/4600034/calculate-number-of-weekdays-between-two-dates-in-java/44942039#answer-44942039"&gt;Ronald's second solution&lt;/A&gt;. Please try.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval Created2=floor(relative_time(strptime(Created,"%d/%m/%Y %H:%M"),"@d"))
| eval today = floor(relative_time(now(),"@d"))
| eval createdW = strftime(Created2,"%w")
| convert num(createdW)
| eval createdW = if(createdW == 0, 7, createdW)
| eval todayW = strftime(today,"%w")
| convert num(todayW)
| eval todayW = if(todayW == 0, 7, todayW)
| eval totalDays = (today-Created2)/86400
| eval Aging = totalDays - 2 * floor((totalDays/7))
| eval Aging = if(totalDays % 7 != 0, case(createdW = 7, Aging-1, todayW = 7, Aging-1, todayW &amp;lt; createdW, Aging-2, 1=1, Aging), Aging)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Hope this helps!!!!&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jun 2019 08:42:40 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-search-count-by-only-business-days/m-p/419035#M120467</guid>
      <dc:creator>VatsalJagani</dc:creator>
      <dc:date>2019-06-12T08:42:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to search count by only business days?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-search-count-by-only-business-days/m-p/419036#M120468</link>
      <description>&lt;P&gt;The solutions referring to date_wday are indeed not of any use to you. But the answer from @VatsalJagani should work, I think.&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jun 2019 12:56:10 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-search-count-by-only-business-days/m-p/419036#M120468</guid>
      <dc:creator>FrankVl</dc:creator>
      <dc:date>2019-06-12T12:56:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to search count by only business days?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-search-count-by-only-business-days/m-p/419037#M120469</link>
      <description>&lt;P&gt;Hi @FrankVl &amp;amp; @VatsalJagani , I think we're close but it's not quite calculating correctly. I've attached a screenshot of my results in excel.&lt;/P&gt;

&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="alt text"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/7185i49064B6FDBC635F2/image-size/large?v=v2&amp;amp;px=999" role="button" title="alt text" alt="alt text" /&gt;&lt;/span&gt;&lt;/P&gt;

&lt;P&gt;I used almost the exact query (I just changed the eval to make a Created2 field so i could retain the Created field).&lt;/P&gt;

&lt;P&gt;The results for June 4th &amp;amp; 5th are more accurate than the excel answer, taking the time into consideration, however the results for June 6th, 7th &amp;amp; 10th are not accurate. You'll notice a jump in noOfBusinessDays between the 5th &amp;amp; 6th.&lt;/P&gt;

&lt;P&gt;| eval Created2=strptime(Created,"%d/%m/%Y %H:%M") &lt;BR /&gt;
 | eval today = now()&lt;BR /&gt;
 | eval createdW = strftime(Created2,"%w")&lt;BR /&gt;
 | convert num(createdW)&lt;BR /&gt;
 | eval createdW = if(createdW == 0, 7, createdW)&lt;BR /&gt;
 | eval todayW = strftime(today,"%w")&lt;BR /&gt;
 | convert num(todayW)&lt;BR /&gt;
 | eval todayW = if(todayW == 0, 7, todayW)&lt;BR /&gt;
 | eval totalDays = round(((today-Created2)/(86400)),2) &lt;BR /&gt;
 | eval noOfBusinessDays = totalDays - 2 * (totalDays/7)&lt;BR /&gt;
 | eval noOfBusinessDays = if(totalDays % 7 != 0, case(createdW = 7, noOfBusinessDays-1, todayW = 7, noOfBusinessDays-1,  todayW &amp;lt; createdW, noOfBusinessDays-2, 1=1, noOfBusinessDays), noOfBusinessDays)&lt;/P&gt;

&lt;P&gt;Thanks again!&lt;BR /&gt;
Lewis&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jun 2019 13:27:10 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-search-count-by-only-business-days/m-p/419037#M120469</guid>
      <dc:creator>lewisgrantevans</dc:creator>
      <dc:date>2019-06-12T13:27:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to search count by only business days?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-search-count-by-only-business-days/m-p/419038#M120470</link>
      <description>&lt;P&gt;Try adding a floor to the second to last line: &lt;CODE&gt;| eval noOfBusinessDays = totalDays - 2 * floor((totalDays/7))&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;You don't want to take the decimal part of &lt;CODE&gt;totalDays/7&lt;/CODE&gt; into account.&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jun 2019 15:13:39 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-search-count-by-only-business-days/m-p/419038#M120470</guid>
      <dc:creator>FrankVl</dc:creator>
      <dc:date>2019-06-12T15:13:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to search count by only business days?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-search-count-by-only-business-days/m-p/419039#M120471</link>
      <description>&lt;P&gt;Thank you @FrankVl &amp;amp; @VatsalJagani !&lt;/P&gt;

&lt;P&gt;This works perfectly when taking @FrankVl last comment into consideration&lt;/P&gt;

&lt;P&gt;"Try adding a floor to the second to last line: | eval noOfBusinessDays = totalDays - 2 * floor((totalDays/7))&lt;/P&gt;

&lt;P&gt;You don't want to take the decimal part of totalDays/7 into account."&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jun 2019 15:23:18 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-search-count-by-only-business-days/m-p/419039#M120471</guid>
      <dc:creator>lewisgrantevans</dc:creator>
      <dc:date>2019-06-12T15:23:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to search count by only business days?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-search-count-by-only-business-days/m-p/419040#M120472</link>
      <description>&lt;P&gt;Also: it seems to be missing logic for dealing with createdW=6 or todayW=6, right?&lt;/P&gt;

&lt;P&gt;Ah, the stackoverflow solution that was referred to uses a function that calculates the 'days between'.   And round(((today-Created2)/(86400)),2)  is not the same as days between. Same for excel, that does not do a days between but adds 1.&lt;/P&gt;

&lt;P&gt;I think you also need to replace the round with a floor: &lt;CODE&gt;| eval totalDays = floor(((today-Created2)/86400))&lt;/CODE&gt;.&lt;/P&gt;

&lt;P&gt;Alternative would be to subtract 2 for sundays and subtract 1 for saturdays in the case statement.&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jun 2019 15:32:25 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-search-count-by-only-business-days/m-p/419040#M120472</guid>
      <dc:creator>FrankVl</dc:creator>
      <dc:date>2019-06-12T15:32:25Z</dc:date>
    </item>
    <item>
      <title>Re: How to search count by only business days?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-search-count-by-only-business-days/m-p/419041#M120473</link>
      <description>&lt;P&gt;Careful it seems to be missing logic for dealing with createdW=6 or todayW=6, right?&lt;/P&gt;

&lt;P&gt;The stackoverflow solution that was referred to uses a function that calculates the 'days between'. And round(((today-Created2)/(86400)),2) is not the same as days between. Same for excel, that does not do a days between but adds 1.&lt;/P&gt;

&lt;P&gt;I think you also need to replace the round with a floor: | eval totalDays = floor(((today-Created2)/86400)) . (but then you do have an off-by-one compared to Excel, depends a bit on what you want. Is today-today equal to 0 or to 1?)&lt;/P&gt;

&lt;P&gt;Alternative would be to subtract 2 for sundays and subtract 1 for saturdays in the case statement.&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jun 2019 15:46:26 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-search-count-by-only-business-days/m-p/419041#M120473</guid>
      <dc:creator>FrankVl</dc:creator>
      <dc:date>2019-06-12T15:46:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to search count by only business days?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-search-count-by-only-business-days/m-p/419042#M120474</link>
      <description>&lt;P&gt;Looking a bit closer, I think the issue might actually be with the &lt;CODE&gt;totalDays % 7 != 0&lt;/CODE&gt;. totalDays % 7 is practically never 0 except when the timestamps are identical. You need a floor around that.&lt;/P&gt;

&lt;P&gt;So the last 2 lines should be:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval noOfBusinessDays = totalDays - 2 * floor((totalDays/7))
| eval noOfBusinessDays = if(floor(totalDays % 7) != 0, case(createdW = 7, noOfBusinessDays-1, todayW = 7, noOfBusinessDays-1, todayW &amp;lt; createdW, noOfBusinessDays-2, 1=1, noOfBusinessDays), noOfBusinessDays)
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 12 Jun 2019 16:02:31 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-search-count-by-only-business-days/m-p/419042#M120474</guid>
      <dc:creator>FrankVl</dc:creator>
      <dc:date>2019-06-12T16:02:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to search count by only business days?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-search-count-by-only-business-days/m-p/419043#M120475</link>
      <description>&lt;P&gt;Yes i was a little hasty, i noticed a miscalculation on June 5th but your most recent comment seems to have rectified it. I'm now using -&lt;/P&gt;

&lt;P&gt;| eval Created2=strptime(Created,"%d/%m/%Y %H:%M") &lt;BR /&gt;
| eval today = now()&lt;BR /&gt;
| eval createdW = strftime(Created2,"%w")&lt;BR /&gt;
| convert num(createdW)&lt;BR /&gt;
| eval createdW = if(createdW == 0, 7, createdW)&lt;BR /&gt;
| eval todayW = strftime(today,"%w")&lt;BR /&gt;
| convert num(todayW)&lt;BR /&gt;
| eval todayW = if(todayW == 0, 7, todayW)&lt;BR /&gt;
| eval totalDays = floor(((today-Created2)/86400))&lt;BR /&gt;
| eval Aging = totalDays - 2 * floor((totalDays/7))&lt;BR /&gt;
| eval Aging = if(totalDays % 7 != 0, case(createdW = 7, Aging-1, todayW = 7, Aging-1,  todayW &amp;lt; createdW, Aging-2, 1=1, Aging), Aging)&lt;/P&gt;

&lt;P&gt;Cheers buddy&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jun 2019 16:09:02 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-search-count-by-only-business-days/m-p/419043#M120475</guid>
      <dc:creator>lewisgrantevans</dc:creator>
      <dc:date>2019-06-12T16:09:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to search count by only business days?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-search-count-by-only-business-days/m-p/419044#M120476</link>
      <description>&lt;P&gt;The way the totalDays is calculated is just really tricky. It might be much better to simply reduce each date to the start of that day first. That way, if the two dates are the same day of week, the totalDays is 0 or a multiple of 7. Using floor does not solve that if now() is earlier in the day than Created2.&lt;/P&gt;

&lt;P&gt;So I would suggest:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval Created2=floor(relative_time(strptime(Created,"%d/%m/%Y %H:%M"),"@d"))
| eval today = floor(relative_time(now(),"@d"))
| eval createdW = strftime(Created2,"%w")
| convert num(createdW)
| eval createdW = if(createdW == 0, 7, createdW)
| eval todayW = strftime(today,"%w")
| convert num(todayW)
| eval todayW = if(todayW == 0, 7, todayW)
| eval totalDays = (today-Created2)/86400
| eval Aging = totalDays - 2 * floor((totalDays/7))
| eval Aging = if(totalDays % 7 != 0, case(createdW = 7, Aging-1, todayW = 7, Aging-1, todayW &amp;lt; createdW, Aging-2, 1=1, Aging), Aging)
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 12 Jun 2019 17:16:14 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-search-count-by-only-business-days/m-p/419044#M120476</guid>
      <dc:creator>FrankVl</dc:creator>
      <dc:date>2019-06-12T17:16:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to search count by only business days?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-search-count-by-only-business-days/m-p/419045#M120477</link>
      <description>&lt;P&gt;Thanks @FrankVl for correction in logic. Thanks @lewisgrantevans now I've updated my answer so other user also can easily find the solution.&lt;/P&gt;

&lt;P&gt;I think this is very common day to day life logic problem, isn't it?&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jun 2019 06:03:07 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-search-count-by-only-business-days/m-p/419045#M120477</guid>
      <dc:creator>VatsalJagani</dc:creator>
      <dc:date>2019-06-13T06:03:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to search count by only business days?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-search-count-by-only-business-days/m-p/419046#M120478</link>
      <description>&lt;P&gt;Amazing, thank you guys. It's now more accurate than the excel calculation. Much appreciated!&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jun 2019 08:09:05 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-search-count-by-only-business-days/m-p/419046#M120478</guid>
      <dc:creator>lewisgrantevans</dc:creator>
      <dc:date>2019-06-13T08:09:05Z</dc:date>
    </item>
  </channel>
</rss>

