Splunk Search

How can I compute value based on group by values in timechart?

Sathiya123
Explorer

| eval vm_unit=case(vmSize="Standard_F16s_v2",2,vmSize="Standard_F8s_v2",1,vmSize="Standard_F4s",0.5,vmSize="Standard_F2s_v2",0.25)
| bin _time span=1h
| stats values(vm_unit) as vm_unit values(location) as location by _time id
| timechart span=1h usenull=true sum(vm_unit)-case(location="westus2",2245,location="centralus",2146,location="northeurope",624,location="germanywestcentral",620) as vm_count by location
| fillnull value=0

Labels (2)
0 Karma
1 Solution

ITWhisperer
SplunkTrust
SplunkTrust

@yuanliu vm_count doesn't exist after timechart

Either do it this way:

| eval vm_unit=case(vmSize="Standard_F16s_v2",2,vmSize="Standard_F8s_v2",1,vmSize="Standard_F4s",0.5,vmSize="Standard_F2s_v2",0.25)
| bin _time span=1h
| stats values(vm_unit) as vm_unit values(location) as location by _time id
| timechart span=1h usenull=true sum(vm_unit) as vm_count by location
| fillnull value=0
| eval westus2 = westus2 - 2245
| eval centralus = centralus - 2146
| eval northeurope = northeurope - 624
| eval germanywestcentral = germanywestcentral - 620

or this way

| eval vm_unit=case(vmSize="Standard_F16s_v2",2,vmSize="Standard_F8s_v2",1,vmSize="Standard_F4s",0.5,vmSize="Standard_F2s_v2",0.25)
| bin _time span=1h
| stats values(vm_unit) as vm_unit values(location) as location by _time id
| stats sum(vm_unit) as vm_count by _time location
| eval vm_count = vm_count - case(location="westus2",2245,location="centralus",2146,location="northeurope",624,location="germanywestcentral",620)
| timechart span=1h usenull=true sum(vm_count) as vm_count by location
| fillnull value=0

Although, to be fair, I am not sure what this is trying to achieve

View solution in original post

yuanliu
SplunkTrust
SplunkTrust

As you discovered, operators are not allowed outside of stats functions in a stats command.  Why not calculate it after stats?

| eval vm_unit=case(vmSize="Standard_F16s_v2",2,vmSize="Standard_F8s_v2",1,vmSize="Standard_F4s",0.5,vmSize="Standard_F2s_v2",0.25)
| bin _time span=1h
| stats values(vm_unit) as vm_unit values(location) as location by _time id
| timechart span=1h usenull=true sum(vm_unit) as vm_count by location
| eval vm_count = vm_count - case(location="westus2",2245,location="centralus",2146,location="northeurope",624,location="germanywestcentral",620)
| fillnull value=0

 

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

@yuanliu vm_count doesn't exist after timechart

Either do it this way:

| eval vm_unit=case(vmSize="Standard_F16s_v2",2,vmSize="Standard_F8s_v2",1,vmSize="Standard_F4s",0.5,vmSize="Standard_F2s_v2",0.25)
| bin _time span=1h
| stats values(vm_unit) as vm_unit values(location) as location by _time id
| timechart span=1h usenull=true sum(vm_unit) as vm_count by location
| fillnull value=0
| eval westus2 = westus2 - 2245
| eval centralus = centralus - 2146
| eval northeurope = northeurope - 624
| eval germanywestcentral = germanywestcentral - 620

or this way

| eval vm_unit=case(vmSize="Standard_F16s_v2",2,vmSize="Standard_F8s_v2",1,vmSize="Standard_F4s",0.5,vmSize="Standard_F2s_v2",0.25)
| bin _time span=1h
| stats values(vm_unit) as vm_unit values(location) as location by _time id
| stats sum(vm_unit) as vm_count by _time location
| eval vm_count = vm_count - case(location="westus2",2245,location="centralus",2146,location="northeurope",624,location="germanywestcentral",620)
| timechart span=1h usenull=true sum(vm_count) as vm_count by location
| fillnull value=0

Although, to be fair, I am not sure what this is trying to achieve

Sathiya123
Explorer

Wow, thanks for your solution! It worked like charm!

0 Karma

Sathiya123
Explorer

it says no results found. But the below query works without the case.

 

| eval vm_unit=case(vmSize="Standard_F16s_v2",2,vmSize="Standard_F8s_v2",1,vmSize="Standard_F4s",0.5,vmSize="Standard_F2s_v2",0.25)
| bin _time span=1h
| stats values(vm_unit) as vm_unit values(location) as location by _time id
| timechart span=1h usenull=true sum(vm_unit) as vm_count by location
| fillnull value=0

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

The subtraction with the case is not valid in the timechart command.

It is not clear what you are trying to do here.

Do you wish to subtract a value from the sum for the location based on the location, or subtract a value from the vm_unit based on the location prior to summing for each location, or something else?

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Why Splunk Customers Should Attend Cisco Live 2026 Las Vegas

Why Splunk Customers Should Attend Cisco Live 2026 Las Vegas     Cisco Live 2026 is almost here, and this ...

What Is the Name of the USB Key Inserted by Bob Smith? (BOTS Hint, Not the Answer)

Hello Splunkers,   So you searched, “what is the name of the usb key inserted by bob smith?”  Not gonna lie… ...

Automating Threat Operations and Threat Hunting with Recorded Future

    Automating Threat Operations and Threat Hunting with Recorded Future June 29, 2026 | Register   Is your ...