<?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 Compare row value against a value from the previous row in Talend Studio</title>
    <link>https://community.qlik.com/t5/Talend-Studio/Compare-row-value-against-a-value-from-the-previous-row/m-p/2284741#M58375</link>
    <description>&lt;P&gt;Sometimes it is useful to be able to compare a column against the same column in a previous row within a dataset. A simple example of this requirement might be as follows. You have a collection of orders that are sorted by Order Number and Date. An Order Number is usually unique, however there can be amendments made. You are tasked with identifying original Orders and Amended Orders. Original Orders will always be the first Order record created. Amendment Orders are always created after the Original Order.&amp;nbsp;&lt;SPAN&gt;The table below is an example of the dataset....&lt;/SPAN&gt;&lt;/P&gt; 
&lt;P&gt;&amp;nbsp;&lt;/P&gt; 
&lt;DIV class="table-responsive"&gt; 
 &lt;TABLE border="1" cellspacing="1" cellpadding="1"&gt; 
  &lt;TBODY&gt; 
   &lt;TR&gt; 
    &lt;TD&gt;Order Number&lt;/TD&gt; 
    &lt;TD&gt;Date&lt;/TD&gt; 
    &lt;TD&gt;Value&lt;/TD&gt; 
   &lt;/TR&gt; 
   &lt;TR&gt; 
    &lt;TD&gt;A123&lt;/TD&gt; 
    &lt;TD&gt;21/01/2015 10:23:45&lt;/TD&gt; 
    &lt;TD&gt;£123.40&lt;/TD&gt; 
   &lt;/TR&gt; 
   &lt;TR&gt; 
    &lt;TD&gt;A123&lt;/TD&gt; 
    &lt;TD&gt;21/01/2015 10:34:48&lt;/TD&gt; 
    &lt;TD&gt;£145.50&lt;/TD&gt; 
   &lt;/TR&gt; 
   &lt;TR&gt; 
    &lt;TD&gt;A123&lt;/TD&gt; 
    &lt;TD&gt;22/01/2015 09:21:21&lt;/TD&gt; 
    &lt;TD&gt;£151.45&lt;/TD&gt; 
   &lt;/TR&gt; 
   &lt;TR&gt; 
    &lt;TD&gt;B234&lt;/TD&gt; 
    &lt;TD&gt;21/01/2015 11:12:32&lt;/TD&gt; 
    &lt;TD&gt;£23.45&lt;/TD&gt; 
   &lt;/TR&gt; 
   &lt;TR&gt; 
    &lt;TD&gt;B234&lt;/TD&gt; 
    &lt;TD&gt;21/01/2015 11:45:54&lt;/TD&gt; 
    &lt;TD&gt;£43.65&lt;/TD&gt; 
   &lt;/TR&gt; 
  &lt;/TBODY&gt; 
 &lt;/TABLE&gt; 
&lt;/DIV&gt; 
&lt;P&gt;&amp;nbsp;&lt;/P&gt; 
&lt;P&gt;In order to identify the Amendment Orders from the Original Orders, we need to check the Order Number of the row before. If it is the same, then the current record is an Amendment Order, if it is different it is an Original Order.&lt;/P&gt; 
&lt;P&gt;&amp;nbsp;&lt;/P&gt; 
&lt;P&gt;We can do this in Talend quite simply using a tMap component and tMap variables. One of the most overlooked features of the tMap component is the fact that the tMap variables can store values between rows and they are processed in order (top down). This allows us to store values between rows and compare them with current rows. The screenshot below shows the layout of the tMap component. I will explain the expressions below the screenshot.&lt;/P&gt; 
&lt;P&gt;&amp;nbsp;&lt;/P&gt; 
&lt;P&gt;&lt;SPAN class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="CompareRowsTMap.jpg" style="width: 999px;"&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="0683p000009M06r.jpg"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/150438i137FFD3148783044/image-size/large?v=v2&amp;amp;px=999" role="button" title="0683p000009M06r.jpg" alt="0683p000009M06r.jpg" /&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;/P&gt; 
&lt;P&gt;&amp;nbsp;&lt;/P&gt; 
&lt;P&gt;&amp;nbsp;&lt;/P&gt; 
&lt;P&gt;The variable expressions are shown and explained in the table below....&lt;/P&gt; 
&lt;DIV class="table-responsive"&gt;
  Variable Expression Description 
 &lt;TABLE border="1" cellspacing="1" cellpadding="1"&gt; 
  &lt;TBODY&gt; 
   &lt;TR&gt; 
    &lt;TD&gt;order_type&lt;/TD&gt; 
    &lt;TD&gt;Var.last_order!=null &amp;amp;&amp;amp; Var.last_order.compareToIgnoreCase(row2.OrderNumber)==0&lt;BR /&gt;? "Amendment Order" : "Original Order"&amp;nbsp;&lt;/TD&gt; 
    &lt;TD&gt;This variable expression is tested first. First the "last_order" variable is checked to see if it is not null and to see if it matches the current "OrderNumber". Since the "last_order" variable has not been assigned yet for the first row, it will be null. For subsequent rows it will hold the value of the previous row. If the "OrderNumber" value matches the value assigned to "last_order" in the previous row, it means it is an "Amendment Order". Otherwise it is an "Original Order".&lt;/TD&gt; 
   &lt;/TR&gt; 
   &lt;TR&gt; 
    &lt;TD&gt;last_order&lt;/TD&gt; 
    &lt;TD&gt;row2.OrderNumber&amp;nbsp;&lt;/TD&gt; 
    &lt;TD&gt;The "last_order" variable is checked first and assigned second due to the order of processing the variables.&amp;nbsp;&lt;/TD&gt; 
   &lt;/TR&gt; 
  &lt;/TBODY&gt; 
 &lt;/TABLE&gt; 
&lt;/DIV&gt; 
&lt;P&gt;&amp;nbsp;&lt;/P&gt; 
&lt;P&gt;The result of the tMap processing the data in the table at the top is shown below.....&lt;/P&gt; 
&lt;DIV class="table-responsive"&gt; 
 &lt;TABLE border="1" cellspacing="1" cellpadding="1"&gt; 
  &lt;TBODY&gt; 
   &lt;TR&gt; 
    &lt;TD&gt;&lt;SPAN&gt;[statistics] connecting to socket on port 4007&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[statistics] connected&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;A123|2015-01-21T10:23:45|123.4|Original Order&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;A123|2015-01-21T10:34:48|145.5|Amendment Order&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;A123|2015-01-22T09:21:21|151.45|Amendment Order&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;B234|2015-01-21T11:12:32|23.45|Original Order&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;B234|2015-01-21T11:45:54|43.65|Amendment Order&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[statistics] disconnected&lt;/SPAN&gt;&lt;/TD&gt; 
   &lt;/TR&gt; 
  &lt;/TBODY&gt; 
 &lt;/TABLE&gt; 
&lt;/DIV&gt;</description>
    <pubDate>Thu, 13 Sep 2018 21:07:28 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2018-09-13T21:07:28Z</dc:date>
    <item>
      <title>Compare row value against a value from the previous row</title>
      <link>https://community.qlik.com/t5/Talend-Studio/Compare-row-value-against-a-value-from-the-previous-row/m-p/2284741#M58375</link>
      <description>&lt;P&gt;Sometimes it is useful to be able to compare a column against the same column in a previous row within a dataset. A simple example of this requirement might be as follows. You have a collection of orders that are sorted by Order Number and Date. An Order Number is usually unique, however there can be amendments made. You are tasked with identifying original Orders and Amended Orders. Original Orders will always be the first Order record created. Amendment Orders are always created after the Original Order.&amp;nbsp;&lt;SPAN&gt;The table below is an example of the dataset....&lt;/SPAN&gt;&lt;/P&gt; 
&lt;P&gt;&amp;nbsp;&lt;/P&gt; 
&lt;DIV class="table-responsive"&gt; 
 &lt;TABLE border="1" cellspacing="1" cellpadding="1"&gt; 
  &lt;TBODY&gt; 
   &lt;TR&gt; 
    &lt;TD&gt;Order Number&lt;/TD&gt; 
    &lt;TD&gt;Date&lt;/TD&gt; 
    &lt;TD&gt;Value&lt;/TD&gt; 
   &lt;/TR&gt; 
   &lt;TR&gt; 
    &lt;TD&gt;A123&lt;/TD&gt; 
    &lt;TD&gt;21/01/2015 10:23:45&lt;/TD&gt; 
    &lt;TD&gt;£123.40&lt;/TD&gt; 
   &lt;/TR&gt; 
   &lt;TR&gt; 
    &lt;TD&gt;A123&lt;/TD&gt; 
    &lt;TD&gt;21/01/2015 10:34:48&lt;/TD&gt; 
    &lt;TD&gt;£145.50&lt;/TD&gt; 
   &lt;/TR&gt; 
   &lt;TR&gt; 
    &lt;TD&gt;A123&lt;/TD&gt; 
    &lt;TD&gt;22/01/2015 09:21:21&lt;/TD&gt; 
    &lt;TD&gt;£151.45&lt;/TD&gt; 
   &lt;/TR&gt; 
   &lt;TR&gt; 
    &lt;TD&gt;B234&lt;/TD&gt; 
    &lt;TD&gt;21/01/2015 11:12:32&lt;/TD&gt; 
    &lt;TD&gt;£23.45&lt;/TD&gt; 
   &lt;/TR&gt; 
   &lt;TR&gt; 
    &lt;TD&gt;B234&lt;/TD&gt; 
    &lt;TD&gt;21/01/2015 11:45:54&lt;/TD&gt; 
    &lt;TD&gt;£43.65&lt;/TD&gt; 
   &lt;/TR&gt; 
  &lt;/TBODY&gt; 
 &lt;/TABLE&gt; 
&lt;/DIV&gt; 
&lt;P&gt;&amp;nbsp;&lt;/P&gt; 
&lt;P&gt;In order to identify the Amendment Orders from the Original Orders, we need to check the Order Number of the row before. If it is the same, then the current record is an Amendment Order, if it is different it is an Original Order.&lt;/P&gt; 
&lt;P&gt;&amp;nbsp;&lt;/P&gt; 
&lt;P&gt;We can do this in Talend quite simply using a tMap component and tMap variables. One of the most overlooked features of the tMap component is the fact that the tMap variables can store values between rows and they are processed in order (top down). This allows us to store values between rows and compare them with current rows. The screenshot below shows the layout of the tMap component. I will explain the expressions below the screenshot.&lt;/P&gt; 
&lt;P&gt;&amp;nbsp;&lt;/P&gt; 
&lt;P&gt;&lt;SPAN class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="CompareRowsTMap.jpg" style="width: 999px;"&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="0683p000009M06r.jpg"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/150438i137FFD3148783044/image-size/large?v=v2&amp;amp;px=999" role="button" title="0683p000009M06r.jpg" alt="0683p000009M06r.jpg" /&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;/P&gt; 
&lt;P&gt;&amp;nbsp;&lt;/P&gt; 
&lt;P&gt;&amp;nbsp;&lt;/P&gt; 
&lt;P&gt;The variable expressions are shown and explained in the table below....&lt;/P&gt; 
&lt;DIV class="table-responsive"&gt;
  Variable Expression Description 
 &lt;TABLE border="1" cellspacing="1" cellpadding="1"&gt; 
  &lt;TBODY&gt; 
   &lt;TR&gt; 
    &lt;TD&gt;order_type&lt;/TD&gt; 
    &lt;TD&gt;Var.last_order!=null &amp;amp;&amp;amp; Var.last_order.compareToIgnoreCase(row2.OrderNumber)==0&lt;BR /&gt;? "Amendment Order" : "Original Order"&amp;nbsp;&lt;/TD&gt; 
    &lt;TD&gt;This variable expression is tested first. First the "last_order" variable is checked to see if it is not null and to see if it matches the current "OrderNumber". Since the "last_order" variable has not been assigned yet for the first row, it will be null. For subsequent rows it will hold the value of the previous row. If the "OrderNumber" value matches the value assigned to "last_order" in the previous row, it means it is an "Amendment Order". Otherwise it is an "Original Order".&lt;/TD&gt; 
   &lt;/TR&gt; 
   &lt;TR&gt; 
    &lt;TD&gt;last_order&lt;/TD&gt; 
    &lt;TD&gt;row2.OrderNumber&amp;nbsp;&lt;/TD&gt; 
    &lt;TD&gt;The "last_order" variable is checked first and assigned second due to the order of processing the variables.&amp;nbsp;&lt;/TD&gt; 
   &lt;/TR&gt; 
  &lt;/TBODY&gt; 
 &lt;/TABLE&gt; 
&lt;/DIV&gt; 
&lt;P&gt;&amp;nbsp;&lt;/P&gt; 
&lt;P&gt;The result of the tMap processing the data in the table at the top is shown below.....&lt;/P&gt; 
&lt;DIV class="table-responsive"&gt; 
 &lt;TABLE border="1" cellspacing="1" cellpadding="1"&gt; 
  &lt;TBODY&gt; 
   &lt;TR&gt; 
    &lt;TD&gt;&lt;SPAN&gt;[statistics] connecting to socket on port 4007&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[statistics] connected&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;A123|2015-01-21T10:23:45|123.4|Original Order&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;A123|2015-01-21T10:34:48|145.5|Amendment Order&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;A123|2015-01-22T09:21:21|151.45|Amendment Order&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;B234|2015-01-21T11:12:32|23.45|Original Order&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;B234|2015-01-21T11:45:54|43.65|Amendment Order&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[statistics] disconnected&lt;/SPAN&gt;&lt;/TD&gt; 
   &lt;/TR&gt; 
  &lt;/TBODY&gt; 
 &lt;/TABLE&gt; 
&lt;/DIV&gt;</description>
      <pubDate>Thu, 13 Sep 2018 21:07:28 GMT</pubDate>
      <guid>https://community.qlik.com/t5/Talend-Studio/Compare-row-value-against-a-value-from-the-previous-row/m-p/2284741#M58375</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2018-09-13T21:07:28Z</dc:date>
    </item>
    <item>
      <title>Re: Compare row value against a value from the previous row</title>
      <link>https://community.qlik.com/t5/Talend-Studio/Compare-row-value-against-a-value-from-the-previous-row/m-p/2284742#M58376</link>
      <description>&lt;P&gt;Thank You, in addition to answering my basic question of how to refer a previous variable in the next variable, it also educates me on how to compare column values between current and previous rows using "compareToIgnoreCase"&lt;/P&gt;</description>
      <pubDate>Mon, 03 Jun 2019 19:47:52 GMT</pubDate>
      <guid>https://community.qlik.com/t5/Talend-Studio/Compare-row-value-against-a-value-from-the-previous-row/m-p/2284742#M58376</guid>
      <dc:creator>tnewbie</dc:creator>
      <dc:date>2019-06-03T19:47:52Z</dc:date>
    </item>
    <item>
      <title>Re: Compare row value against a value from the previous row</title>
      <link>https://community.qlik.com/t5/Talend-Studio/Compare-row-value-against-a-value-from-the-previous-row/m-p/2284743#M58377</link>
      <description>&lt;P&gt;This solution is only possible in Standard jobs.&lt;/P&gt;&lt;P&gt;For Spark job you would need to do something different.&lt;/P&gt;&lt;P&gt;Check out this video:&lt;/P&gt;&lt;P&gt;&lt;A href="https://youtu.be/9x75hVd2h64" target="_blank" rel="noopener nofollow noopener noreferrer"&gt;https://youtu.be/9x75hVd2h64&lt;/A&gt;&lt;/P&gt;&lt;P&gt;It explains a use case where the previous record needs to be considered in Spark.&lt;/P&gt;</description>
      <pubDate>Wed, 25 Sep 2019 13:12:38 GMT</pubDate>
      <guid>https://community.qlik.com/t5/Talend-Studio/Compare-row-value-against-a-value-from-the-previous-row/m-p/2284743#M58377</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2019-09-25T13:12:38Z</dc:date>
    </item>
    <item>
      <title>Re: Compare row value against a value from the previous row</title>
      <link>https://community.qlik.com/t5/Talend-Studio/Compare-row-value-against-a-value-from-the-previous-row/m-p/2284744#M58378</link>
      <description>&lt;P&gt;Thanks for letting us know.&lt;/P&gt;</description>
      <pubDate>Wed, 25 Sep 2019 14:53:18 GMT</pubDate>
      <guid>https://community.qlik.com/t5/Talend-Studio/Compare-row-value-against-a-value-from-the-previous-row/m-p/2284744#M58378</guid>
      <dc:creator>tnewbie</dc:creator>
      <dc:date>2019-09-25T14:53:18Z</dc:date>
    </item>
  </channel>
</rss>

