Splunk Search

How to calculate Weighted Average

tac24
New Member

After the base search such as:

......
 | stats sum(r1) as t_r1  sum(r2) as t_r2  sum(duras) as total_dura c(member) as samples by group 
 | eval ratio1 = t_r1/total_dura
 | eval ratio2 = t_r2/total_dura
 | table group ratio1 ratio2 samples 

I got the table like below:

   group        ratio1           ratio2        samples
---------------------------------------------------------------
     A            0.8              0.2            100
     B            0.3              0.7             50
     C            0.5              0.5            300

Then, I can calculate simple average over group by: | stats avg(ratio1) avg(ratio2) .
Could someone help me how to calculate weighted average by using samples?
i.e., the weight for A is 100/(100+50+300), B is 50/(100+50+300),...

0 Karma
1 Solution

renjith_nair
Legend

You can use eventstats to achieve this

|eventstats sum(samples) as total | eval weighted_avg=samples/total 

You can use by clause in eventstats to group the records

---
What goes around comes around. If it helps, hit it with Karma 🙂

View solution in original post

tac24
New Member

Thank you very much, it worked!
Excuse me, my question is rather ambiguous. I would like to add something for beginners like me...
To get weighted-average finally, after eventstats, the following commands can be added.

 | eval w_avg_of_ratio1_per_group = ratio1*samples/total 
 | stats sum(w_avg_of_ratio1_per_group) as w_avg_of_ratio1
0 Karma

renjith_nair
Legend

You can use eventstats to achieve this

|eventstats sum(samples) as total | eval weighted_avg=samples/total 

You can use by clause in eventstats to group the records

---
What goes around comes around. If it helps, hit it with Karma 🙂
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Can’t Make It to Boston? Stream .conf25 and Learn with Haya Husain

Boston may be buzzing this September with Splunk University and .conf25, but you don’t have to pack a bag to ...

Splunk Lantern’s Guide to The Most Popular .conf25 Sessions

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...

Unlock What’s Next: The Splunk Cloud Platform at .conf25

In just a few days, Boston will be buzzing as the Splunk team and thousands of community members come together ...