<?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: Splunk table with percentages in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Splunk-table-with-percentages/m-p/157475#M44317</link>
    <description>&lt;P&gt;Have you tried using the top command? This will give you a count and a percentage.&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
&lt;P&gt;| top 10 field&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;</description>
    <pubDate>Thu, 08 May 2014 19:36:02 GMT</pubDate>
    <dc:creator>ecambra_splunk</dc:creator>
    <dc:date>2014-05-08T19:36:02Z</dc:date>
    <item>
      <title>Splunk table with percentages</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Splunk-table-with-percentages/m-p/157473#M44315</link>
      <description>&lt;P&gt;I want is a table that looks like this, but it seems like there is no simple way:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Field        Count of sessions with the field   Percent of sessions with the field
field_1      count_1                            percent_1
field_2      count_2                            percent_2
field_3      count_3                            percent_3
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This is the best way I have found to do it:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;search (_field1_ OR _field2_ OR _field3_ OR _field_______that_______is_______in_______all_______sessions_) | stats dc(eval(if(field=_field1_, SessionID, NULL))) AS count1, dc(eval(if(field=_field2_, SessionID, NULL))) AS count2, dc(eval(if(field=_field1_, SessionID, NULL))) AS count3, dc(eval(if(field=_field_______that_______is_______in_______all_______sessions_, SessionID, NULL))) AS numberOfSessions | eval row=mvrange(1,4) | mvexpand row | eval Field=case(row=1,"field_______1", row=2,"field_______2", row=3,"field_______3") | eval Count=case(row=1,count1, row=2,count2, row=3,count3) | eval Percent=100*Count/numberOfSessions | table Field Count Percent
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This works, but I feel like it is a pretty messy workaround and that there should be a better way. Also, I think using mvexpand makes it take longer. I could probably also do it with a transaction, but that also slows things down. I would like to just do &lt;EM&gt;dc(SessionID) by field&lt;/EM&gt;, but then I can't get the total number of sessions into its own column to calculate the percentages.&lt;/P&gt;</description>
      <pubDate>Thu, 08 May 2014 19:21:26 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Splunk-table-with-percentages/m-p/157473#M44315</guid>
      <dc:creator>kevink1</dc:creator>
      <dc:date>2014-05-08T19:21:26Z</dc:date>
    </item>
    <item>
      <title>Re: Splunk table with percentages</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Splunk-table-with-percentages/m-p/157474#M44316</link>
      <description>&lt;P&gt;So... your data looks like this?&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;SessionID=id1 field=foo
SessionID=id2 field=bar
SessionID=id3 field=baz
SessionID=id4 field=foo
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;And you'd like a result like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;field   count  percentage
foo         2         50%
bar         1         25%
baz         1         25%
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;?&lt;/P&gt;

&lt;P&gt;If so, you can do this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;your search | eventstats dc(SessionID) as total | stats dc(SessionID) as count avg(total) as total by field | eval percentage = round(count/total*100, 2)."%" | fields - total
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 08 May 2014 19:32:40 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Splunk-table-with-percentages/m-p/157474#M44316</guid>
      <dc:creator>martin_mueller</dc:creator>
      <dc:date>2014-05-08T19:32:40Z</dc:date>
    </item>
    <item>
      <title>Re: Splunk table with percentages</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Splunk-table-with-percentages/m-p/157475#M44317</link>
      <description>&lt;P&gt;Have you tried using the top command? This will give you a count and a percentage.&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
&lt;P&gt;| top 10 field&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;</description>
      <pubDate>Thu, 08 May 2014 19:36:02 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Splunk-table-with-percentages/m-p/157475#M44317</guid>
      <dc:creator>ecambra_splunk</dc:creator>
      <dc:date>2014-05-08T19:36:02Z</dc:date>
    </item>
    <item>
      <title>Re: Splunk table with percentages</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Splunk-table-with-percentages/m-p/157476#M44318</link>
      <description>&lt;P&gt;Thanks for the response! That is almost it. In every session, there is a log line with field=foo. The log lines I am interested in have field=bar, field=baz1, and field=baz2. I am looking for a distinct count of field=bar as the total.&lt;/P&gt;

&lt;P&gt;I think eventstats is the command I was looking for, though. If I just do "| eventstats dc(eval(if(field="bar", SessionID, NULL))) as total" instead of "| eventstats dc(SessionID) as total", I should get the desired result, albeit with an extra row for the total, which is fine.&lt;/P&gt;

&lt;P&gt;EDIT: Nvm, this is exactly what I was looking for.&lt;/P&gt;</description>
      <pubDate>Thu, 08 May 2014 20:52:46 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Splunk-table-with-percentages/m-p/157476#M44318</guid>
      <dc:creator>kevink1</dc:creator>
      <dc:date>2014-05-08T20:52:46Z</dc:date>
    </item>
    <item>
      <title>Re: Splunk table with percentages</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Splunk-table-with-percentages/m-p/157477#M44319</link>
      <description>&lt;P&gt;The top command will give me a percentage, but not the percentage I am looking for, as my total is only the count for one value of field. The rest of the values are the things that I am looking to count and get the percentages of. I think martin_mueller's solution is more along the lines of what I need.&lt;/P&gt;</description>
      <pubDate>Thu, 08 May 2014 20:56:13 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Splunk-table-with-percentages/m-p/157477#M44319</guid>
      <dc:creator>kevink1</dc:creator>
      <dc:date>2014-05-08T20:56:13Z</dc:date>
    </item>
    <item>
      <title>Re: Splunk table with percentages</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Splunk-table-with-percentages/m-p/157478#M44320</link>
      <description>&lt;P&gt;If &lt;CODE&gt;field="bar"&lt;/CODE&gt; for every &lt;CODE&gt;SessionID&lt;/CODE&gt;, how does your eval'd distinct count differ from a distinct count without any filter?&lt;/P&gt;</description>
      <pubDate>Thu, 08 May 2014 21:01:25 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Splunk-table-with-percentages/m-p/157478#M44320</guid>
      <dc:creator>martin_mueller</dc:creator>
      <dc:date>2014-05-08T21:01:25Z</dc:date>
    </item>
    <item>
      <title>Re: Splunk table with percentages</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Splunk-table-with-percentages/m-p/157479#M44321</link>
      <description>&lt;P&gt;That's a good point. Looks like I'm just over complicating things. Anyways, eventstats is the key. Didn't know about it before. Thanks for the help!&lt;/P&gt;</description>
      <pubDate>Thu, 08 May 2014 21:10:27 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Splunk-table-with-percentages/m-p/157479#M44321</guid>
      <dc:creator>kevink1</dc:creator>
      <dc:date>2014-05-08T21:10:27Z</dc:date>
    </item>
  </channel>
</rss>

