title: Time Lag Calculation in Dates/Datetime by using DAX
body: <p>I am trying to reproduce this <a href="https://stackoverflow.com/questions/37446950/powerbi-time-lag-calculation-in-dates-using-query-editor">example</a>. I use this DAX expression</p>
  <pre><code>LagColumn = DATEDIFF(
              CALCULATE(MAX(MyTable[Date]), 
                  FILTER(MyTable, 
                      MyTable[Device] = EARLIER(MyTable[Device]) &amp;&amp; 
                      MyTable[Date] &lt; EARLIER(MyTable[Date])
                  )
              ), MyTable[Date]
  
          , DAY
          )
  </code></pre>
  <p>However, this calculation does not work when you have two same values (I modified the input of the example):</p>
  <p><a href="https://i.stack.imgur.com/OA4ef.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OA4ef.png" alt="enter image description here" /></a></p>
  <p>It should be 0 instead of 3. How can I solve this problem? It seems that there is no Lag function in DAX.</p>