<?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 add a new column to existing stats result after performing a calculation on each value in a column ? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-add-a-new-column-to-existing-stats-result-after/m-p/317052#M164683</link>
    <description>&lt;P&gt;Trying is important, community members are always there otherwise &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 01 Dec 2017 15:09:36 GMT</pubDate>
    <dc:creator>niketn</dc:creator>
    <dc:date>2017-12-01T15:09:36Z</dc:date>
    <item>
      <title>How to add a new column to existing stats result after performing a calculation on each value in a column ?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-add-a-new-column-to-existing-stats-result-after/m-p/317049#M164680</link>
      <description>&lt;P&gt;Hi&lt;BR /&gt;
I am new to splunk and still exploring it.&lt;BR /&gt;
How do i create a new result set after performing some calculation on existing stats output ?&lt;/P&gt;

&lt;P&gt;More details here:&lt;BR /&gt;
There can be multiple stores and each store can create multiple deals.&lt;/P&gt;

&lt;P&gt;I was able to get total deals per store id using this query&lt;BR /&gt;
index=fosign &lt;BR /&gt;
env="test" &lt;BR /&gt;
Level="Information" &lt;BR /&gt;
Properties.DealJacketId=* &lt;BR /&gt;
Properties.StoreId=*&lt;BR /&gt;
Properties.LogSource="Create.NewDeal.Handler"&lt;BR /&gt;
| stats count(Properties.DealJacketId) as "total_deals (In selected time period)" by Properties.StoreId&lt;/P&gt;

&lt;P&gt;but I am finding it difficult to produce average deals per day, dynamically based on selected time frame.&lt;BR /&gt;
Note: User can select multiple time frames and this needs to work for all time frames selected.&lt;/P&gt;

&lt;HR /&gt;

&lt;H2&gt;StoreId  |  total_deals (In selected time period)  |  average_deals_per_day (includes weekend) ==&amp;gt; "need help for this column"&lt;/H2&gt;

&lt;P&gt;S1234                                              100                                                                               12&lt;BR /&gt;
S1234                                              200                                                                               15&lt;BR /&gt;
.&lt;BR /&gt;
.&lt;BR /&gt;
.&lt;/P&gt;

&lt;P&gt;Sample log: &lt;BR /&gt;
{&lt;BR /&gt;&lt;BR /&gt;
     Level:  Information&lt;BR /&gt;&lt;BR /&gt;
     MessageTemplate: Deal created successfully for store: {storeId}, deal id: {DealJacketId}, DealNumber: {DealNumber}&lt;BR /&gt;
     Properties:    {&lt;BR /&gt;&lt;BR /&gt;
               CorrelationId:    No Correlation Id Provided &lt;BR /&gt;
               DealJacketId:             MTc1ODY2MDAwMDAwMDAwMDAyfDcyMjN8bmp3OUVZQkZ6Sw==&lt;BR /&gt;&lt;BR /&gt;
               DealNumber:           7223&lt;BR /&gt;&lt;BR /&gt;
               LogSource:            Create.NewDeal.Handler &lt;BR /&gt;
               SourceContext:    CreatingNewDeal&lt;BR /&gt;&lt;BR /&gt;
    }&lt;BR /&gt;&lt;BR /&gt;
     Timestamp:  2017-12-01T09:20:08.7158876+00:00&lt;BR /&gt;&lt;BR /&gt;
}&lt;/P&gt;

&lt;P&gt;Any help is appreciated. Thank you.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 17:02:49 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-add-a-new-column-to-existing-stats-result-after/m-p/317049#M164680</guid>
      <dc:creator>UdayAditya</dc:creator>
      <dc:date>2020-09-29T17:02:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to add a new column to existing stats result after performing a calculation on each value in a column ?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-add-a-new-column-to-existing-stats-result-after/m-p/317050#M164681</link>
      <description>&lt;P&gt;@UdayAditya, following is a run anywhere search based on Splunk's _internal index which gives a daily average of errors as well as total for selected time period:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=_internal sourcetype=splunkd log_level!=INFO
|  bin span=1d _time
|  stats count as Total by log_level, _time
|  stats avg(Total) as Avg sum(Total) as Total by log_level
|  eval Avg=round(Avg,1)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;You can try the following with your search:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; &amp;lt;YourBaseSearch&amp;gt;
| bin span=1d _time
| stats count(Properties.DealJacketId) as total_deals by Properties.StoreId, _time
| stats sum(total_deals) as "total_deals (In selected time period)" avg(total_deals) as "avg_deals_daily" by Properties.StoreId
| eval avg_deals_daily=round(avg_deals_daily,1)
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 01 Dec 2017 10:38:40 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-add-a-new-column-to-existing-stats-result-after/m-p/317050#M164681</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2017-12-01T10:38:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to add a new column to existing stats result after performing a calculation on each value in a column ?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-add-a-new-column-to-existing-stats-result-after/m-p/317051#M164682</link>
      <description>&lt;P&gt;@niketnilay&lt;BR /&gt;
Thank you, i tried multiple things and looked like i added unnecessary complexity.&lt;BR /&gt;
This solution looks simple and clear.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2017 10:48:43 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-add-a-new-column-to-existing-stats-result-after/m-p/317051#M164682</guid>
      <dc:creator>UdayAditya</dc:creator>
      <dc:date>2017-12-01T10:48:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to add a new column to existing stats result after performing a calculation on each value in a column ?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-add-a-new-column-to-existing-stats-result-after/m-p/317052#M164683</link>
      <description>&lt;P&gt;Trying is important, community members are always there otherwise &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2017 15:09:36 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-add-a-new-column-to-existing-stats-result-after/m-p/317052#M164683</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2017-12-01T15:09:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to add a new column to existing stats result after performing a calculation on each value in a column ?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-add-a-new-column-to-existing-stats-result-after/m-p/317053#M164684</link>
      <description>&lt;P&gt;@niketnilay&lt;BR /&gt;
if i understood correctly. "span=1d _time" will try placing all events in buckets corresponding to each day.&lt;/P&gt;

&lt;P&gt;While aggregating values, I realized Splunk ignores buckets without any events in it.&lt;BR /&gt;
In that case the average calculated can easily become invalid if there are no events falling into particular day's bucket. &lt;/P&gt;

&lt;P&gt;ex: &lt;BR /&gt;
Selected time range = 5 days, total events = 10 then&lt;BR /&gt;
I expect the daily average to be 10/5 = 2.&lt;/P&gt;

&lt;P&gt;day1 bucket = 3&lt;BR /&gt;
day2 bucket = 3&lt;BR /&gt;
day3 bucket = 0&lt;BR /&gt;
day4 bucket = 4&lt;BR /&gt;
day5 bucket = 0&lt;/P&gt;

&lt;P&gt;then i see that Splunk calculates average as (3+3+4)/3 ~ 3.33 which is not desired for me. &lt;/P&gt;

&lt;P&gt;Any suggestion to how can i achieve the correct average using splunk ?&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2017 17:23:15 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-add-a-new-column-to-existing-stats-result-after/m-p/317053#M164684</guid>
      <dc:creator>UdayAditya</dc:creator>
      <dc:date>2017-12-01T17:23:15Z</dc:date>
    </item>
    <item>
      <title>Re: How to add a new column to existing stats result after performing a calculation on each value in a column ?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-add-a-new-column-to-existing-stats-result-after/m-p/317054#M164685</link>
      <description>&lt;P&gt;Yes your understanding of &lt;CODE&gt;bin&lt;/CODE&gt; command is correct. Refer to documentation: &lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Bin"&gt;https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Bin&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;You would have to switch from stats to timechart. The &lt;CODE&gt;timechart command with fillnull&lt;/CODE&gt; will fill empty rows with 0 so that your average shows up correctly.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;  &amp;lt;YourBaseSearch&amp;gt;
 | timechart span=1d count(Properties.DealJacketId) as total_deals by Properties.StoreId
 | fillnull value=0 &amp;lt;yourStoreID1&amp;gt;,&amp;lt;yourStoreID2&amp;gt;...
 | stats sum(total_deals) as "total_deals (In selected time period)" avg(total_deals) as "avg_deals_daily" by Properties.StoreId
 | eval avg_deals_daily=round(avg_deals_daily,1)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;PS: &lt;CODE&gt;span=1d&lt;/CODE&gt; argument in timechart command has same effect as bin command in stats. If you remove &lt;CODE&gt;span=1d&lt;/CODE&gt;, Splunk will adjust on its own the span buckets. For example, for &lt;CODE&gt;7 days&lt;/CODE&gt; it will be &lt;CODE&gt;span=1d&lt;/CODE&gt; and for &lt;CODE&gt;24 hours&lt;/CODE&gt; it will be &lt;CODE&gt;span=1h&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;Please try out and confirm.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Dec 2017 03:07:20 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-add-a-new-column-to-existing-stats-result-after/m-p/317054#M164685</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2017-12-12T03:07:20Z</dc:date>
    </item>
  </channel>
</rss>

