<?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 calculate  the average of a field value for n number of days? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-average-of-a-field-value-for-n-number-of/m-p/317573#M94993</link>
    <description>&lt;P&gt;So, considering your sample data of &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;time h1 h2 h3 h4 h5 h6 h7 total
2017-11-24 2334 68125 86384 120811 0 28020 0 305674
2017-11-25 5580 130912 172614 199817 0 38812 0 547735
2017-11-26 9788 308490 372618 474212 0 112607 0 1277715
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The output should be (single row, one column for each host)&lt;/P&gt;

&lt;P&gt;h1=(2334 +5580+9788 )/3 (which is number days h1 had data) , OR&lt;BR /&gt;
h1=(2334 +5580+9788 )/7 (which is the total number of hosts) , OR something else?? &lt;/P&gt;</description>
    <pubDate>Fri, 01 Dec 2017 16:36:29 GMT</pubDate>
    <dc:creator>somesoni2</dc:creator>
    <dc:date>2017-12-01T16:36:29Z</dc:date>
    <item>
      <title>How to calculate  the average of a field value for n number of days?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-average-of-a-field-value-for-n-number-of/m-p/317566#M94986</link>
      <description>&lt;P&gt;I have the below query&lt;/P&gt;

&lt;P&gt;index=abc sourcetype=xy.. |timechart span=1d count as events by host | addtotals&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;time           h1     h2        h3       h4             h5         h6       h7     total
2017-11-24  2334    68125   86384   120811         0         28020  0      305674
2017-11-25  5580    130912  172614  199817      0         38812 0      547735
2017-11-26  9788    308490  372618  4742120     0        112607   0    1277715
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;How do I calculate the average of h1 for the last 7 days  and similarly for all other hosts  &lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2017 15:51:35 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-average-of-a-field-value-for-n-number-of/m-p/317566#M94986</guid>
      <dc:creator>vrmandadi</dc:creator>
      <dc:date>2017-12-01T15:51:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate  the average of a field value for n number of days?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-average-of-a-field-value-for-n-number-of/m-p/317567#M94987</link>
      <description>&lt;P&gt;For that day OR just average for whole time range?&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2017 15:57:45 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-average-of-a-field-value-for-n-number-of/m-p/317567#M94987</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2017-12-01T15:57:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate  the average of a field value for n number of days?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-average-of-a-field-value-for-n-number-of/m-p/317568#M94988</link>
      <description>&lt;P&gt;sorry I am calculating for last 7 days..so last 7 days count of a particular host by the total&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2017 16:03:43 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-average-of-a-field-value-for-n-number-of/m-p/317568#M94988</guid>
      <dc:creator>vrmandadi</dc:creator>
      <dc:date>2017-12-01T16:03:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate  the average of a field value for n number of days?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-average-of-a-field-value-for-n-number-of/m-p/317569#M94989</link>
      <description>&lt;P&gt;your search returns the total number of events of h1 on each of 7 days.&lt;BR /&gt;
The 'average of h1 for that day' makes no sense (because it will be the same) as the total.&lt;/P&gt;

&lt;P&gt;Do you mean the avg number of events for h1 in the last week?&lt;BR /&gt;
(and h2, h3 etc)&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2017 16:22:02 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-average-of-a-field-value-for-n-number-of/m-p/317569#M94989</guid>
      <dc:creator>nickhills</dc:creator>
      <dc:date>2017-12-01T16:22:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate  the average of a field value for n number of days?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-average-of-a-field-value-for-n-number-of/m-p/317570#M94990</link>
      <description>&lt;P&gt;It's still not very clear. Could you post the expected output per your sample data in question. Give this a try as well.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=abc sourcetype=xy.. |timechart span=1d count as events by host | addtotals
| stats avg(*) as *
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 01 Dec 2017 16:25:10 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-average-of-a-field-value-for-n-number-of/m-p/317570#M94990</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2017-12-01T16:25:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate  the average of a field value for n number of days?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-average-of-a-field-value-for-n-number-of/m-p/317571#M94991</link>
      <description>&lt;P&gt;I am trying to find out the average of each host&lt;/P&gt;

&lt;P&gt;average=count of h1 for 7 days /total count of all hosts and similarly for all hosts&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2017 16:25:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-average-of-a-field-value-for-n-number-of/m-p/317571#M94991</guid>
      <dc:creator>vrmandadi</dc:creator>
      <dc:date>2017-12-01T16:25:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate  the average of a field value for n number of days?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-average-of-a-field-value-for-n-number-of/m-p/317572#M94992</link>
      <description>&lt;P&gt;ok for example H1 has 25 count each for last 4 days out of 100 events each day.Then the average is calculated &lt;/P&gt;

&lt;P&gt;100-total events of h1 for last 4 days&lt;BR /&gt;
400-total events &lt;/P&gt;

&lt;P&gt;average of h1=100/4 =25&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2017 16:30:30 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-average-of-a-field-value-for-n-number-of/m-p/317572#M94992</guid>
      <dc:creator>vrmandadi</dc:creator>
      <dc:date>2017-12-01T16:30:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate  the average of a field value for n number of days?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-average-of-a-field-value-for-n-number-of/m-p/317573#M94993</link>
      <description>&lt;P&gt;So, considering your sample data of &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;time h1 h2 h3 h4 h5 h6 h7 total
2017-11-24 2334 68125 86384 120811 0 28020 0 305674
2017-11-25 5580 130912 172614 199817 0 38812 0 547735
2017-11-26 9788 308490 372618 474212 0 112607 0 1277715
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The output should be (single row, one column for each host)&lt;/P&gt;

&lt;P&gt;h1=(2334 +5580+9788 )/3 (which is number days h1 had data) , OR&lt;BR /&gt;
h1=(2334 +5580+9788 )/7 (which is the total number of hosts) , OR something else?? &lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2017 16:36:29 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-average-of-a-field-value-for-n-number-of/m-p/317573#M94993</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2017-12-01T16:36:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate  the average of a field value for n number of days?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-average-of-a-field-value-for-n-number-of/m-p/317574#M94994</link>
      <description>&lt;P&gt;So you want a results table which would would look like this: ?&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;             h1     h2       h3    h4   total      
Day1    25      33       33     9      100
Day2    25      30       36     9      100
Day3    25      36       9     33      100
Day4    25      33       33     9      100
 AVG     25     33       27.5    15
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 01 Dec 2017 16:38:05 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-average-of-a-field-value-for-n-number-of/m-p/317574#M94994</guid>
      <dc:creator>nickhills</dc:creator>
      <dc:date>2017-12-01T16:38:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate  the average of a field value for n number of days?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-average-of-a-field-value-for-n-number-of/m-p/317575#M94995</link>
      <description>&lt;P&gt;meh, formatting,, you get the point&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2017 16:38:30 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-average-of-a-field-value-for-n-number-of/m-p/317575#M94995</guid>
      <dc:creator>nickhills</dc:creator>
      <dc:date>2017-12-01T16:38:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate  the average of a field value for n number of days?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-average-of-a-field-value-for-n-number-of/m-p/317576#M94996</link>
      <description>&lt;P&gt;h1=(2334 +5580+9788 )/3 (which is number days h1 had data) ..This is what I am looking for&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2017 16:40:27 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-average-of-a-field-value-for-n-number-of/m-p/317576#M94996</guid>
      <dc:creator>vrmandadi</dc:creator>
      <dc:date>2017-12-01T16:40:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate  the average of a field value for n number of days?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-average-of-a-field-value-for-n-number-of/m-p/317577#M94997</link>
      <description>&lt;P&gt;or.. do you just want the last row? - ie just the averages?&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2017 16:40:29 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-average-of-a-field-value-for-n-number-of/m-p/317577#M94997</guid>
      <dc:creator>nickhills</dc:creator>
      <dc:date>2017-12-01T16:40:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate  the average of a field value for n number of days?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-average-of-a-field-value-for-n-number-of/m-p/317578#M94998</link>
      <description>&lt;P&gt;Ya correct I want the same thing like the table you showed with totals and the last row with avg&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2017 16:45:25 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-average-of-a-field-value-for-n-number-of/m-p/317578#M94998</guid>
      <dc:creator>vrmandadi</dc:creator>
      <dc:date>2017-12-01T16:45:25Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate  the average of a field value for n number of days?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-average-of-a-field-value-for-n-number-of/m-p/317579#M94999</link>
      <description>&lt;P&gt;Would this give not you what you need?&lt;BR /&gt;
    index=abc sourcetype=xy..  |stats avg(host) by host&lt;BR /&gt;
run over 7 days&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2017 16:45:29 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-average-of-a-field-value-for-n-number-of/m-p/317579#M94999</guid>
      <dc:creator>nickhills</dc:creator>
      <dc:date>2017-12-01T16:45:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate  the average of a field value for n number of days?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-average-of-a-field-value-for-n-number-of/m-p/317580#M95000</link>
      <description>&lt;P&gt;nope,I tried that earlier and it does not give you the _time field also&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2017 16:48:30 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-average-of-a-field-value-for-n-number-of/m-p/317580#M95000</guid>
      <dc:creator>vrmandadi</dc:creator>
      <dc:date>2017-12-01T16:48:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate  the average of a field value for n number of days?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-average-of-a-field-value-for-n-number-of/m-p/317581#M95001</link>
      <description>&lt;P&gt;actually thats total rubbish - it wont even work. (commenting on my phone)&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2017 16:50:05 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-average-of-a-field-value-for-n-number-of/m-p/317581#M95001</guid>
      <dc:creator>nickhills</dc:creator>
      <dc:date>2017-12-01T16:50:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate  the average of a field value for n number of days?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-average-of-a-field-value-for-n-number-of/m-p/317582#M95002</link>
      <description>&lt;P&gt;Are you looking to calculate the average from daily counts, or from the sum of 7 days worth?  This is the confusing part.   If it's the former, are you looking to do this over time, i.e. see the average every 7 days, or just a single 7 day period?    I'll tackle the first scenario - calculate the average count of events, per host, per day, over a period of 7 days.&lt;/P&gt;

&lt;P&gt;In any case, timechart can't really do this in one step - so you'll need to bucket/bin the events first, then use a couple of stats commands..   This will group events by day, then create a count of events per host, per day.   The second stats will then calculate the average daily count per host over whatever time period you search (the assumption is 7 days)  The eval is just to round the average down to 2 decimal places.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;yoursearchhere 
| bin span=1d _time
| stats count as dailycount by host, _time
| stats avg(dailycount) as average_count_per_day by host
| eval average_count_per_day=round(average_count_per_day,2)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Calculating this average every 7 days gets a bit more complicated - I believe eventstats would be a better solution in that case.&lt;/P&gt;

&lt;P&gt;If you're looking for an average total events for 7 days, this would change a bit, and you would need to search for more than 7 days obviously - otherwise the total is the same as the average as &lt;BR /&gt;
 nickhillscpl mentioned.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;yoursearchhere  
| bin span=7d _time 
| stats count as 7daycount by host, _time   
| stats avg(7daycount) as average_events_per_7days by host
| eval average_events_per_7days=round(average_events_per_7days,2)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Hope this helps.&lt;/P&gt;

&lt;P&gt;EDIT:  I see from the comments you are looking to retain the formatting you have in the table..   THis should do the trick:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;   yoursearch
  | timechart span=1d count by host
  | addtotals
  | appendpipe [stats avg(*) as *]
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 01 Dec 2017 16:59:55 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-average-of-a-field-value-for-n-number-of/m-p/317582#M95002</guid>
      <dc:creator>c_boggs</dc:creator>
      <dc:date>2017-12-01T16:59:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate  the average of a field value for n number of days?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-average-of-a-field-value-for-n-number-of/m-p/317583#M95003</link>
      <description>&lt;P&gt;Did you get a chance to test this query?&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2017 17:12:29 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-average-of-a-field-value-for-n-number-of/m-p/317583#M95003</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2017-12-01T17:12:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate  the average of a field value for n number of days?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-average-of-a-field-value-for-n-number-of/m-p/317584#M95004</link>
      <description>&lt;P&gt;yup it gives you two columns one with host names and the other avg(host) with no values&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2017 17:14:09 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-average-of-a-field-value-for-n-number-of/m-p/317584#M95004</guid>
      <dc:creator>vrmandadi</dc:creator>
      <dc:date>2017-12-01T17:14:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate  the average of a field value for n number of days?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-average-of-a-field-value-for-n-number-of/m-p/317585#M95005</link>
      <description>&lt;P&gt;Give this a try&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=abc sourcetype=xy.. | bucket span=1d _time | stats count by _time host | stats avg(count) as count by host | eval temp=1 | xyseries temp host count
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 01 Dec 2017 17:17:03 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-average-of-a-field-value-for-n-number-of/m-p/317585#M95005</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2017-12-01T17:17:03Z</dc:date>
    </item>
  </channel>
</rss>

