<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to fetch FY columns based on current year in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-fetch-FY-columns-based-on-current-year/m-p/349723#M159544</link>
    <description>&lt;P&gt;The following works fine for me when importing your sample above as a CSV file (ignore the first line basically):&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| inputcsv mycsv2.csv
| addinfo
| eval current_year = strftime(if(isNum(info_max_time), info_max_time, now()), "%y"), previous_year = current_year - 1
| eval current_year = "FY" . current_year, previous_year = "FY" . previous_year
| foreach FY* [ eval &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt; = if("&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;" = current_year OR "&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;" = previous_year, &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;, null()) ]
| table ID, FY*
| stats values(*) as * by ID
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Output:&lt;/P&gt;

&lt;P&gt;&lt;IMG src="http://i.imgur.com/lolAX0S.png" alt="alt text" /&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 03 Aug 2017 13:42:18 GMT</pubDate>
    <dc:creator>javiergn</dc:creator>
    <dc:date>2017-08-03T13:42:18Z</dc:date>
    <item>
      <title>How to fetch FY columns based on current year</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-fetch-FY-columns-based-on-current-year/m-p/349720#M159541</link>
      <description>&lt;P&gt;I have a data set with columns FY15, FY16, FY17 and say FY18, now based  on time of execution of query i need to fetch current &amp;amp; previous years columns.&lt;BR /&gt;
For ex. i will run the query today so it should fetch me FY16 &amp;amp; FY17 columns(i.e., table FY16 FY17) as previous &amp;amp; current years data and if i run the query next year it should fetch me FY17 &amp;amp; FY18 (i.e., table FY17 FY18) as previous &amp;amp; current years.&lt;/P&gt;

&lt;P&gt;It should be dynamic to fetch data in future for FY19, FY20 and so on.&lt;/P&gt;

&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Thu, 03 Aug 2017 09:08:48 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-fetch-FY-columns-based-on-current-year/m-p/349720#M159541</guid>
      <dc:creator>amitca</dc:creator>
      <dc:date>2017-08-03T09:08:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to fetch FY columns based on current year</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-fetch-FY-columns-based-on-current-year/m-p/349721#M159542</link>
      <description>&lt;P&gt;Hi, without really knowing how your data looks like it's hard to come up with a final and valid answer but see if the below helps anyway:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;your query here
| addinfo
| eval current_year = strftime(if(isNum(info_max_time), info_max_time, now()), "%y"), previous_year = current_year - 1
| eval current_year = "FY" . current_year, previous_year = "FY" . previous_year
| foreach FY* [ eval &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt; = if("&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;" = current_year OR "&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;" = previous_year, &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;, null()) ]
| table _raw, FY*
| stats values(*) as * by _raw
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Addinfo will add information about your search, including the time range you were using.&lt;BR /&gt;
The next two lines are simply calculating current and previous years based on the info from your time range.&lt;BR /&gt;
Foreach will basically populate as null() all those FY columns whose name does not match current or previous&lt;BR /&gt;
And then stats values will get rid of the null columns.&lt;/P&gt;

&lt;P&gt;If the above doesn't match your exact needs please post an example of how your data looks like.&lt;BR /&gt;
I have tested it in my lab with a small subset of data and it seems to do the trick.&lt;/P&gt;

&lt;P&gt;Thanks,&lt;BR /&gt;
J&lt;/P&gt;</description>
      <pubDate>Thu, 03 Aug 2017 10:03:33 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-fetch-FY-columns-based-on-current-year/m-p/349721#M159542</guid>
      <dc:creator>javiergn</dc:creator>
      <dc:date>2017-08-03T10:03:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to fetch FY columns based on current year</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-fetch-FY-columns-based-on-current-year/m-p/349722#M159543</link>
      <description>&lt;P&gt;Thanks it did help, but i need to display only current and previous years data and not other FY years even with empty values.&lt;BR /&gt;
Ex: Data Set&lt;BR /&gt;
ID  FY15           FY16 FY17    FY18&lt;BR /&gt;
1       1            0.5              0.2             1&lt;BR /&gt;
2       1            0.2              2           3&lt;BR /&gt;
3       2               2             2.5             3&lt;BR /&gt;
4       3               3             3           2&lt;BR /&gt;
Based on current year the output should be:&lt;BR /&gt;
ID    FY16  FY17&lt;BR /&gt;
1       0.5           0.2&lt;BR /&gt;&lt;BR /&gt;
2       0.2           2&lt;BR /&gt;&lt;BR /&gt;
3        2            2.5&lt;BR /&gt;&lt;BR /&gt;
4        3            3      &lt;/P&gt;

&lt;P&gt;Thanks in advance&lt;/P&gt;</description>
      <pubDate>Thu, 03 Aug 2017 10:34:32 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-fetch-FY-columns-based-on-current-year/m-p/349722#M159543</guid>
      <dc:creator>amitca</dc:creator>
      <dc:date>2017-08-03T10:34:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to fetch FY columns based on current year</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-fetch-FY-columns-based-on-current-year/m-p/349723#M159544</link>
      <description>&lt;P&gt;The following works fine for me when importing your sample above as a CSV file (ignore the first line basically):&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| inputcsv mycsv2.csv
| addinfo
| eval current_year = strftime(if(isNum(info_max_time), info_max_time, now()), "%y"), previous_year = current_year - 1
| eval current_year = "FY" . current_year, previous_year = "FY" . previous_year
| foreach FY* [ eval &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt; = if("&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;" = current_year OR "&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;" = previous_year, &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;, null()) ]
| table ID, FY*
| stats values(*) as * by ID
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Output:&lt;/P&gt;

&lt;P&gt;&lt;IMG src="http://i.imgur.com/lolAX0S.png" alt="alt text" /&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Aug 2017 13:42:18 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-fetch-FY-columns-based-on-current-year/m-p/349723#M159544</guid>
      <dc:creator>javiergn</dc:creator>
      <dc:date>2017-08-03T13:42:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to fetch FY columns based on current year</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-fetch-FY-columns-based-on-current-year/m-p/349724#M159545</link>
      <description>&lt;P&gt;Yes it did work. Thanks a lot.&lt;/P&gt;</description>
      <pubDate>Thu, 03 Aug 2017 17:14:54 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-fetch-FY-columns-based-on-current-year/m-p/349724#M159545</guid>
      <dc:creator>amitca</dc:creator>
      <dc:date>2017-08-03T17:14:54Z</dc:date>
    </item>
  </channel>
</rss>

