<?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: Can you help me with the following conditional streamstats issue? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Can-you-help-me-with-the-following-conditional-streamstats-issue/m-p/396767#M167314</link>
    <description>&lt;P&gt;Hi Renjith!&lt;BR /&gt;
I'm getting somewhere, but not there yet.&lt;BR /&gt;
I managed to zero the UnitPrice as per picture below, but what I need to do now is to copy the last valid UnitPrice (0.00000576) across to where UnitPrice is 0.&lt;BR /&gt;
So whenever BuyT and SellT = 0, copy the last valid UnitPrice.&lt;/P&gt;

&lt;P&gt;Does that make sense?&lt;BR /&gt;
Thanks! &lt;/P&gt;

&lt;P&gt;&lt;A href="https://answers.splunk.comstorage/temp/255094-ritesh.jpg"&gt;alt text&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 04 Oct 2018 01:51:43 GMT</pubDate>
    <dc:creator>ADRIANODL</dc:creator>
    <dc:date>2018-10-04T01:51:43Z</dc:date>
    <item>
      <title>Can you help me with the following conditional streamstats issue?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Can-you-help-me-with-the-following-conditional-streamstats-issue/m-p/396762#M167309</link>
      <description>&lt;P&gt;Hi splunkers,&lt;/P&gt;

&lt;P&gt;Suppose I have the following table:&lt;/P&gt;

&lt;P&gt;Date ItemsPurchased UnitPrice&lt;BR /&gt;
1/1/1111 20 0.5 &lt;BR /&gt;
2/1/1111 10 1 &lt;BR /&gt;
3/1/1111 -7 0 &lt;BR /&gt;
4/1/1111 8 0.2&lt;/P&gt;

&lt;P&gt;Which is basically a representation of my stock, where the -7 means that 7 items have been sold.&lt;/P&gt;

&lt;P&gt;So now I want to calculate the Median Unit Price, which I do by using the following query:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| streamstats sum(ItemsPurchased) as GTotal |streamstats sum(eval(ItemsPurchased*UnitPrice)) as UTotal  |eval MedianUnitPrice= UTotal / GTotal  |table date ItemsPurchased UnitPrice GTotal  UTotal MedianUnitPrice
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This works fine, calculating the MedianUnitPrice as required, HOWEVER, it also tries to calculate it for my Sale (-7), which skews the results thereon.. &lt;/P&gt;

&lt;P&gt;Date ItemsPurchased UnitPrice MedianUnitPrice&lt;BR /&gt;
1/1/1111 20 0.5 0.5&lt;BR /&gt;
2/1/1111 10 1 0.6667&lt;BR /&gt;
3/1/1111 -7 0 (-0.475)&lt;BR /&gt;
4/1/1111 8 0.2 (wrong result since it's adding -0.475 to the calculation)&lt;/P&gt;

&lt;P&gt;What I'd like to do is to keep calculating the MedianUnitPrice  EXCEPT when ItemsPurchased is a negative value.&lt;/P&gt;

&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Tue, 02 Oct 2018 01:55:06 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Can-you-help-me-with-the-following-conditional-streamstats-issue/m-p/396762#M167309</guid>
      <dc:creator>ADRIANODL</dc:creator>
      <dc:date>2018-10-02T01:55:06Z</dc:date>
    </item>
    <item>
      <title>Re: Can you help me with the following conditional streamstats issue?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Can-you-help-me-with-the-following-conditional-streamstats-issue/m-p/396763#M167310</link>
      <description>&lt;P&gt;@ADRIANODL,&lt;/P&gt;

&lt;P&gt;Try adding this to your search &lt;CODE&gt;sum(eval(if(ItemsPurchased&amp;lt;0,0,ItemsPurchased)*UnitPrice)) as UTotal&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;You might need to do it for GTotal as well if you are only considering purchases and not the stock&lt;/P&gt;</description>
      <pubDate>Tue, 02 Oct 2018 09:47:46 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Can-you-help-me-with-the-following-conditional-streamstats-issue/m-p/396763#M167310</guid>
      <dc:creator>renjith_nair</dc:creator>
      <dc:date>2018-10-02T09:47:46Z</dc:date>
    </item>
    <item>
      <title>Re: Can you help me with the following conditional streamstats issue?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Can-you-help-me-with-the-following-conditional-streamstats-issue/m-p/396764#M167311</link>
      <description>&lt;P&gt;Hi Renjith, please see picture below.&lt;/P&gt;</description>
      <pubDate>Tue, 02 Oct 2018 11:39:05 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Can-you-help-me-with-the-following-conditional-streamstats-issue/m-p/396764#M167311</guid>
      <dc:creator>ADRIANODL</dc:creator>
      <dc:date>2018-10-02T11:39:05Z</dc:date>
    </item>
    <item>
      <title>Re: Can you help me with the following conditional streamstats issue?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Can-you-help-me-with-the-following-conditional-streamstats-issue/m-p/396765#M167312</link>
      <description>&lt;P&gt;Hi Renjith, I had a similar query to the one you suggested, but thanks anyways.&lt;BR /&gt;
what's actually happening here is that the MedianUnitPrice is calculating fine, until I hit the sale transaction (ItemsPurchased is a negative value). &lt;BR /&gt;
What I'd like it to do is for the MedianUnitPrice to be the same as the one above if ItemsPurchased&amp;lt;0&lt;BR /&gt;
On the example below where MedianUnitPrice=0, it should actually be 0.05959825, but I don't know how to prevent it from being recalculated. Any ideas?&lt;/P&gt;

&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="alt text"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/5842iA553337738E7AE4F/image-size/large?v=v2&amp;amp;px=999" role="button" title="alt text" alt="alt text" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Oct 2018 11:45:33 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Can-you-help-me-with-the-following-conditional-streamstats-issue/m-p/396765#M167312</guid>
      <dc:creator>ADRIANODL</dc:creator>
      <dc:date>2018-10-02T11:45:33Z</dc:date>
    </item>
    <item>
      <title>Re: Can you help me with the following conditional streamstats issue?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Can-you-help-me-with-the-following-conditional-streamstats-issue/m-p/396766#M167313</link>
      <description>&lt;P&gt;@ADRIANODL,&lt;/P&gt;

&lt;P&gt;From your search, MedianUnitPrice  is calculated from UTotal / GTotal . So we need to fix UTotal as mentioned above. If you do not want the condition applied for UTotal , then make it as a separate variable and calculate median from that. I am trying with the below run anywhere example and you could see that if the value of unit is &amp;lt;0, it just takes the last value. Let me know what difference you need.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;|makeresults count=5|eval unit=1|accum unit|eval unit=if(unit%2==0,unit*-1,unit) |streamstats sum(eval(if(unit&amp;lt;0,0,unit)*1)) as sum
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 03 Oct 2018 17:34:24 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Can-you-help-me-with-the-following-conditional-streamstats-issue/m-p/396766#M167313</guid>
      <dc:creator>renjith_nair</dc:creator>
      <dc:date>2018-10-03T17:34:24Z</dc:date>
    </item>
    <item>
      <title>Re: Can you help me with the following conditional streamstats issue?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Can-you-help-me-with-the-following-conditional-streamstats-issue/m-p/396767#M167314</link>
      <description>&lt;P&gt;Hi Renjith!&lt;BR /&gt;
I'm getting somewhere, but not there yet.&lt;BR /&gt;
I managed to zero the UnitPrice as per picture below, but what I need to do now is to copy the last valid UnitPrice (0.00000576) across to where UnitPrice is 0.&lt;BR /&gt;
So whenever BuyT and SellT = 0, copy the last valid UnitPrice.&lt;/P&gt;

&lt;P&gt;Does that make sense?&lt;BR /&gt;
Thanks! &lt;/P&gt;

&lt;P&gt;&lt;A href="https://answers.splunk.comstorage/temp/255094-ritesh.jpg"&gt;alt text&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 04 Oct 2018 01:51:43 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Can-you-help-me-with-the-following-conditional-streamstats-issue/m-p/396767#M167314</guid>
      <dc:creator>ADRIANODL</dc:creator>
      <dc:date>2018-10-04T01:51:43Z</dc:date>
    </item>
    <item>
      <title>Re: Can you help me with the following conditional streamstats issue?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Can-you-help-me-with-the-following-conditional-streamstats-issue/m-p/396768#M167315</link>
      <description>&lt;P&gt;Hi  Renjith, please see comment below with picture.&lt;/P&gt;</description>
      <pubDate>Thu, 04 Oct 2018 01:52:07 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Can-you-help-me-with-the-following-conditional-streamstats-issue/m-p/396768#M167315</guid>
      <dc:creator>ADRIANODL</dc:creator>
      <dc:date>2018-10-04T01:52:07Z</dc:date>
    </item>
    <item>
      <title>Re: Can you help me with the following conditional streamstats issue?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Can-you-help-me-with-the-following-conditional-streamstats-issue/m-p/396769#M167316</link>
      <description>&lt;P&gt;Hi Renjith,&lt;BR /&gt;
I managed to solve this one using the filldown function. Thank you very much anyways.&lt;/P&gt;</description>
      <pubDate>Thu, 04 Oct 2018 04:38:28 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Can-you-help-me-with-the-following-conditional-streamstats-issue/m-p/396769#M167316</guid>
      <dc:creator>ADRIANODL</dc:creator>
      <dc:date>2018-10-04T04:38:28Z</dc:date>
    </item>
  </channel>
</rss>

