title: How do I use lag to get the previous row before a specific time window of data?
body: <p>Every day I create a table that looks like this:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>user_id</th>
  <th>received_at</th>
  <th>age_pref</th>
  <th>ethnicity_pref</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>1</td>
  <td>10:01</td>
  <td>18-28</td>
  <td>open_to_all</td>
  </tr>
  <tr>
  <td>2</td>
  <td>10:05</td>
  <td>18-23</td>
  <td>open_to_all</td>
  </tr>
  <tr>
  <td>1</td>
  <td>10:08</td>
  <td>18-30</td>
  <td>open_to_all</td>
  </tr>
  <tr>
  <td>2</td>
  <td>10:07</td>
  <td>18-25</td>
  <td>Hispanic/Iatino</td>
  </tr>
  <tr>
  <td>3</td>
  <td>10:09</td>
  <td>56-33</td>
  <td>White</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>It's a table that lists the actions a user takes from 10am-11am. As you can see, there are 3 distinct user IDs.</p>
  <p>Using this, I'm trying to create another table using <code>lag</code> to see if the previous value changed or not. However, the problem is that the first row is inaccurate because there's no way for me to measure if an attribute changed without the previous row before this set of data (maybe it occurred at 930am). How do I get the previous <code>received_at</code> row for each user ID in this table, but only 1 for each user_id? I want the new table to look like this, where the new records are prepended at the beginning</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>user_id</th>
  <th>received_at</th>
  <th>age_pref</th>
  <th>ethnicity_pref</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>1</td>
  <td>9:48</td>
  <td>20-30</td>
  <td>asian</td>
  </tr>
  <tr>
  <td>2</td>
  <td>9:52</td>
  <td>30-32</td>
  <td>white</td>
  </tr>
  <tr>
  <td>3</td>
  <td>9:58</td>
  <td>28-30</td>
  <td>open_to_all</td>
  </tr>
  <tr>
  <td>1</td>
  <td>10:01</td>
  <td>18-28</td>
  <td>open_to_all</td>
  </tr>
  <tr>
  <td>2</td>
  <td>10:05</td>
  <td>18-23</td>
  <td>open_to_all</td>
  </tr>
  <tr>
  <td>1</td>
  <td>10:08</td>
  <td>18-30</td>
  <td>open_to_all</td>
  </tr>
  <tr>
  <td>2</td>
  <td>10:07</td>
  <td>18-25</td>
  <td>Hispanic/Iatino</td>
  </tr>
  <tr>
  <td>3</td>
  <td>10:09</td>
  <td>56-33</td>
  <td>White</td>
  </tr>
  </tbody>
  </table>
  </div>
  <ul>
  <li><p>note there are several rows that exist before this time interval. I want the most recent one prepended to the table for the user_ids that exist in the table.</p>
  </li>
  <li><p>basically I want to include 1 more row for EACH user_id before the time window so that my table that tracks changes is accurate since lag will always have the first row be null.</p>
  </li>
  </ul>