<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Calculate time difference based on another column and it's previous/next row in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-time-difference-based-on-another-column-and-it-s/m-p/893117#M7939</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="197128" data-lia-user-login="stzlee" class="lia-mention lia-mention-user"&gt;stzlee&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;try a measure&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Measure = 
var _lastNoTrip = CALCULATE(MAX(Table1[Time Local]),FILTER(ALL(Table1),Table1[Time Local]&amp;lt;MAX(Table1[Time Local]) &amp;amp;&amp;amp; Table1[TripTypeTest]="NoTrip"))
var _lastFirstInTrip = CALCULATE(MIN(Table1[Time Local]),Table1[Time Local]&amp;gt;_lastNoTrip,Table1[TripTypeTest]="InTrip")
var _previousTime = CALCULATE(MAX(Table1[Time Local]),FILTER(ALL(Table1),Table1[Time Local]&amp;lt;MAX(Table1[Time Local])))
var _previousTripType = CALCULATE(FIRSTNONBLANK(Table1[TripTypeTest],1),Table1[Time Local]=_previousTime)
var _duration = DATEDIFF(_lastFirstInTrip,SELECTEDVALUE(Table1[Time Local]),SECOND)
var Hours = INT(_duration/3600)
var Minutes = INT(MOD(_duration - (Hours * 3600),3600)/60)
var Seconds = ROUNDUP(MOD(MOD(_duration - (Hours * 3600),3600),60),0)
var H =
    IF ( LEN ( Hours ) = 1, 
        CONCATENATE ( "0", Hours ),
        CONCATENATE ( "", Hours )
      )
var M =
    IF (
        LEN ( Minutes ) = 1,
        CONCATENATE ( "0", Minutes ),
        CONCATENATE ( "", Minutes )
    )
var S =
    IF (
        LEN ( Seconds ) = 1,
        CONCATENATE ( "0", Seconds ),
        CONCATENATE ( "", Seconds )
    )

RETURN
if(_previousTripType="InTrip",CONCATENATE(H,CONCATENATE( ":", CONCATENATE(M,CONCATENATE(":", S)))),0)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;big thx for a huge part of this solution for&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="313" data-lia-user-login="Greg_Deckler" class="lia-mention lia-mention-user"&gt;Greg_Deckler&lt;/a&gt;&amp;nbsp;here&amp;nbsp;&lt;A href="https://community.powerbi.com/t5/DAX-Commands-and-Tips/Duration-in-DAX/td-p/57978" target="_blank"&gt;https://community.powerbi.com/t5/DAX-Commands-and-Tips/Duration-in-DAX/td-p/57978&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;I&gt;&lt;FONT color="#ababab"&gt;do not hesitate to give a kudo to useful posts and mark solutions as solution&lt;/FONT&gt;&lt;/I&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://linkedin.com/in/alekseizhukov" target="_blank"&gt;LinkedIn&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Wed, 08 Jan 2020 07:51:47 GMT</pubDate>
    <dc:creator>az38</dc:creator>
    <dc:date>2020-01-08T07:51:47Z</dc:date>
    <item>
      <title>Calculate time difference based on another column and it's previous/next row</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-time-difference-based-on-another-column-and-it-s/m-p/892686#M7919</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am somewhat new to DAX and Power BI in general, but I have made pseudocode for this problem and am having difficulty trying to convert this into DAX.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I'm trying to achieve is to calculate the time difference when the subject is determined to be in a 'trip'. The values for whether it is in a trip or not is 'InTrip' and 'NoTrip' respectively. The complexity comes from the fact that if the subject is InTrip for only one time-stamp then the time in trip shall not be calculated (essentially requires to having a minimum of&amp;nbsp;two InTrips consecutively). Here is an arbitrary example of what I mean:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;One thing to note is that the counter would start from the bottom-most row. Unsure what the convention is in Power BI and perhaps I have to flip the Time Local column?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have looked at other somewhat similar threads but I have not been able to alter their code/logic to be able to solve my problem :&amp;lt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance!!&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jan 2020 23:22:46 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-time-difference-based-on-another-column-and-it-s/m-p/892686#M7919</guid>
      <dc:creator>stzlee</dc:creator>
      <dc:date>2020-01-07T23:22:46Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate time difference based on another column and it's previous/next row</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-time-difference-based-on-another-column-and-it-s/m-p/893117#M7939</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="197128" data-lia-user-login="stzlee" class="lia-mention lia-mention-user"&gt;stzlee&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;try a measure&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Measure = 
var _lastNoTrip = CALCULATE(MAX(Table1[Time Local]),FILTER(ALL(Table1),Table1[Time Local]&amp;lt;MAX(Table1[Time Local]) &amp;amp;&amp;amp; Table1[TripTypeTest]="NoTrip"))
var _lastFirstInTrip = CALCULATE(MIN(Table1[Time Local]),Table1[Time Local]&amp;gt;_lastNoTrip,Table1[TripTypeTest]="InTrip")
var _previousTime = CALCULATE(MAX(Table1[Time Local]),FILTER(ALL(Table1),Table1[Time Local]&amp;lt;MAX(Table1[Time Local])))
var _previousTripType = CALCULATE(FIRSTNONBLANK(Table1[TripTypeTest],1),Table1[Time Local]=_previousTime)
var _duration = DATEDIFF(_lastFirstInTrip,SELECTEDVALUE(Table1[Time Local]),SECOND)
var Hours = INT(_duration/3600)
var Minutes = INT(MOD(_duration - (Hours * 3600),3600)/60)
var Seconds = ROUNDUP(MOD(MOD(_duration - (Hours * 3600),3600),60),0)
var H =
    IF ( LEN ( Hours ) = 1, 
        CONCATENATE ( "0", Hours ),
        CONCATENATE ( "", Hours )
      )
var M =
    IF (
        LEN ( Minutes ) = 1,
        CONCATENATE ( "0", Minutes ),
        CONCATENATE ( "", Minutes )
    )
var S =
    IF (
        LEN ( Seconds ) = 1,
        CONCATENATE ( "0", Seconds ),
        CONCATENATE ( "", Seconds )
    )

RETURN
if(_previousTripType="InTrip",CONCATENATE(H,CONCATENATE( ":", CONCATENATE(M,CONCATENATE(":", S)))),0)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;big thx for a huge part of this solution for&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="313" data-lia-user-login="Greg_Deckler" class="lia-mention lia-mention-user"&gt;Greg_Deckler&lt;/a&gt;&amp;nbsp;here&amp;nbsp;&lt;A href="https://community.powerbi.com/t5/DAX-Commands-and-Tips/Duration-in-DAX/td-p/57978" target="_blank"&gt;https://community.powerbi.com/t5/DAX-Commands-and-Tips/Duration-in-DAX/td-p/57978&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;I&gt;&lt;FONT color="#ababab"&gt;do not hesitate to give a kudo to useful posts and mark solutions as solution&lt;/FONT&gt;&lt;/I&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://linkedin.com/in/alekseizhukov" target="_blank"&gt;LinkedIn&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Jan 2020 07:51:47 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-time-difference-based-on-another-column-and-it-s/m-p/893117#M7939</guid>
      <dc:creator>az38</dc:creator>
      <dc:date>2020-01-08T07:51:47Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate time difference based on another column and it's previous/next row</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-time-difference-based-on-another-column-and-it-s/m-p/894415#M8002</link>
      <description>&lt;P&gt;Thanks &lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="82403" data-lia-user-login="az38" class="lia-mention lia-mention-user"&gt;az38&lt;/a&gt;. I have tried to understand the logic and have applied your provided measure to my data.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I have found by changing the RETURN function to output "lastNoTrip", "previousTime", and "lastFirstInTrip" (for debugging purposes) provides the desired output for each.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, when I revert it to H,M,and S; and try to display the measure as a column, the error of "Expressions that yield variant data-type cannot be used to define calculated columns." This was attempted by simply using:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;newColumn = Table1[Measure]&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To remedy this for debugging purposes, I have tried formatting the values from a number to a string. The result is just a column full of 0's. The DAX equation for this is:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;newColumn = CONCATENATE(FORMAT(Table1[Measure], "General Number", "")&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have and will be attempting multiple other workarounds but eagerly wait your (or anyone's) response.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you again in advance!&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jan 2020 05:21:38 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-time-difference-based-on-another-column-and-it-s/m-p/894415#M8002</guid>
      <dc:creator>stzlee</dc:creator>
      <dc:date>2020-01-09T05:21:38Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate time difference based on another column and it's previous/next row</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-time-difference-based-on-another-column-and-it-s/m-p/895185#M8030</link>
      <description>&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="197128" data-lia-user-login="stzlee" class="lia-mention lia-mention-user"&gt;stzlee&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;you could share your pbix-file without sensitive data for me, i will check&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;I&gt;&lt;FONT color="#ababab"&gt;do not hesitate to give a kudo to useful posts and mark solutions as solution&lt;/FONT&gt;&lt;/I&gt;&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jan 2020 16:16:04 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-time-difference-based-on-another-column-and-it-s/m-p/895185#M8030</guid>
      <dc:creator>az38</dc:creator>
      <dc:date>2020-01-09T16:16:04Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate time difference based on another column and it's previous/next row</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-time-difference-based-on-another-column-and-it-s/m-p/895620#M8061</link>
      <description>&lt;P&gt;Thanks for the offer &lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="82403" data-lia-user-login="az38" class="lia-mention lia-mention-user"&gt;az38&lt;/a&gt;&amp;nbsp;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;After deleting a few of the sensitive data in preparation to share the pbix-file, the column shows the desired result!! Although the Visual shows a weird&amp;nbsp;value, but I believe I can write a new Measure to get what&amp;nbsp;I need.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I will try to troubleshoot why having multiple irrelevant columns have affected the result but I will approve your solution!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for everything!!&lt;/P&gt;</description>
      <pubDate>Fri, 10 Jan 2020 00:42:56 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-time-difference-based-on-another-column-and-it-s/m-p/895620#M8061</guid>
      <dc:creator>stzlee</dc:creator>
      <dc:date>2020-01-10T00:42:56Z</dc:date>
    </item>
  </channel>
</rss>

