Dashboards & Visualizations

Using field names as variable in column name

deepdive100
Loves-to-Learn Everything

I have a timechart that I want a column name to reflect a field name like how a variable or token would be used.

 

 

```Example```
|makeresults
|eval sample="100"
|eval name=if(sample=100,"C",N/A)
|timechart max(sample) as $name$

```Expected Outcome would by a timechart with column named "C"```

 

 

 

Is this possible?

Labels (1)
0 Karma

danspav
SplunkTrust
SplunkTrust

Hi @deepdive100.,

You can create the column name based on what the field "name" is set to using by:

|makeresults
|eval sample="100"
|eval name=if(sample=100,"C",N/A)
|timechart max(sample) by name

This creates a table with columns: _time, C. If the values are less or more than 100, there'll be an additional column "N/A"

If you have a dashboard and you want to pick which column is displayed, you could do something like:

|makeresults
|eval sample="100"
|eval name=if(sample=100,"$DROPDOWN_TOKEN$",N/A)
|timechart max(sample) by name

And set up an input that sets the token $DROPDOWN_TOKEN$.

 

0 Karma

deepdive100
Loves-to-Learn Everything

Hi Thanks. This is almost what I need. I think I need to expand on my requirements a bit more.  

```Example```
|makeresults
|eval sample="100"
|eval perc="45"
|eval name=if(sample=100,"C",N/A)
|timechart max(sample) as "The Sample yields $name$", avg(perc) as "percentage"

```Expected Outcome would by a timechart with column named "The Sample yields C" and another column titled "percentage"```

Using the BY clause appends that eval'd field in the column name, but not all columns need that field name.
I'd think that the easiest way about doing it would be some type of variable replacement but it seems that the AS clause does not allow that.

0 Karma

danspav
SplunkTrust
SplunkTrust

I think I understand -  try this search to create a table with fields: _time, percentage and one or more columns based on the value calculated each hour:

| gentimes start=-7
| eval sample=random()%100
| eval perc=random()%50
| rename starttime as _time
| append[|makeresults | eval sample=100, perc=45| table _time, sample, perc]
| timechart span=1d max(sample) as name, avg(perc) as "percentage"

``` Calculate how we name the fields based on the value of: name ```
| eval rename_field_to=if(name=100,"C","N/A")
| eval "The Sample Yields {rename_field_to}" = name
| fields - rename_field_to, name

 

This will create three or four columns:

  1. _time = time
  2. percentage = hourly average of the perc field
  3. The Sample Yields C  =  If the max for that hour was 100
  4. The Sample Yields N/A = If the max for that hour was not 100

If you only want "The Sample Yields C" or nothing, then you can filter out with a | search name="C" after the timechart command.

The main SPL is : 

| eval "The Sample Yields {rename_field_to}" = name


That will allow you to name a field using the value of another field.

 

If you want NA to simply be N/A then you can do a rename:

| rename "The Sample Yields N/A" as "N/A"



Is that closer to what you were after?

 

 

 

0 Karma
Get Updates on the Splunk Community!

Stay Connected: Your Guide to May Tech Talks, Office Hours, and Webinars!

Take a look below to explore our upcoming Community Office Hours, Tech Talks, and Webinars this month. This ...

They're back! Join the SplunkTrust and MVP at .conf24

With our highly anticipated annual conference, .conf, comes the fez-wearers you can trust! The SplunkTrust, as ...

Enterprise Security Content Update (ESCU) | New Releases

Last month, the Splunk Threat Research Team had two releases of new security content via the Enterprise ...