title: SQL Server : LAG multiple Nulls
body: <p>I am trying to &quot;fill in the blanks&quot; with my table below.</p>
  <p>I have created a PDF reader that spits out a JSON file that I have manipulated into the below format.</p>
  <p>There is very little scope in changing source data, or using a secondary table as a helper, due to the fact that this is meant to handle new data not &quot;seen&quot; before.</p>
  <p>What I need help with is getting the <code>Class</code> and <code>Group</code> columns filled in. By this I mean that the Class Column will always have a value in the last row, and I need this repeated (upwards) until it comes across a non-blank value in the column. It then needs to repeat this value, until it comes across the next non-blank and so on.</p>
  <p>Similarly the Group Column needs the same solution but starting from the first row down.</p>
  <p>I have tried <code>LAG()</code> <code>LEAD()</code> etc with default values, but it doesn't handle the multiple <code>nulls</code>.</p>
  <p>I also need the <code>Group</code> column to show the class value when not blank.</p>
  <p>I have had a look at cte's but not overly familiar with them and have gotten myself tied in knots today!</p>
  <p>Any help is appreciated.</p>
  <p>Current Data</p>
  <pre><code>ID, Class, Group, Total, Account
  1, null, INCOME, null, Fencing
  2, null, null, null, Crop
  3, Net Income, null, null, Net Income
  4, null, Farm Expenditure, null, Irrigation
  5, null, null, null, electricity
  6, Surplus, null, null, Surplus
  7, null, GST, null, GST
  8, Closing Balance, null, null, Closing Balance
  </code></pre>
  <p>What I want</p>
  <pre><code>ID, Class, Group, Total, Account
  1, Net Income, INCOME, null, Fencing
  2, Net Income, INCOME, null, Crop
  3, Net Income, INCOME, null, Net Income
  4, Surplus, Farm Expenditure, null, Irrigation
  5, Surplus, Farm Expenditure, null, electricity
  6, Surplus, Farm Expenditure, null, Surplus
  7, Closing Balance, GST, null, GST
  8, Closing Balance, GST, null, Closing Balance
  </code></pre>