Dashboards & Visualizations

How to create a drop-down menu that lists the last 6 months with the previous month selected by default?

gabriel_vasseur
Contributor

I want a drop-down menu that lists the last 6 months in antichronological order starting with the current month and ideally with the previous month selected by default.

I want to use a search to generate the choice list, but I only know the | stats count | eval ... trick, which yields only one row of results when I need 6. I reluctantly tried the inelegant solution of using several append subsearches, but that takes forever in "parsing job". There has to be a better solution!

As for selecting the correct default, I have no idea how.

0 Karma
1 Solution

sundareshr
Legend

Try this

<form>
  <label>Test Answers</label>
  <fieldset submitButton="false">
    <input type="dropdown" token="field2">
      <search>
        <query>| gentimes start=-180 | bin span=1mon endtime | stats count by endtime | eval month=strftime(endtime, "%B") | table month | reverse</query>
      </search>
      <fieldForLabel>month</fieldForLabel>
      <fieldForValue>month</fieldForValue>
      <default>$d$</default>
    </input>
  </fieldset>
  <row>
    <panel depends="$x$">
      <input type="radio" token="d" searchWhenChanged="true">
        <label>field3</label>
        <default>April</default>
        <search>
          <query>| gentimes start=-180 | bin span=1mon endtime | stats count by endtime | eval month=strftime(endtime, "%B") | table month | tail 2 | tail 1</query>
          <earliest>@d</earliest>
          <latest>now</latest>
        </search>
        <fieldForLabel>month</fieldForLabel>
        <fieldForValue>month</fieldForValue>
      </input>
    </panel>
  </row>
</form>

View solution in original post

sundareshr
Legend

Try this

<form>
  <label>Test Answers</label>
  <fieldset submitButton="false">
    <input type="dropdown" token="field2">
      <search>
        <query>| gentimes start=-180 | bin span=1mon endtime | stats count by endtime | eval month=strftime(endtime, "%B") | table month | reverse</query>
      </search>
      <fieldForLabel>month</fieldForLabel>
      <fieldForValue>month</fieldForValue>
      <default>$d$</default>
    </input>
  </fieldset>
  <row>
    <panel depends="$x$">
      <input type="radio" token="d" searchWhenChanged="true">
        <label>field3</label>
        <default>April</default>
        <search>
          <query>| gentimes start=-180 | bin span=1mon endtime | stats count by endtime | eval month=strftime(endtime, "%B") | table month | tail 2 | tail 1</query>
          <earliest>@d</earliest>
          <latest>now</latest>
        </search>
        <fieldForLabel>month</fieldForLabel>
        <fieldForValue>month</fieldForValue>
      </input>
    </panel>
  </row>
</form>

gabriel_vasseur
Contributor

Awesome! I didn't know gentimes.

I was wondering why you put April as an explicit default in the second search. After experimenting, I believe the default causes splunk to ignore the results of the second search, defeating the purpose. Removing the default doesn't work either: by removing the "depends" clause, I could see the radio button was correct but was not selected. However in my earlier searches online I had heard of "selectFirstChoice" and that worked. So in conclusion, your solution is perfect when:

<default>April</default>

Is replaced with:

<selectFirstChoice>true</selectFirstChoice>

Thanks a lot for taking the time to help, much appreciated.

0 Karma

avneet26
Engager

this code didn't work for me. when I am selecting the month from the drop down i dont see any changes happening in my dashboard. 

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!

Can’t Make It to Boston? Stream .conf25 and Learn with Haya Husain

Boston may be buzzing this September with Splunk University and .conf25, but you don’t have to pack a bag to ...

Splunk Lantern’s Guide to The Most Popular .conf25 Sessions

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...

Unlock What’s Next: The Splunk Cloud Platform at .conf25

In just a few days, Boston will be buzzing as the Splunk team and thousands of community members come together ...