Splunk Search

Chart, order, and bins: How to separate a chart according to an imposed order

BrentHetherwick
Explorer

I have some non-time-based data that I'd like to summarize using chart with a small number of bins.  For example,

 

<some search> | stats count as c by foo | sort -c | chart sum(c) by foo bins=10

 

"foo" is not numeric, so it automatically fails, but I don't want the bins to be determined by an intrinsic order on foo anyway: The bins should respect the order that comes from the sort command. Thus, in the chart, the first "bar" should represent the greatest decile of the "sort -c" command, the second, the second decile, and so on.

I can't figure out how to wrangle an order into the chart command or otherwise make it respect the sort and use that order as the basis for the separations into bins. Am I barking up the wrong tree here?

Labels (1)
0 Karma
1 Solution

ITWhisperer
SplunkTrust
SplunkTrust

Try something like this

| stats count as c by foo
| sort - c
| eventstats count as foos
| eval binsize=ceil(foos/10)
| streamstats count as row
| eval foobin=1+floor((row-1)/binsize)
| chart sum(c) by foobin

View solution in original post

0 Karma

BrentHetherwick
Explorer

Yes, I do realize that my question isn't very well-formed. Let me provide an example, using 2 bins, instead of 10 for brevity:

Suppose my data has 10 lines where foo="A", 9 lines where foo="B", 8 lines where foo="C", and 7 lines where foo="D". Then

| stats count as c by foo | sort -c

should output

foo c
A   10
B   9
C   8
D   7 

What I want from the chart+bins command (for example) is something like this:

bins sum
bin1 19
bin2 15

 ...where bin1 is formed from A and B, since they have the top two c values and bin2 is C and D, as the least, and the sum value is sum(c).

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Try something like this

| stats count as c by foo
| sort - c
| eventstats count as foos
| eval binsize=ceil(foos/10)
| streamstats count as row
| eval foobin=1+floor((row-1)/binsize)
| chart sum(c) by foobin
0 Karma

BrentHetherwick
Explorer

That appears to be what I need. Thanks much.

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

It isn't clear what your expected result would look like - having said that, does this help?

| stats count as c by foo
| sort - c
| transpose 0 header_field=foo column_name=count
0 Karma
Get Updates on the Splunk Community!

Advanced Splunk Data Management Strategies

Join us on Wednesday, May 14, 2025, at 11 AM PDT / 2 PM EDT for an exclusive Tech Talk that delves into ...

Uncovering Multi-Account Fraud with Splunk Banking Analytics

Last month, I met with a Senior Fraud Analyst at a nationally recognized bank to discuss their recent success ...

Secure Your Future: A Deep Dive into the Compliance and Security Enhancements for the ...

What has been announced?  In the blog, “Preparing your Splunk Environment for OpensSSL3,”we announced the ...