Splunk Search

How to sort dynamic column names after timechart and transpose

gnoriega
Explorer

Hi,

I'm creating a report with the following search that runs each month covering the past 3 months of data.

It works and I can display the results in a bar chart but it gets sorted alphabetically by sourcetype. 

 

index=* | timechart span=1mon count by sourcetype 
 | eval _time = strftime(_time,"%B")
 | rename _time as Time
 | fields - _*
 | transpose header_field=Time column_name="sourcetype"

 

I want to sort it by count of last month. So for example if I run the report in July I get columns "sourcetype", "April", "May", "June". Each month that I run the report the column names will change. I can get the results I want this month  by adding:

 

 | sort - "June"

 

How can I set this up automatically  so that the results are sorted by the last column (previous month)?

Labels (1)
0 Karma
1 Solution

bowesmana
SplunkTrust
SplunkTrust

This works by giving the previous month a fixed field name and then sorting by that before naming it correctly at the end

 

index=*
| timechart span=1mon count by sourcetype 
| eval _time = if(_time=relative_time(now(),"-1mon@mon"), "Last Month", strftime(_time,"%B")) 
| rename _time as Time 
| fields - _* 
| transpose header_field=Time column_name="sourcetype"
| sort - "Last Month"
| eval name=strftime(relative_time(now(),"-1mon@mon"),"%B")
| eval {name}='Last Month'
| fields - "Last Month" name

 

which is simply just calling the relative -1 month from now as last month rather than the name itself and then fixing up the name after the sort.

"Last Month" could be anything temporary

 

View solution in original post

0 Karma

bowesmana
SplunkTrust
SplunkTrust

This works by giving the previous month a fixed field name and then sorting by that before naming it correctly at the end

 

index=*
| timechart span=1mon count by sourcetype 
| eval _time = if(_time=relative_time(now(),"-1mon@mon"), "Last Month", strftime(_time,"%B")) 
| rename _time as Time 
| fields - _* 
| transpose header_field=Time column_name="sourcetype"
| sort - "Last Month"
| eval name=strftime(relative_time(now(),"-1mon@mon"),"%B")
| eval {name}='Last Month'
| fields - "Last Month" name

 

which is simply just calling the relative -1 month from now as last month rather than the name itself and then fixing up the name after the sort.

"Last Month" could be anything temporary

 

0 Karma

gnoriega
Explorer

Thanks @bowesmana  this is precisely what I needed.

0 Karma
Get Updates on the Splunk Community!

.conf24 | Personalize your .conf experience with Learning Paths!

Personalize your .conf24 Experience Learning paths allow you to level up your skill sets and dive deeper ...

Threat Hunting Unlocked: How to Uplevel Your Threat Hunting With the PEAK Framework ...

WATCH NOWAs AI starts tackling low level alerts, it's more critical than ever to uplevel your threat hunting ...

Splunk APM: New Product Features + Community Office Hours Recap!

Howdy Splunk Community! Over the past few months, we’ve had a lot going on in the world of Splunk Application ...