title: Difference between multiple dates in the same column based on category
body: <p>I have the following problem ..</p>
  
  <p>I have this table:</p>
  
  <p><a href="https://i.stack.imgur.com/2uUDv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2uUDv.png" alt="enter image description here"></a></p>
  
  <p>I want to create a calculated column that tells me the difference of days between two dates of the same code (<strong>COD</strong>), the difference should be calculated based on the date before the line. </p>
  
  <p>For example:</p>
  
  <p>Using <strong>COD</strong> <em>B</em></p>
  
  <p><strong>COD |     DATE</strong>  | <em>days_diff</em></p>
  
  <p>B |05/01/2018 |   </p>
  
  <p>B |09/01/2018 | <em>4</em></p>
  
  <p>B |12/01/2018 | <em>3</em></p>
  
  <p>In the example image the codes / dates are sorted in sequence, but in reality they are out of order.</p>
  
  <p>I tried to use the following sentence in DAX:</p>
  
  <pre><code> DATEDIFF(Testing[DATE]; FIRSTDATE(FILTER( ALL(Testing[DATE]) ;Testing[DATE] &gt; EARLIER(Testing[DATE])));DAY)
  </code></pre>
  
  <p><strong>Explaining what I've tried:</strong></p>
  
  <p>Make the difference between the date on the line and using the <em>EARLIER</em> function to get the most recent date out of the current one.</p>
  
  <p>However, I obtained the following result:</p>
  
  <p><a href="https://i.stack.imgur.com/SL7WU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SL7WU.png" alt="enter image description here"></a></p>
  
  <p>I can not filter <strong>COD</strong>, so that the analysis of 'EARLIER' is performed only in the same 'group', so I understand the PowerBI is considering all the dates.</p>
  
  <p>Any idea?</p>