<?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 Calculate on one field in multiple events in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Calculate-on-one-field-in-multiple-events/m-p/391907#M172939</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;I collect json data like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;{"timestamp":"2019.02.19-10:20:30","label":"xxx","size":"100"}
{"timestamp":"2019.02.19-10:20:30","label":"yyy","size":"250"}
{"timestamp":"2019.02.19-10:20:30","label":"zzz","size":"300"}

{"timestamp":"2019.02.20-10:20:30","label":"xxx","size":"400"}
{"timestamp":"2019.02.20-10:20:30","label":"yyy","size":"500"}
{"timestamp":"2019.02.20-10:20:30","label":"zzz","size":"900"}
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;and I would like to calculate difference between the two sizes of the same label, i.e.:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;+-------+--------+--------+-----------+
| label | size_1 | size_2 | diff_size |
+-------+--------+--------+-----------+
| xxx   | 100    | 400    | 300       |
+-------+--------+--------+-----------+
| yyy   | 250    | 500    | 250       |
+-------+--------+--------+-----------+
| zzz   | 300    | 900    | 600       |
+-------+--------+--------+-----------+
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I tried many methods (chart over by, xqueries, subsearches, etc) but I never get this result &lt;span class="lia-unicode-emoji" title=":confused_face:"&gt;😕&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Wed, 20 Feb 2019 09:53:18 GMT</pubDate>
    <dc:creator>JuGuSm</dc:creator>
    <dc:date>2019-02-20T09:53:18Z</dc:date>
    <item>
      <title>Calculate on one field in multiple events</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Calculate-on-one-field-in-multiple-events/m-p/391907#M172939</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;I collect json data like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;{"timestamp":"2019.02.19-10:20:30","label":"xxx","size":"100"}
{"timestamp":"2019.02.19-10:20:30","label":"yyy","size":"250"}
{"timestamp":"2019.02.19-10:20:30","label":"zzz","size":"300"}

{"timestamp":"2019.02.20-10:20:30","label":"xxx","size":"400"}
{"timestamp":"2019.02.20-10:20:30","label":"yyy","size":"500"}
{"timestamp":"2019.02.20-10:20:30","label":"zzz","size":"900"}
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;and I would like to calculate difference between the two sizes of the same label, i.e.:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;+-------+--------+--------+-----------+
| label | size_1 | size_2 | diff_size |
+-------+--------+--------+-----------+
| xxx   | 100    | 400    | 300       |
+-------+--------+--------+-----------+
| yyy   | 250    | 500    | 250       |
+-------+--------+--------+-----------+
| zzz   | 300    | 900    | 600       |
+-------+--------+--------+-----------+
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I tried many methods (chart over by, xqueries, subsearches, etc) but I never get this result &lt;span class="lia-unicode-emoji" title=":confused_face:"&gt;😕&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Feb 2019 09:53:18 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Calculate-on-one-field-in-multiple-events/m-p/391907#M172939</guid>
      <dc:creator>JuGuSm</dc:creator>
      <dc:date>2019-02-20T09:53:18Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate on one field in multiple events</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Calculate-on-one-field-in-multiple-events/m-p/391908#M172940</link>
      <description>&lt;P&gt;You could try something like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;...your search to get to the data...
| stats range(size) as diff_size earliest(size) as size_1 latest(size) as size_2 by label
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 20 Feb 2019 10:02:51 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Calculate-on-one-field-in-multiple-events/m-p/391908#M172940</guid>
      <dc:creator>FrankVl</dc:creator>
      <dc:date>2019-02-20T10:02:51Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate on one field in multiple events</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Calculate-on-one-field-in-multiple-events/m-p/391909#M172941</link>
      <description>&lt;P&gt;Try this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults 
| eval "timestamp"="2019.02.19-10:20:30","label"="xxx","size"="100" 
| append 
    [ makeresults 
    | eval 
        "timestamp"="2019.02.19-10:20:30","label"="yyy","size"="250"] 
| append 
    [ makeresults 
    | eval
        "timestamp"="2019.02.19-10:20:30","label"="zzz","size"="300"] 
| append 
    [ makeresults 
    | eval "timestamp"="2019.02.20-10:20:30","label"="xxx","size"="400"] 
| append 
    [ makeresults 
    | eval "timestamp"="2019.02.20-10:20:30","label"="yyy","size"="500"] 
| append 
    [ makeresults 
    | eval 
        "timestamp"="2019.02.20-10:20:30","label"="zzz","size"="900"] 
| fields - _time timestamp
| stats  first(size) as size_1 last(size) as size_2 range(size) as diff_size by label
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 20 Feb 2019 10:17:21 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Calculate-on-one-field-in-multiple-events/m-p/391909#M172941</guid>
      <dc:creator>ashajambagi</dc:creator>
      <dc:date>2019-02-20T10:17:21Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate on one field in multiple events</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Calculate-on-one-field-in-multiple-events/m-p/391910#M172942</link>
      <description>&lt;P&gt;That's great! It is almost what I want but sometimes size_1 is greater than size_2 and I need to have size_1 equal to the first value and size_2 to the last &lt;span class="lia-unicode-emoji" title=":confused_face:"&gt;😕&lt;/span&gt;&lt;/P&gt;

&lt;P&gt;In fact, I misspoke because there is 3 values and it makes thing more complicated :&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;{"timestamp":"2019.02.18-10:20:30","label":"xxx","size":"400"}
{"timestamp":"2019.02.18-10:20:30","label":"yyy","size":"250"}
{"timestamp":"2019.02.18-10:20:30","label":"zzz","size":"300"}

{"timestamp":"2019.02.19-10:20:30","label":"xxx","size":"500"}
{"timestamp":"2019.02.19-10:20:30","label":"yyy","size":"400"}
{"timestamp":"2019.02.19-10:20:30","label":"zzz","size":"900"}

{"timestamp":"2019.02.20-10:20:30","label":"xxx","size":"450"}
{"timestamp":"2019.02.20-10:20:30","label":"yyy","size":"300"}
{"timestamp":"2019.02.20-10:20:30","label":"zzz","size":"600"}

+-------+--------+--------+--------+----------+----------+
| label | size_1 | size_2 | size_3 | diff_1_2 | diff_2_3 |
+-------+--------+--------+--------+----------+----------+
| xxx   | 400    | 500    | 450    | 100      | -50      |
+-------+--------+--------+--------+----------+----------+
| yyy   | 250    | 400    | 300    | 150      | -100     |
+-------+--------+--------+--------+----------+----------+
| zzz   | 300    | 900    | 600    | 600      | -300     |
+-------+--------+--------+--------+----------+----------+
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Sep 2020 23:18:31 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Calculate-on-one-field-in-multiple-events/m-p/391910#M172942</guid>
      <dc:creator>JuGuSm</dc:creator>
      <dc:date>2020-09-29T23:18:31Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate on one field in multiple events</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Calculate-on-one-field-in-multiple-events/m-p/391911#M172943</link>
      <description>&lt;P&gt;Thank you very much for reply. Are first() and last() different from earliest() and latest()? Because it can be very usefull in this case but I'm not sure because it seems to generate the same result than FrankVl's answer.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Feb 2019 10:53:12 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Calculate-on-one-field-in-multiple-events/m-p/391911#M172943</guid>
      <dc:creator>JuGuSm</dc:creator>
      <dc:date>2019-02-20T10:53:12Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate on one field in multiple events</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Calculate-on-one-field-in-multiple-events/m-p/391912#M172944</link>
      <description>&lt;P&gt;first() and last() depend on how the events were sorted before the stats command was invoked. Earliest and latest always return the earliest and latest based on _time.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Feb 2019 11:45:26 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Calculate-on-one-field-in-multiple-events/m-p/391912#M172944</guid>
      <dc:creator>FrankVl</dc:creator>
      <dc:date>2019-02-20T11:45:26Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate on one field in multiple events</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Calculate-on-one-field-in-multiple-events/m-p/391913#M172945</link>
      <description>&lt;P&gt;That indeed makes it a bit more complicated.&lt;/P&gt;

&lt;P&gt;You could try:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;...
| stats earliest(size) as size_1 list(size) as size_2 latest(size) as size_3 by label
| eval size_2 = mvindex(size_2, 1)
| eval diff_1_2 = size_2 - size_1
| eval diff_2_3 = size_3 - size_2
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;list(size) will return a multivalued field with all 3 sizes, in the original order. Using the mvindex you then pick the second (index starts at 0) value. Then you can calculate the two diffs.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Feb 2019 12:08:16 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Calculate-on-one-field-in-multiple-events/m-p/391913#M172945</guid>
      <dc:creator>FrankVl</dc:creator>
      <dc:date>2019-02-20T12:08:16Z</dc:date>
    </item>
  </channel>
</rss>

