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"

 

Get Updates on the Splunk Community!

Introducing the 2024 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...

Introducing the 2024 Splunk MVPs!

We are excited to announce the 2024 cohort of the Splunk MVP program. Splunk MVPs are passionate members of ...

Splunk Custom Visualizations App End of Life

The Splunk Custom Visualizations apps End of Life for SimpleXML will reach end of support on Dec 21, 2024, ...