<?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: Calculate time and doubling it from the user selection in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Calculate-time-and-doubling-it-from-the-user-selection/m-p/253933#M189380</link>
    <description>&lt;P&gt;Try this in you search/query (MY QUERY)&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;  base search [
  | makeresults 
  | eval earliest=if(isnum($field1.earliest$), $field1.earliest$, relative_time(now(), "$field1.earliest$")) 
  | eval latest=if(isnum($field1.latest$), $field1.latest$, relative_time(now(), "$field1.latest$"))  
  | eval span=latest-earliest
  | eval earliest=earliest-span 
  | table earliest latest ] | rest of your query
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;And here is rest of your query with calculation for mid&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;  | eval mid=$field1.earliest$
  | eval when=if(_time&amp;gt;relative_time(now(), mid), "Current_Period", "Prev_Period") 
  | stats count as events by  source when 
  | chart sum(events) by source, when 
  | eval perc = (Current_Period-Prev_Period)/Prev_Period
  | eval trend = case(perc &amp;lt; -0.3, "low", (perc &amp;gt;= -0.3 and perc &amp;lt;= 0.3 ), "medium", perc &amp;gt; 0.3, "high")
  | table source, Current_Period, Prev_Period, perc, trend
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 31 Aug 2016 16:59:27 GMT</pubDate>
    <dc:creator>sundareshr</dc:creator>
    <dc:date>2016-08-31T16:59:27Z</dc:date>
    <item>
      <title>Calculate time and doubling it from the user selection</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Calculate-time-and-doubling-it-from-the-user-selection/m-p/253932#M189379</link>
      <description>&lt;P&gt;Hi all.&lt;BR /&gt;
I have a normal time selector in splunk that I think that everybody know.&lt;/P&gt;

&lt;P&gt;&lt;IMG src="https://docs.splunk.com/images/3/3f/6.2_timerange_presets.png" alt="alt text" /&gt;&lt;/P&gt;

&lt;P&gt;I noticed that in my dashboard it gets used in the following way:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;lt;search&amp;gt;
    &amp;lt;query&amp;gt;MY QUERY&amp;lt;/query&amp;gt;
    &amp;lt;earliest&amp;gt;$field1.earliest$&amp;lt;/earliest&amp;gt;
    &amp;lt;latest&amp;gt;$field1.latest$&amp;lt;/latest&amp;gt;
&amp;lt;/search&amp;gt;
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Now, what I want to do is to double the time range selected by the user.&lt;/P&gt;

&lt;P&gt;For instance, if the user select 1 week, I want to pick 2 weeks.&lt;BR /&gt;
Same thing for days, months, hours and any time range .&lt;/P&gt;

&lt;P&gt;If the user pick some strange period (es: from 1st January to 21 February ) I want to maintain the closest selection (21 February ) and double the chosen time.&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
&lt;P&gt;1st January to 21 February = 51 days&lt;/P&gt;

&lt;P&gt;51 * 2 = 102&lt;/P&gt;

&lt;P&gt;21 February  - 102 days = 11 November&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;How can I do this in my code.&lt;BR /&gt;
I'm also willing to trasform my dashboard in HTML (I think I'll do it anyway later).&lt;/P&gt;

&lt;P&gt;Thanks a lot!&lt;/P&gt;

&lt;HR /&gt;

&lt;P&gt;Thank you a lot Sundareshr!&lt;/P&gt;

&lt;P&gt;I tried to implement your solution but for some reason it does not work.&lt;BR /&gt;
the code is the following:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;BASE QUERY [
 | makeresults 
 | eval earliest=if(isnum($field1.earliest$), $field1.earliest$, relative_time(now(), "$field1.earliest$") 
 | eval latest=if(isnum($field1.latest$), $field1.latest$, relative_time(now(), "$field1.latest$")  
 | eval span=latest-earliest
 | eval mid=earliest
 | eval earliest=earliest-span 
 | table earliest latest mid] 
 | eval when=if(_time&amp;gt;relative_time(now(), mid), "Current_Period", "Prev_Period") 
 | stats count as events by  source when 
 | chart sum(events) by source, when 
 | eval perc = (Current_Period-Prev_Period)/Prev_Period
 | eval trend = case(perc &amp;lt; -0.3, "low", (perc &amp;gt;= -0.3 and perc &amp;lt;= 0.3 ), "madium", perc &amp;gt; 0.3, "high")
 | table source, Current_Period, Prev_Period, perc, trend
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;It shows me the following error  &lt;/P&gt;

&lt;BLOCKQUOTE&gt;
&lt;P&gt;Error in 'eval' command: The expression is malformed. Expected ).&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;in your part of the code I changed this &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;eval span=latest=earliest
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;to this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;eval span=latest-earliest
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Because I thought it was a typo &lt;/P&gt;

&lt;P&gt;Thanks a lot again&lt;/P&gt;

&lt;HR /&gt;

&lt;P&gt;There is something wrong with the formatting that te selector gives to the variable.&lt;/P&gt;

&lt;P&gt;If i chose "from the beginning of the week" i get thios error:&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
&lt;P&gt;Error in 'eval' command: The expression is malformed. An unexpected character is reached at '@w1), @w1, relative_time(now(), "@w1"))'.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;for the month is like this:&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
&lt;P&gt;Error in 'eval' command: The expression is malformed. An unexpected character is reached at '@mon), @mon, relative_time(now(), "@mon"))'.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;if i choose always is like this:&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
&lt;P&gt;Error in 'eval' command: The expression is malformed. An unexpected character is reached at ', relative_time(now(), ""))'.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;if i choose between 2 dates is like this:&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
&lt;P&gt;Error in 'search' command: Unable to parse the search: 'AND' operator is missing a clause on the left hand side.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;if i choose last 7 days is like this&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
&lt;P&gt;Error in 'eval' command: The expression is malformed. Expected ).&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Tue, 30 Aug 2016 07:13:48 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Calculate-time-and-doubling-it-from-the-user-selection/m-p/253932#M189379</guid>
      <dc:creator>andreafebbo</dc:creator>
      <dc:date>2016-08-30T07:13:48Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate time and doubling it from the user selection</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Calculate-time-and-doubling-it-from-the-user-selection/m-p/253933#M189380</link>
      <description>&lt;P&gt;Try this in you search/query (MY QUERY)&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;  base search [
  | makeresults 
  | eval earliest=if(isnum($field1.earliest$), $field1.earliest$, relative_time(now(), "$field1.earliest$")) 
  | eval latest=if(isnum($field1.latest$), $field1.latest$, relative_time(now(), "$field1.latest$"))  
  | eval span=latest-earliest
  | eval earliest=earliest-span 
  | table earliest latest ] | rest of your query
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;And here is rest of your query with calculation for mid&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;  | eval mid=$field1.earliest$
  | eval when=if(_time&amp;gt;relative_time(now(), mid), "Current_Period", "Prev_Period") 
  | stats count as events by  source when 
  | chart sum(events) by source, when 
  | eval perc = (Current_Period-Prev_Period)/Prev_Period
  | eval trend = case(perc &amp;lt; -0.3, "low", (perc &amp;gt;= -0.3 and perc &amp;lt;= 0.3 ), "medium", perc &amp;gt; 0.3, "high")
  | table source, Current_Period, Prev_Period, perc, trend
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 31 Aug 2016 16:59:27 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Calculate-time-and-doubling-it-from-the-user-selection/m-p/253933#M189380</guid>
      <dc:creator>sundareshr</dc:creator>
      <dc:date>2016-08-31T16:59:27Z</dc:date>
    </item>
  </channel>
</rss>

