<?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 can I calculate data in sub groups? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-calculate-data-in-sub-groups/m-p/408766#M117984</link>
    <description>&lt;P&gt;Hi @juliaester03  ,&lt;/P&gt;

&lt;P&gt;Did you have a chance to check out some answers? If it worked, please resolve this post by approving it! If your problem is still not solved, keep us updated so that someone else can help you. &lt;/P&gt;

&lt;P&gt;Thanks for posting!&lt;/P&gt;</description>
    <pubDate>Mon, 17 Jun 2019 22:40:23 GMT</pubDate>
    <dc:creator>evania</dc:creator>
    <dc:date>2019-06-17T22:40:23Z</dc:date>
    <item>
      <title>How can I calculate data in sub groups?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-calculate-data-in-sub-groups/m-p/408761#M117979</link>
      <description>&lt;P&gt;Hello all,&lt;/P&gt;

&lt;P&gt;I have a question regarding a calculation for the stock. &lt;/P&gt;

&lt;P&gt;My table has three coloums: ISIN, price and timestamp. Every event gives a new price for an arbitrary ISIN. The task is to find out which ISIN has a price change over a given threshold (e.g. 5%) in the last two events for this ISIN.&lt;/P&gt;

&lt;P&gt;I want to build an alert if the price change of an ISIN exceeds the threshold in two sequenced events which belong to each ISIN.  &lt;/P&gt;

&lt;P&gt;I have tried the following command: &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;sourcetype="stock_data.csv" | top 2 _time by ISIN | stats count by ISIN
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;But I have no idea how to calculate the two newest prices for one ISIN and to compare it with a given threshold.&lt;/P&gt;

&lt;P&gt;Many thanks in advance!&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jun 2019 16:18:45 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-calculate-data-in-sub-groups/m-p/408761#M117979</guid>
      <dc:creator>juliaester03</dc:creator>
      <dc:date>2019-06-05T16:18:45Z</dc:date>
    </item>
    <item>
      <title>Re: How can I calculate data in sub groups?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-calculate-data-in-sub-groups/m-p/408762#M117980</link>
      <description>&lt;P&gt;@juliaester03 "last two events for ISIN" and "top two events for ISIN" may imply different things. I am assuming it is the first that you need.&lt;/P&gt;

&lt;P&gt;Using &lt;CODE&gt;| dedup 2 ISIN&lt;/CODE&gt; you can retain latest two events for each ISIN.  Using &lt;CODE&gt;streamstats&lt;/CODE&gt; with &lt;CODE&gt;window=1 current=f&lt;/CODE&gt;, on applying &lt;CODE&gt;last()&lt;/CODE&gt; aggregation gives us the previous value of ISIN which can be used for calculating the percent change.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;sourcetype="stock_data.csv"
| fields _time ISIN price
| dedup 2 ISIN
| reverse
| streamstats last(price) as lastPrice by ISIN window=1 current=f
| eval perc=round(((price-lastPrice)/price)*100,2)
| search perc&amp;gt;5
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Following is a run anywhere example based on the detailed provided in the question&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults
| eval data="time=\"2019/06/05 01:00:00\",ISIN=\"A\",price=50;time=\"2019/06/05 02:00:00\",ISIN=\"A\",price=60;time=\"2019/06/05 03:00:00\",ISIN=\"A\",price=100;time=\"2019/06/05 01:00:00\",ISIN=\"B\",price=50;time=\"2019/06/05 02:00:00\",ISIN=\"B\",price=51"
| makemv data delim=";"
| mvexpand data
| rename data as _raw
| KV
| eval _time=strptime(time,"%Y/%m/%d %H:%M:%S")
| sort - _time
| fields _time ISIN price
| dedup 2 ISIN
| reverse
| streamstats last(price) as lastPrice by ISIN window=1 current=f
| eval perc=round(((price-lastPrice)/price)*100,2)
| search perc&amp;gt;5
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 05 Jun 2019 17:19:42 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-calculate-data-in-sub-groups/m-p/408762#M117980</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2019-06-05T17:19:42Z</dc:date>
    </item>
    <item>
      <title>Re: How can I calculate data in sub groups?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-calculate-data-in-sub-groups/m-p/408763#M117981</link>
      <description>&lt;P&gt;@juliaester03 Try something similar -&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;sourcetype="stock_data.csv" | sort 0 - _time|streamstats count as id by ISIN | where id &amp;lt;=2 | eval diff=( price - threshold )/threshold * 100| where diff &amp;gt; 5
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 05 Jun 2019 17:23:36 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-calculate-data-in-sub-groups/m-p/408763#M117981</guid>
      <dc:creator>Vijeta</dc:creator>
      <dc:date>2019-06-05T17:23:36Z</dc:date>
    </item>
    <item>
      <title>Re: How can I calculate data in sub groups?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-calculate-data-in-sub-groups/m-p/408764#M117982</link>
      <description>&lt;P&gt;Dear @niketnilay ,&lt;/P&gt;

&lt;P&gt;many thanks for your fast and very detailed answer!!! The first try worked out really well. I will do some testing now. Maybe I come back to you with some more questions. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; &lt;/P&gt;</description>
      <pubDate>Fri, 07 Jun 2019 16:57:19 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-calculate-data-in-sub-groups/m-p/408764#M117982</guid>
      <dc:creator>juliaester03</dc:creator>
      <dc:date>2019-06-07T16:57:19Z</dc:date>
    </item>
    <item>
      <title>Re: How can I calculate data in sub groups?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-calculate-data-in-sub-groups/m-p/408765#M117983</link>
      <description>&lt;P&gt;Dear @Vijeta ,&lt;/P&gt;

&lt;P&gt;many thanks for answering me so fast!!  &lt;/P&gt;</description>
      <pubDate>Fri, 07 Jun 2019 16:59:47 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-calculate-data-in-sub-groups/m-p/408765#M117983</guid>
      <dc:creator>juliaester03</dc:creator>
      <dc:date>2019-06-07T16:59:47Z</dc:date>
    </item>
    <item>
      <title>Re: How can I calculate data in sub groups?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-calculate-data-in-sub-groups/m-p/408766#M117984</link>
      <description>&lt;P&gt;Hi @juliaester03  ,&lt;/P&gt;

&lt;P&gt;Did you have a chance to check out some answers? If it worked, please resolve this post by approving it! If your problem is still not solved, keep us updated so that someone else can help you. &lt;/P&gt;

&lt;P&gt;Thanks for posting!&lt;/P&gt;</description>
      <pubDate>Mon, 17 Jun 2019 22:40:23 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-calculate-data-in-sub-groups/m-p/408766#M117984</guid>
      <dc:creator>evania</dc:creator>
      <dc:date>2019-06-17T22:40:23Z</dc:date>
    </item>
  </channel>
</rss>

