Splunk Search

How to use appendpipe to sum count and calculate correct percentage?

richtate
Path Finder

I have a query that works, but the output calculates a percentage column in a chart.  I need to show the total of TAM and the correct percentage value for all the returned rows.  I'm using this:

| inputlookup Patch-Status_Summary_AllBU_v3.csv
| stats count(ip_address) as total, sum(comptag) as compliant_count by BU
| eval patchcompliance=round((compliant_count/total)*100,1)
| fields BU total compliant_count patchcompliance
| rename BU as Domain, total as TAM, patchcompliance as "% Compliance"
| appendpipe
[stats sum(TAM) as TAM sum(compliant_count) as compliant_count
| eval totpercent=round((comp/TAM)*100,1)]
| eval TAM = tostring(TAM, "commas")

 

The output is:

Domain TAM compliant_count % Compliance

BU1 1,180 1146 97.1
BU2 2,489 2420 97.2
BU3 409,881 96653 23.6
BU4 3 3 100.0
BU5 1,404 1375 97.9
BU6 119,003 90100 75.7
BU7 33,506 30669 91.5
BU8 2,862 1997 69.8
BU9 239,897 216401 90.2
BU10 3,945 3832 97.1
BU11 569 482 84.7
  814,739 445078  

 

If I add to the appendpipe stats command avg("% Compliance") as "% Compliance" then it will not take add up the correct percentage which in this case is "54.6" but the average would display "87.1".

How do I calculate the correct percentage as a total using the totals of columns TA

Labels (2)
0 Karma
1 Solution

johnhuang
Motivator

You don't need to use appendpipe for this. You can simply use addcoltotals to sum up the field total prior to calculating the percentage.

| inputlookup Patch-Status_Summary_AllBU_v3.csv
| stats count(ip_address) as total, sum(comptag) as compliant_count by BU
| addcoltotals labelfield=BU label=TOTAL total compliant_count
| eval patchcompliance=round((compliant_count/total)*100,1)
| fields BU total compliant_count patchcompliance
| rename BU as Domain, total as TAM, patchcompliance as "% Compliance"

 

View solution in original post

richtate
Path Finder

Thanks @johnhuang, that worked exactly like I needed it to!

johnhuang
Motivator

You don't need to use appendpipe for this. You can simply use addcoltotals to sum up the field total prior to calculating the percentage.

| inputlookup Patch-Status_Summary_AllBU_v3.csv
| stats count(ip_address) as total, sum(comptag) as compliant_count by BU
| addcoltotals labelfield=BU label=TOTAL total compliant_count
| eval patchcompliance=round((compliant_count/total)*100,1)
| fields BU total compliant_count patchcompliance
| rename BU as Domain, total as TAM, patchcompliance as "% Compliance"

 

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 ...