title: How to group based on value in previous row?
body: <p>edit: The answer below solved my initial question, but i did not mention that the list also includes individual vessels, and they need to be considered. </p>
  
  <p>I have a list of vessels  and I want to create routes based on certain criteria. I have produced the following list, and I want to create a TRACKID for each route segment for each individual ship. </p>
  
  <pre><code>VESSEL  Date    Sailing/Port
  1      01.02.2016   SAILING
  1      02.02.2016   SAILING
  1      03.02.2016   SAILING
  1      04.02.2016   SAILING
  1      05.02.2016   SAILING
  1      06.02.2016   PORT
  1      07.02.2016   PORT
  1      08.02.2016   PORT
  1      09.02.2016   PORT
  1      10.02.2016   SAILING
  2      11.02.2016   SAILING
  2      12.02.2016   SAILING
  2      13.02.2016   SAILING
  2      14.02.2016   PORT
  2      15.02.2016   PORT
  2      16.02.2016   SAILING
  </code></pre>
  
  <p>If the ship is SAILING I want it to keep the same TRACKID until it enters a PORT. The first half of the positions in the PORT should be assigned to the same TRACKID as before entering the port. The second half should be given a new TRACKID that remain until next port. I also need the TRACKID to change when a new ship is introduced. </p>
  
  <pre><code>VESSEL      Date      Sailing/Port  TRACKID     
     1      01.02.2016    SAILING     1
     1      02.02.2016    SAILING     1
     1      03.02.2016    SAILING     1
     1      04.02.2016    SAILING     1
     1      05.02.2016    SAILING     1
     1      06.02.2016    PORT        1
     1      07.02.2016    PORT        1
     1      08.02.2016    PORT        2
     1      09.02.2016    PORT        2
     1      10.02.2016    SAILING     2
     2      11.02.2016    SAILING     3
     2      12.02.2016    SAILING     3
     2      13.02.2016    SAILING     3
     2      14.02.2016    PORT        3
     2      15.02.2016    PORT        4
     2      16.02.2016    SAILING     4
  </code></pre>
  
  <p>The answers below were able to create a TRACKID using sailed/port, but they did not take the new vessel into consideration. It is random if a track starts in port or sailing, and it the date is random as well.</p>