Splunk Search

Distinct count by multiple conditions chart

kriptonpt
Engager

Hi 

 

Assuming a sample of data from this example: 

 

 

 

| makeresults count=5
| eval f1=random()%2
| eval f2=random()%2
| eval f3=random()%2
| eval f4=random()%2
| eval H=round(((random() % 102)/(102)) * (104 - 100) + 100)

 

 

 

Hf1f2f3f4
1001001
1001101
1011100
1021110

 

I want to built a chart which contains the distinct count of H  for f1,f2,f3,f4 with 1 

f1f2f3f4
3311

 

Someone can help?

Labels (2)
0 Karma
1 Solution

ITWhisperer
SplunkTrust
SplunkTrust
| foreach f1 f2 f3 f4
    [| eval <<FIELD>>=if(<<FIELD>>==1,1,null())]
| eventstats dc(H) as d1 by f1
| eventstats dc(H) as d2 by f2
| eventstats dc(H) as d3 by f3
| eventstats dc(H) as d4 by f4
| stats values(d*) as d*

View solution in original post

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust
| foreach f1 f2 f3 f4
    [| eval <<FIELD>>=if(<<FIELD>>==1,1,null())]
| eventstats dc(H) as d1 by f1
| eventstats dc(H) as d2 by f2
| eventstats dc(H) as d3 by f3
| eventstats dc(H) as d4 by f4
| stats values(d*) as d*
0 Karma

kriptonpt
Engager

Didn't work.

one possible way was:

f1=1 | stats dc(H)
|appendcols [search  f2=1 | stats dc(H)]
| appendcols [search  f3=1 | stats dc(H)]
| appendcols [search  f4=1 | stats dc(H)]


but it is not efficient 

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

In what way didn't it work?

Here is a runanywhere example showing it working - I have used eventstats for the final command so you can see the random values used

| makeresults count=5
| fields - _time
| eval f1=random()%2
| eval f2=random()%2
| eval f3=random()%2
| eval f4=random()%2
| eval H=round(((random() % 102)/(102)) * (104 - 100) + 100)
| foreach f1 f2 f3 f4
    [| eval <<FIELD>>=if(<<FIELD>>==1,1,null())]
| eventstats dc(H) as d1 by f1
| eventstats dc(H) as d2 by f2
| eventstats dc(H) as d3 by f3
| eventstats dc(H) as d4 by f4
| eventstats values(d*) as d*
0 Karma

kriptonpt
Engager

my search isn't created with makeresults, I only put it as an example.
doesn't work because if I use:

search | foreach f1 f2 f3 f4
    [| eval <<FIELD>>=if(<<FIELD>>==1,1,null())]
| eventstats dc(H) as d1 by f1
| eventstats dc(H) as d2 by f2
| eventstats dc(H) as d3 by f3
| eventstats dc(H) as d4 by f4
| stats values(d*) as d*

the result of f1 is different comparing with the result if I use:

search f1=1 |stats dc(H)
0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Given the limited amount of information you have given, it is not possible to determine the reason for the difference. Your example data does not represent your real data closely enough. For example, do you have special characters / non-alphanumeric characters in your field names? Are your fields multi-valued or appear in your events more than once? If possible, please share a representative example of your data without showing any sensitive data.

0 Karma
Get Updates on the Splunk Community!

Wondering How to Build Resiliency in the Cloud?

IT leaders are choosing Splunk Cloud as an ideal cloud transformation platform to drive business resilience,  ...

Updated Data Management and AWS GDI Inventory in Splunk Observability

We’re making some changes to Data Management and Infrastructure Inventory for AWS. The Data Management page, ...

Introducing the Splunk Community Dashboard Challenge!

Welcome to Splunk Community Dashboard Challenge! This is your chance to showcase your skills in creating ...