cancel
Showing results for
Did you mean:

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Resolver I

## Date time difference in hours and minutes

Hello All ,

My name is Sujit . I hope @amitchandak   or other experts can help me in this . Any help is greatly appreciated .

I have a column with Timestamp in them with format yyyy/mm/dd hh:mm . And i have almost 756 rows . There are one index for it which is not integer data type but string type because it does have some alphabet in between .here is an example of my index 1234A980 .

Now following is how my data looks like ,

 Datetime Index 1/1/2020 12:45 2123A4 1/2/2020 1:32 2123A4 1/3/2020 4:00 2123A4 1/6/2020 5:00 2123A4 2/20/2020 3:00 48320B3249 2/21/2020 4:00 48320B3249 2/22/2020 4:00 48320B3249

What i want to do is depending on the index , i have to calculate time difference in hours and minutes .
For example for index 21234A has 4 rows . hence for these 4 rows ill calculate the time difference as
first row  will show as 0 in a new column as timedifference
second row will show value of  1/2/2020 1:32  -  1/1/2020 12:45
third row will show value of  1/3/2020 4:00   -  1/2/2020 1:32

So every time a index changes for the first time (first date time stamp) time diff must be zero.

The time difference should be in reference with index

Please if any doubt do comment . Any help will be appreciated .

I am trying with following DAX but its not working , its showing largest date based on index

timediff =

VAR a = 'Main Table'[Timestamp]
VAR b = 'Main Table'[Index]
var d = CALCULATE(MAXX('Main Table', 'Main Table'[Timestamp]),FILTER('Main Table','Main Table'[index]= EARLIER('Main Table'[index])))
return
Datediff(b,a,HOURS)

1 ACCEPTED SOLUTION
Resolver I

@Vera_33  Hello i finally cracked it with your starting help . thanks .

I used MAXX instead of MINX what you used and i also made a column with RANKX and used it in the filter as current RANK > EARLIER (RANK) Thankyou so much. For my other friends out there following is what i used .

Timediff =
VAR CurTime = 'Main Table'[Timestamp]
VAR CurIndex = 'Main Table'[ind]
VAR CurRank = 'Main Table'[Rank]
VAR PrvTime = MAXX(FILTER( 'Main Table','Main Table'[ind] = EARLIER('Main Table'[ind]) && 'Main Table'[Rank] < EARLIER('Main Table'[Rank])
),'Main Table'[Timestamp])

return
IF(ISBLANK(PrvTime),0,(DATEDIFF(PrvTime,CurTime,MINUTE)))

7 REPLIES 7
Super User

Hi @Sujit14496
Another way of doing that (Including the HH:MM time format)

``````timediff =
VAR CurrentTime = 'Main Table'[Timestamp]
VAR CurrentIndexTable =
CALCULATETABLE ( 'Main Table', ALLEXCEPT ( 'Main Table', 'Main Table'[Index] ) )
VAR TimesBefore =
FILTER ( CurrentIndexTable, 'Main Table'[Timestamp] < CurrentTime )
VAR PreviousTime =
MAXX ( TimesBefore, 'Main Table'[Timestamp] )
VAR TimeDiff =
DATEDIFF ( PreviousTime, CurrentTime, MINUTE )
RETURN
IF (
NOT ISBLANK ( PreviousTime ),
QUOTIENT ( TimeDiff, 24 ) & ":"
& MOD ( TimeDiff, 24 )
)``````

Resolver I

@Vera_33  Hello i finally cracked it with your starting help . thanks .

I used MAXX instead of MINX what you used and i also made a column with RANKX and used it in the filter as current RANK > EARLIER (RANK) Thankyou so much. For my other friends out there following is what i used .

Timediff =
VAR CurTime = 'Main Table'[Timestamp]
VAR CurIndex = 'Main Table'[ind]
VAR CurRank = 'Main Table'[Rank]
VAR PrvTime = MAXX(FILTER( 'Main Table','Main Table'[ind] = EARLIER('Main Table'[ind]) && 'Main Table'[Rank] < EARLIER('Main Table'[Rank])
),'Main Table'[Timestamp])

return
IF(ISBLANK(PrvTime),0,(DATEDIFF(PrvTime,CurTime,MINUTE)))

Resolver I
Resolver I
Resident Rockstar

I assume you are adding a DAX Calculated column, see if it is what you are looking for

``````timediff =
VAR CurTime = 'Table'[Datetime]
VAR CurIndex = 'Table'[Index]
var NextTime = MINX(FILTER('Table','Table'[index]= CurIndex&&'Table'[Datetime]>CurTime), 'Table'[Datetime])
return
Datediff(CurTime,NextTime,HOUR)``````

Resolver I

hello @Vera_33  ,
Thankyou for your reply . but it is not working properly for me .

I would love to also explain one more thing ,
Following is output i am looking for ,

Col A                                        Index                                                 Timediff

 1/1/2020 12:45 2123A4 0 1/2/2020 1:32 2123A4 A2-A1 1/3/2020 4:00 2123A4 A3-A2 1/6/2020 5:00 2123A4 A4-A3 2/20/2020 3:00 48320B3249 0 2/21/2020 4:00 48320B3249 A6-A5 2/22/2020 4:00 48320B3249 A7-A6
Resolver I

Announcements

#### New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

#### Power BI Monthly Update - May 2024

Check out the May 2024 Power BI update to learn about new features.

#### Fabric certifications survey

Certification feedback opportunity for the community.

Top Solution Authors
Top Kudoed Authors