cancel
Showing results for 
Search instead for 
Did you mean: 

Fabric is Generally Available. Browse Fabric Presentations. Work towards your Fabric certification with the Cloud Skills Challenge.

Reply
Sujit14496
Resolver I
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 ,

DatetimeIndex
1/1/2020 12:452123A4
1/2/2020 1:322123A4
1/3/2020 4:002123A4
1/6/2020 5:002123A4
2/20/2020 3:0048320B3249
2/21/2020 4:0048320B3249
2/22/2020 4:0048320B3249
  

 

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
please help

 

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
Sujit14496
Resolver I
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)))




View solution in original post

7 REPLIES 7
tamerj1
Super User
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 )
    )



Sujit14496
Resolver I
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)))




Sujit14496
Resolver I
Resolver I

Sujit14496
Resolver I
Resolver I

Hi @Sujit14496 

 

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)

Vera_33_0-1655857872430.png

 

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:322123A4                                           A2-A1
1/3/2020 4:002123A4A3-A2
1/6/2020 5:002123A4A4-A3
2/20/2020 3:0048320B32490
2/21/2020 4:0048320B3249A6-A5
2/22/2020 4:00                    48320B3249                                             A7-A6

 @tamerj1Please help

Helpful resources

Announcements
PBI November 2023 Update Carousel

Power BI Monthly Update - November 2023

Check out the November 2023 Power BI update to learn about new features.

Community News

Fabric Community News unified experience

Read the latest Fabric Community announcements, including updates on Power BI, Synapse, Data Factory and Data Activator.

Dashboard in a day with date

Exclusive opportunity for Women!

Join us for a free, hands-on Microsoft workshop led by women trainers for women where you will learn how to build a Dashboard in a Day!

Power BI Fabric Summit Carousel

The largest Power BI and Fabric virtual conference

130+ sessions, 130+ speakers, Product managers, MVPs, and experts. All about Power BI and Fabric. Attend online or watch the recordings.

Top Solution Authors
Top Kudoed Authors