<?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: Sorting multi-series column chart by count field in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Sorting-multi-series-column-chart-by-count-field/m-p/607265#M211127</link>
    <description>&lt;P&gt;Not to necro this thread, but...&lt;/P&gt;&lt;P&gt;This page still turns up in Google so let's update it with an answer!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The following seems to fix the suggested code&amp;nbsp; (at least here in the year 2022...)&lt;/P&gt;&lt;PRE&gt;yoursearchhere
| chart count by field1, field2
| addtotals &lt;STRONG&gt;fieldname=totalCount
| &lt;/STRONG&gt;sort 0 totalCount
| fields - totalCount&lt;/PRE&gt;&lt;P&gt;Alternatively,&lt;/P&gt;&lt;P&gt;you should be able to accept the default field created by addtotals (i.e. it is named "&lt;STRONG&gt;Total")&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Which simplifies the code to...&lt;/P&gt;&lt;PRE&gt;yoursearchhere
| chart count by field1, field2
| addtotals
| sort 0 Total
| fields - Total&lt;/PRE&gt;&lt;P&gt;For more information, refer to the &lt;STRONG&gt;addtotals &lt;/STRONG&gt;documentation&lt;STRONG&gt;:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Addtotals" target="_blank" rel="noopener"&gt;addtotals - Splunk Documentation&lt;/A&gt;&lt;/P&gt;&lt;P&gt;refer to the &lt;STRONG&gt;sort &lt;/STRONG&gt;documentation also&lt;/P&gt;&lt;P&gt;&lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Sort" target="_blank" rel="noopener"&gt;sort - Splunk Documentation&lt;/A&gt;&lt;/P&gt;&lt;P&gt;the &lt;STRONG&gt;sort 0 &lt;/STRONG&gt;above is discussed there&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&amp;lt;count&amp;gt;&lt;BR /&gt;&lt;STRONG&gt;Syntax&lt;/STRONG&gt;:&amp;nbsp;&amp;lt;int&amp;gt; | limit=&amp;lt;int&amp;gt;&lt;BR /&gt;&lt;STRONG&gt;Description&lt;/STRONG&gt;:&amp;nbsp;Specify the number of results to return from the sorted results. &lt;BR /&gt;If no count is specified, the default limit of 10000 is used. &lt;BR /&gt;If&amp;nbsp;0&amp;nbsp;is specified, all results are returned.          &lt;FONT color="#FF0000"&gt; *** &lt;STRONG&gt;NOTE&lt;/STRONG&gt; ***&lt;/FONT&gt;&lt;BR /&gt;You can specify the count using an integer or precede the count with a label,&lt;BR /&gt;for example&amp;nbsp;limit=10.&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;*** NOTE ***&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;Using&amp;nbsp;sort 0&amp;nbsp;might have a negative impact performance, &lt;BR /&gt;depending on how many results are returned.&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you want for example, the top 10 "results"&amp;nbsp; in descending order then you do the following ...&lt;/P&gt;&lt;P&gt;Note the minus character ("-") in front the Total field,&amp;nbsp; this &lt;EM&gt;reverses the sort order iirc.&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Below I used the "limit=10" rather than "10" just because it makes the code more readable&lt;/EM&gt;&lt;/P&gt;&lt;PRE&gt;yoursearchhere
| chart count by field1, field2
| addtotals
| sort &lt;STRONG&gt;limit=10 -&lt;/STRONG&gt;Total
| fields - Total&lt;/PRE&gt;&lt;P&gt;PS.&lt;/P&gt;&lt;P&gt;You should be able to adapt the other example from&amp;nbsp;&lt;STRONG&gt;lguinn2 in the same way&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;P&gt;&lt;EM&gt;"This works well as long as field1 does not contain numeric values. If it&amp;nbsp;does, then you can do this..."&lt;/EM&gt;&lt;/P&gt;&lt;PRE&gt;yoursearchhere
| chart count by field1, field2
| addtotals &lt;STRONG&gt;fieldname=totalCount&lt;/STRONG&gt;
| eval totalCount = totalCount - field1
| sort 0 totalCount
| fields - totalCount&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
    <pubDate>Wed, 27 Jul 2022 21:04:01 GMT</pubDate>
    <dc:creator>Machine247</dc:creator>
    <dc:date>2022-07-27T21:04:01Z</dc:date>
    <item>
      <title>Sorting multi-series column chart by count field</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Sorting-multi-series-column-chart-by-count-field/m-p/108261#M28151</link>
      <description>&lt;P&gt;Not sure why this is so perplexing, but or the life of me I can't get this to sort how I want.  &lt;/P&gt;

&lt;P&gt;The following chart syntax:&lt;BR /&gt;
|chart count(C) as Count by B,C&lt;/P&gt;

&lt;P&gt;where B is a Month field, C represents 5 separate values and Count is the count of those values as they occur by Month.&lt;/P&gt;

&lt;P&gt;The resulting multi-series chart displays with the correct data, but regardless of how I try and sort, the Month is sorted correctly, but within each month the columns representing the 5 counts are always sorted by the alpha value of "C" and not the count.&lt;/P&gt;

&lt;P&gt;Basically I wanted to do this &lt;BR /&gt;
| chart count(C) as Count by B,C |sort 0 B,Count&lt;/P&gt;

&lt;P&gt;But that doesn't work.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jul 2013 21:30:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Sorting-multi-series-column-chart-by-count-field/m-p/108261#M28151</guid>
      <dc:creator>Cuyose</dc:creator>
      <dc:date>2013-07-26T21:30:34Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting multi-series column chart by count field</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Sorting-multi-series-column-chart-by-count-field/m-p/108262#M28152</link>
      <description>&lt;P&gt;The reason that this doesn't work is: the columns in the resulting chart are named by the VALUES of C. So if the events had values of C such as "red", "yellow", "green", "blue", "orange" - then the columns would be named red, yellow, green, blue, and orange. So you would have to do something like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| chart count(C) as Count by B,C |sort 0 B,red
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;And that probably doesn't make any sense. Perhaps what you should do is to sort by the &lt;EM&gt;overall&lt;/EM&gt; count. Here is how to do that:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;yoursearchhere
| chart count by field1, field2
| addtotals totalCount
| sort 0 totalCount
| fields - totalCount
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This works well as long as field1 does not contain numeric values. If it &lt;EM&gt;does&lt;/EM&gt;, then you can do this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;yoursearchhere
| chart count by field1, field2
| addtotals totalCount
| eval totalCount = totalCount - field1
| sort 0 totalCount
| fields - totalCount
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 26 Jul 2013 22:02:02 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Sorting-multi-series-column-chart-by-count-field/m-p/108262#M28152</guid>
      <dc:creator>lguinn2</dc:creator>
      <dc:date>2013-07-26T22:02:02Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting multi-series column chart by count field</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Sorting-multi-series-column-chart-by-count-field/m-p/108263#M28153</link>
      <description>&lt;P&gt;Im still unable to get this to do anything different than the original chart.  Perhaps I am misunderstanding your instructions.  In the form above would it be:&lt;BR /&gt;
| chart count(C) as Count by B,C&lt;BR /&gt;
| addtotals totalCount&lt;BR /&gt;
| eval totalCount = totalCount - B&lt;BR /&gt;
| sort 0 totalCount&lt;BR /&gt;
| fields - totalCount&lt;/P&gt;

&lt;P&gt;I tried this and it doesn't do anything different, C is still the 2nd sort, not the count of C&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jul 2013 22:13:46 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Sorting-multi-series-column-chart-by-count-field/m-p/108263#M28153</guid>
      <dc:creator>Cuyose</dc:creator>
      <dc:date>2013-07-26T22:13:46Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting multi-series column chart by count field</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Sorting-multi-series-column-chart-by-count-field/m-p/108264#M28154</link>
      <description>&lt;P&gt;Thanks! I used this and it did EXACTLY what I wanted .... save one minor detail. The Total field at the far right is blank. I was expecting it to sum all of the values left to right on that row.&lt;/P&gt;

&lt;P&gt;example&lt;BR /&gt;
field1, field2_subtotal, field2_ sub total, Total&lt;/P&gt;

&lt;P&gt;Any ideas what I need to do to fix the "Totals" column?&lt;/P&gt;

&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 08:41:12 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Sorting-multi-series-column-chart-by-count-field/m-p/108264#M28154</guid>
      <dc:creator>kennyja</dc:creator>
      <dc:date>2020-09-29T08:41:12Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting multi-series column chart by count field</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Sorting-multi-series-column-chart-by-count-field/m-p/607265#M211127</link>
      <description>&lt;P&gt;Not to necro this thread, but...&lt;/P&gt;&lt;P&gt;This page still turns up in Google so let's update it with an answer!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The following seems to fix the suggested code&amp;nbsp; (at least here in the year 2022...)&lt;/P&gt;&lt;PRE&gt;yoursearchhere
| chart count by field1, field2
| addtotals &lt;STRONG&gt;fieldname=totalCount
| &lt;/STRONG&gt;sort 0 totalCount
| fields - totalCount&lt;/PRE&gt;&lt;P&gt;Alternatively,&lt;/P&gt;&lt;P&gt;you should be able to accept the default field created by addtotals (i.e. it is named "&lt;STRONG&gt;Total")&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Which simplifies the code to...&lt;/P&gt;&lt;PRE&gt;yoursearchhere
| chart count by field1, field2
| addtotals
| sort 0 Total
| fields - Total&lt;/PRE&gt;&lt;P&gt;For more information, refer to the &lt;STRONG&gt;addtotals &lt;/STRONG&gt;documentation&lt;STRONG&gt;:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Addtotals" target="_blank" rel="noopener"&gt;addtotals - Splunk Documentation&lt;/A&gt;&lt;/P&gt;&lt;P&gt;refer to the &lt;STRONG&gt;sort &lt;/STRONG&gt;documentation also&lt;/P&gt;&lt;P&gt;&lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Sort" target="_blank" rel="noopener"&gt;sort - Splunk Documentation&lt;/A&gt;&lt;/P&gt;&lt;P&gt;the &lt;STRONG&gt;sort 0 &lt;/STRONG&gt;above is discussed there&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&amp;lt;count&amp;gt;&lt;BR /&gt;&lt;STRONG&gt;Syntax&lt;/STRONG&gt;:&amp;nbsp;&amp;lt;int&amp;gt; | limit=&amp;lt;int&amp;gt;&lt;BR /&gt;&lt;STRONG&gt;Description&lt;/STRONG&gt;:&amp;nbsp;Specify the number of results to return from the sorted results. &lt;BR /&gt;If no count is specified, the default limit of 10000 is used. &lt;BR /&gt;If&amp;nbsp;0&amp;nbsp;is specified, all results are returned.          &lt;FONT color="#FF0000"&gt; *** &lt;STRONG&gt;NOTE&lt;/STRONG&gt; ***&lt;/FONT&gt;&lt;BR /&gt;You can specify the count using an integer or precede the count with a label,&lt;BR /&gt;for example&amp;nbsp;limit=10.&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;*** NOTE ***&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;Using&amp;nbsp;sort 0&amp;nbsp;might have a negative impact performance, &lt;BR /&gt;depending on how many results are returned.&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you want for example, the top 10 "results"&amp;nbsp; in descending order then you do the following ...&lt;/P&gt;&lt;P&gt;Note the minus character ("-") in front the Total field,&amp;nbsp; this &lt;EM&gt;reverses the sort order iirc.&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Below I used the "limit=10" rather than "10" just because it makes the code more readable&lt;/EM&gt;&lt;/P&gt;&lt;PRE&gt;yoursearchhere
| chart count by field1, field2
| addtotals
| sort &lt;STRONG&gt;limit=10 -&lt;/STRONG&gt;Total
| fields - Total&lt;/PRE&gt;&lt;P&gt;PS.&lt;/P&gt;&lt;P&gt;You should be able to adapt the other example from&amp;nbsp;&lt;STRONG&gt;lguinn2 in the same way&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;P&gt;&lt;EM&gt;"This works well as long as field1 does not contain numeric values. If it&amp;nbsp;does, then you can do this..."&lt;/EM&gt;&lt;/P&gt;&lt;PRE&gt;yoursearchhere
| chart count by field1, field2
| addtotals &lt;STRONG&gt;fieldname=totalCount&lt;/STRONG&gt;
| eval totalCount = totalCount - field1
| sort 0 totalCount
| fields - totalCount&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Wed, 27 Jul 2022 21:04:01 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Sorting-multi-series-column-chart-by-count-field/m-p/607265#M211127</guid>
      <dc:creator>Machine247</dc:creator>
      <dc:date>2022-07-27T21:04:01Z</dc:date>
    </item>
  </channel>
</rss>

