Splunk Search

How can I calculate a percentage over time?

acabrera1
Explorer

I have the following search command defined by a custom time range (last 2 weeks):

index=mlr3_google sourcetype=mlr3_google_ext | bucket span=1w _time | eval Date=strftime(_time,"%m-%d-%y") | chart sum(uniqueVisitors) over sourcetype by Date 

I want to calculate the percentage change for this week from last week.
How do I calculate that?

Tags (2)

ziegfried
Influencer

You probably want to do smth like this:

index=mlr3_google sourcetype=mlr3_google_ext | eval Date=if(_time>relative_time(now(),"-1w"),"this_week","last_week") | chart sum(uniqueVisitors) as count over sourcetype by Date | eval change_pct=if(last_week==0,"-",if((this_week-last_week)>0,"+","")+tostring(round((this_week-last_week)/last_week*100,1))+"%") | table sourcetype last_week this_week change_pct | rename last_week as "Last week" this_week as "This week" change_pct as "Change in %"

acabrera1
Explorer

Specifying the time range actually solves the problem! Thanks so much for your help!!

0 Karma

acabrera1
Explorer

Thank you for the response however I noticed that the value for 'last week' is summing all previous weeks instead of taking the value of only 'last week'. How can I modify your command to only calculate only 7 days last week?

0 Karma
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Community Content Calendar, September edition

Welcome to another insightful post from our Community Content Calendar! We're thrilled to continue bringing ...

Splunkbase Unveils New App Listing Management Public Preview

Splunkbase Unveils New App Listing Management Public PreviewWe're thrilled to announce the public preview of ...

Leveraging Automated Threat Analysis Across the Splunk Ecosystem

Are you leveraging automation to its fullest potential in your threat detection strategy?Our upcoming Security ...