Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Tan_LC
Helper II
Helper II

Time Duration by Rows

Hi, in below table, I would like to calculation the "Duration (mm:ss)" by comparing the DateTime by rows within the same ID.

I've tried on the below method but failed to have the desired solution. Kindly assist.

 

Duration (mm:ss) =
VAR CurrentDateTime = Table[DateTime]
VAR NextDateTime =
    CALCULATE(
        MIN(Table[DateTime]),
        FILTER(Table,
        Table[DateTime] > CurrentDateTime && Table[ID] = EARLIER(Table[ID]))
    )
RETURN IF(ISBLANK(NextDateTime),
    BLANK(),
    DATEDIFF(CurrentDateTime, NextDateTime, SECOND))
 
** outcome should be in [mm:ss] instead of [SECOND]. 

 

IDDateTime

Duration (mm:ss)

(Desired outcome)

10342420/11/2024 3:27:01 PM 
10342420/11/2024 3:27:26 PM00:25
10342420/11/2024 3:27:50 PM00:24
10342420/11/2024 3:31:38 PM03:48
10342420/11/2024 3:32:05 PM00:27
10262520/11/2024 3:35:02 PM 
10262520/11/2024 3:35:02 PM00:00
10262520/11/2024 3:35:55 PM00:53
10262520/11/2024 3:36:19 PM00:24
10262520/11/2024 3:37:55 PM01:36
10188420/11/2024 4:24:56 PM 
10188420/11/2024 4:25:44 PM00:48
10188420/11/2024 4:36:13 PM10:29
10188420/11/2024 4:38:31 PM02:18
10188420/11/2024 4:47:56 PM09:25

 

Thank you.

 

Regards,

LC

2 ACCEPTED SOLUTIONS
bhanu_gautam
Super User
Super User

@Tan_LC , Try using

DAX
Duration (mm:ss) =
VAR CurrentDateTime = Table[DateTime]
VAR NextDateTime =
CALCULATE(
MIN(Table[DateTime]),
FILTER(Table,
Table[DateTime] > CurrentDateTime && Table[ID] = EARLIER(Table[ID]))
)
VAR DurationInSeconds = DATEDIFF(CurrentDateTime, NextDateTime, SECOND)
VAR Minutes = QUOTIENT(DurationInSeconds, 60)
VAR Seconds = MOD(DurationInSeconds, 60)
RETURN IF(ISBLANK(NextDateTime),
BLANK(),
FORMAT(Minutes, "00") & ":" & FORMAT(Seconds, "00"))




Did I answer your question? Mark my post as a solution! And Kudos are appreciated

Proud to be a Super User!




LinkedIn






View solution in original post

rohit1991
Super User
Super User

Hi @Tan_LC ,

To calculate the duration (mm:ss) by comparing the DateTime values between rows within the same ID in Power BI, you need to adjust your DAX formula so that the difference is calculated in seconds and then formatted into minutes and seconds.

Here’s how you can modify your DAX formula:

Duration (mm:ss) = 
VAR CurrentDateTime = Table[DateTime]
VAR NextDateTime =
    CALCULATE(
        MIN(Table[DateTime]),
        FILTER(
            Table,
            Table[DateTime] > CurrentDateTime && Table[ID] = EARLIER(Table[ID])
        )
    )
VAR DurationInSeconds = 
    IF(
        ISBLANK(NextDateTime),
        BLANK(),
        DATEDIFF(CurrentDateTime, NextDateTime, SECOND)
    )
VAR Minutes = QUOTIENT(DurationInSeconds, 60)
VAR Seconds = MOD(DurationInSeconds, 60)
RETURN
    IF(
        ISBLANK(DurationInSeconds),
        BLANK(),
        FORMAT(Minutes, "00") & ":" & FORMAT(Seconds, "00")
    )

 

This will give you the desired output of the duration in mm:ss format between rows within the same ID.


Did it work? ✔ Give a Kudo • Mark as Solution – help others too!

View solution in original post

3 REPLIES 3
rohit1991
Super User
Super User

Hi @Tan_LC ,

To calculate the duration (mm:ss) by comparing the DateTime values between rows within the same ID in Power BI, you need to adjust your DAX formula so that the difference is calculated in seconds and then formatted into minutes and seconds.

Here’s how you can modify your DAX formula:

Duration (mm:ss) = 
VAR CurrentDateTime = Table[DateTime]
VAR NextDateTime =
    CALCULATE(
        MIN(Table[DateTime]),
        FILTER(
            Table,
            Table[DateTime] > CurrentDateTime && Table[ID] = EARLIER(Table[ID])
        )
    )
VAR DurationInSeconds = 
    IF(
        ISBLANK(NextDateTime),
        BLANK(),
        DATEDIFF(CurrentDateTime, NextDateTime, SECOND)
    )
VAR Minutes = QUOTIENT(DurationInSeconds, 60)
VAR Seconds = MOD(DurationInSeconds, 60)
RETURN
    IF(
        ISBLANK(DurationInSeconds),
        BLANK(),
        FORMAT(Minutes, "00") & ":" & FORMAT(Seconds, "00")
    )

 

This will give you the desired output of the duration in mm:ss format between rows within the same ID.


Did it work? ✔ Give a Kudo • Mark as Solution – help others too!

Hi, may I know if the calculation can be done using New Measure instead of New Column with the same outcome?

 

Thank you.

 

Regards,

LC

bhanu_gautam
Super User
Super User

@Tan_LC , Try using

DAX
Duration (mm:ss) =
VAR CurrentDateTime = Table[DateTime]
VAR NextDateTime =
CALCULATE(
MIN(Table[DateTime]),
FILTER(Table,
Table[DateTime] > CurrentDateTime && Table[ID] = EARLIER(Table[ID]))
)
VAR DurationInSeconds = DATEDIFF(CurrentDateTime, NextDateTime, SECOND)
VAR Minutes = QUOTIENT(DurationInSeconds, 60)
VAR Seconds = MOD(DurationInSeconds, 60)
RETURN IF(ISBLANK(NextDateTime),
BLANK(),
FORMAT(Minutes, "00") & ":" & FORMAT(Seconds, "00"))




Did I answer your question? Mark my post as a solution! And Kudos are appreciated

Proud to be a Super User!




LinkedIn






Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.