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

Level up your Power BI skills this month - build one visual each week and tell better stories with data! Get started

Reply
Syndicate_Admin
Administrator
Administrator

Calculate elapsed time

Can you help me

To create a column where the elapsed time is shown with the format DD, HH:MM:SS , in some data the Close Date field is empty there should be placed the current day.

=IF(J3>0;SI(K3>0; ($K3-$J 3); (TODAY()-J3));"")

joel121960_1-1707089514047.png

1 ACCEPTED SOLUTION
amustafa
Solution Sage
Solution Sage

Here's how to fix it in Excel, M Query or DAX...

 

Excel: =IF(ISBLANK(B2),
IF(A2 > NOW(),
"0, 00:00:00",
TEXT(INT(NOW() - A2),"0") & ", " & TEXT(NOW() - A2, "hh:mm:ss")),
IF(A2 > B2,
"0, 00:00:00",
TEXT(INT(B2 - A2),"0") & ", " & TEXT(B2 - A2, "hh:mm:ss")))

 

amustafa_0-1707103306122.png

 

 

M Query:

 

let
    Source = Excel.Workbook(File.Contents("C:\Users\aliom\OneDrive\Power BI Samples\Time Lapsed with Condition\Sample.xlsx"), null, true),
    Table1_Table = Source{[Item="Table1",Kind="Table"]}[Data],
    #"Changed Type" = Table.TransformColumnTypes(Table1_Table,{{"FECCHA APERTURA", type datetime}, {"FECHA CIERRE", type datetime}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "TIEMPO (dd, hh:mm:ss) MCode", each 
        let
            fechaApertura = [FECCHA APERTURA],
            fechaCierre = if [FECHA CIERRE] <> null then [FECHA CIERRE] else DateTime.LocalNow(),
            duration = if fechaApertura > fechaCierre then #duration(0, 0, 0, 0) else fechaCierre - fechaApertura,
            days = Duration.Days(duration),
            hours = Duration.Hours(duration - #duration(days, 0, 0, 0)),
            minutes = Duration.Minutes(duration - #duration(days, hours, 0, 0)),
            seconds = Duration.Seconds(duration - #duration(days, hours, minutes, 0)),
            durationText = if fechaApertura > fechaCierre then
                                "0, 00:00:00"
                            else
                                Text.From(days) & ", " & Text.PadStart(Text.From(hours), 2, "0") & ":" & 
                                Text.PadStart(Text.From(minutes), 2, "0") & ":" & 
                                Text.PadStart(Text.From(Number.RoundDown(seconds)), 2, "0")
        in
            durationText)
in
    #"Added Custom"

 

 

DAX:

 

TIEMPO (dd, hh:mm:ss) DAX = 
VAR FechaApertura = Table1[FECCHA APERTURA]
VAR FechaCierre = IF(ISBLANK(Table1[FECHA CIERRE]), NOW(), Table1[FECHA CIERRE])
VAR FechaCierreToUse = IF(FechaApertura > NOW(), FechaCierre, NOW())
VAR Duration = FechaCierreToUse - FechaApertura
VAR Days = ROUNDDOWN(Duration, 0)
VAR Hours = HOUR(Duration - (Days * 1))
VAR Minutes = MINUTE(Duration - (Days * 1) - (Hours / 24))
VAR Seconds = SECOND(Duration - (Days * 1) - (Hours / 24) - (Minutes / 1440))
RETURN
IF(
    FechaApertura > FechaCierreToUse,
    "0, 00:00:00",
    FORMAT(Days, "00") & ", " & FORMAT(Hours, "00") & ":" & FORMAT(Minutes, "00") & ":" & FORMAT(Seconds, "00")
)

 

 

amustafa_1-1707103390600.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

1 REPLY 1
amustafa
Solution Sage
Solution Sage

Here's how to fix it in Excel, M Query or DAX...

 

Excel: =IF(ISBLANK(B2),
IF(A2 > NOW(),
"0, 00:00:00",
TEXT(INT(NOW() - A2),"0") & ", " & TEXT(NOW() - A2, "hh:mm:ss")),
IF(A2 > B2,
"0, 00:00:00",
TEXT(INT(B2 - A2),"0") & ", " & TEXT(B2 - A2, "hh:mm:ss")))

 

amustafa_0-1707103306122.png

 

 

M Query:

 

let
    Source = Excel.Workbook(File.Contents("C:\Users\aliom\OneDrive\Power BI Samples\Time Lapsed with Condition\Sample.xlsx"), null, true),
    Table1_Table = Source{[Item="Table1",Kind="Table"]}[Data],
    #"Changed Type" = Table.TransformColumnTypes(Table1_Table,{{"FECCHA APERTURA", type datetime}, {"FECHA CIERRE", type datetime}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "TIEMPO (dd, hh:mm:ss) MCode", each 
        let
            fechaApertura = [FECCHA APERTURA],
            fechaCierre = if [FECHA CIERRE] <> null then [FECHA CIERRE] else DateTime.LocalNow(),
            duration = if fechaApertura > fechaCierre then #duration(0, 0, 0, 0) else fechaCierre - fechaApertura,
            days = Duration.Days(duration),
            hours = Duration.Hours(duration - #duration(days, 0, 0, 0)),
            minutes = Duration.Minutes(duration - #duration(days, hours, 0, 0)),
            seconds = Duration.Seconds(duration - #duration(days, hours, minutes, 0)),
            durationText = if fechaApertura > fechaCierre then
                                "0, 00:00:00"
                            else
                                Text.From(days) & ", " & Text.PadStart(Text.From(hours), 2, "0") & ":" & 
                                Text.PadStart(Text.From(minutes), 2, "0") & ":" & 
                                Text.PadStart(Text.From(Number.RoundDown(seconds)), 2, "0")
        in
            durationText)
in
    #"Added Custom"

 

 

DAX:

 

TIEMPO (dd, hh:mm:ss) DAX = 
VAR FechaApertura = Table1[FECCHA APERTURA]
VAR FechaCierre = IF(ISBLANK(Table1[FECHA CIERRE]), NOW(), Table1[FECHA CIERRE])
VAR FechaCierreToUse = IF(FechaApertura > NOW(), FechaCierre, NOW())
VAR Duration = FechaCierreToUse - FechaApertura
VAR Days = ROUNDDOWN(Duration, 0)
VAR Hours = HOUR(Duration - (Days * 1))
VAR Minutes = MINUTE(Duration - (Days * 1) - (Hours / 24))
VAR Seconds = SECOND(Duration - (Days * 1) - (Hours / 24) - (Minutes / 1440))
RETURN
IF(
    FechaApertura > FechaCierreToUse,
    "0, 00:00:00",
    FORMAT(Days, "00") & ", " & FORMAT(Hours, "00") & ":" & FORMAT(Minutes, "00") & ":" & FORMAT(Seconds, "00")
)

 

 

amustafa_1-1707103390600.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Helpful resources

Announcements
April Power BI Update Carousel

Power BI Monthly Update - April 2026

Check out the April 2026 Power BI update to learn about new features.

Fabric SQL PBI Data Days

Data Days 2026 coming soon!

Sign up to receive a private message when registration opens and key events begin.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.