# Calculate difference from row values instead columns

**URL:** <https://discuss.elastic.co/t/calculate-difference-from-row-values-instead-columns/143978>\
**Category:** Logstash\
**Created:** [August 11, 2018, 2:32pm UTC](https://discuss.elastic.co/t/calculate-difference-from-row-values-instead-columns/143978 "2018-08-11T14:32:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rkhapre](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rkhapre/32/48333_2.png) [@rkhapre](https://discuss.elastic.co/u/rkhapre)\
**Post date:** [August 11, 2018, 2:32pm UTC](https://discuss.elastic.co/t/calculate-difference-from-row-values-instead-columns/143978/1 "2018-08-11T14:32:13Z")

</div>

Hi All

How can i Calculate difference from row values instead columns. Below is the example

Below Data is available in csv file

> | Month | Amount Type | Value |
> | --- | --- | --- |
> | Jan | Type A | 150 |
> | Jan | Type B | 100 |
> | Jan | Type C | 20 |
> | Feb | Type A | 200 |
> | Feb | Type C | 20 |
> | Feb | Type B | 100 |

Now I want to calculate difference in Jan and Feb month for Type A and Type B amount Type and the expected result is

> | Month | Type | Difference |
> | --- | --- | --- |
> | Jan | Type A | 50 |
> | Feb | Type B | 0 |

This come with the calculation  
In Jan and Feb Type A value is 150 and 200 and its variance with respect to Jan is (200-150)=50

Similarly  
In Jan and Feb Type B value is 100 and 100 and its variance with respect to Jan is (100-100)=0

How can i achieve this ?

i tried something using ruby filter and transpose the data, with that also no luck.  
I think i am missing something here,if this would have been sql query we would have added the Amount type as filter and do the difference, how we will achieve this with pipeline

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [August 11, 2018, 3:47pm UTC](https://discuss.elastic.co/t/calculate-difference-from-row-values-instead-columns/143978/2 "2018-08-11T15:47:19Z")

</div>

Take a look at the aggregate filter. Your use case is similar to example 1.

Aggregate based on Amount Type. For Jan, do the task start and save the value in the map (and drop the event). For Feb calculate the difference by retrieving the Jan value from the map and then event.set the difference, and set end\_of\_task.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 8, 2018, 3:47pm UTC](https://discuss.elastic.co/t/calculate-difference-from-row-values-instead-columns/143978/3 "2018-09-08T15:47:20Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
