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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more

Reply
Anonymous
Not applicable

How to calculate duration of each TASK per ID

Hi Guys,

 

Newbie here, would it be possible to create a measure or calculated column to get the duration of each task per ID ?

 

The dataset would be like this:

 

Capture4.PNG

 

Thank you guys for the help !

1 ACCEPTED SOLUTION

@Anonymous  - If you have multiple tasks and if it goes through proper order, then you can try the below DAX measure.

 

 

Duration =
VAR _CurrentTaskTime =
    CALCULATE ( SELECTEDVALUE ( 'Table'[DATE/TIME] ) )
VAR _PreviousTaskTime =
    CALCULATE (
        MAX ( 'Table'[DATE/TIME] ),
        ALLEXCEPT ( 'Table', 'Table'[ID] ),
        'Table'[DATE/TIME] < _CurrentTaskTime
    )
VAR _timeDifference_IN_Minutes =
    IF (
        ISBLANK ( _PreviousTaskTime ),
        0,
        DATEDIFF ( _PreviousTaskTime, _CurrentTaskTime, MINUTE )
    )
RETURN
    _timeDifference_IN_Minutes

Regards,
Nandu Krishna

View solution in original post

14 REPLIES 14
v-frfei-msft
Community Support
Community Support

Hi @Anonymous ,

 

Here we go:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtE3NNM3MjAyUDC0sDI2UdJRMgRir9Liksw8ICO4JLGoRClWB0WhpZWhKbrCgqL8lNLkksz8PDTVRgZWJhiqc0rzkjPQjTWD2G8EUphaXJyZnIjLAeZWppgq8bnA1BhDOdQJsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Date/Time" = _t, ID = _t, Name = _t, Task = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date/Time", type datetime}, {"ID", Int64.Type}, {"Name", type text}, {"Task", type text}}),
    Partition = Table.Group(Source, {"ID"}, {{"Partition", each Table.AddIndexColumn(_, "Index",1,1), type table}}),
    #"Expanded Partition" = Table.ExpandTableColumn(Partition, "Partition", {"Date/Time", "Name", "Task", "Index"}, {"Partition.Date/Time", "Partition.Name", "Partition.Task", "Partition.Index"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Partition", "Custom", each [Partition.Index]-1),
    #"Merged Queries" = Table.NestedJoin(#"Added Custom", {"ID", "Partition.Index"}, #"Added Custom", {"ID", "Custom"}, "Added Custom", JoinKind.LeftOuter),
    #"Expanded Added Custom" = Table.ExpandTableColumn(#"Merged Queries", "Added Custom", {"Partition.Date/Time"}, {"Added Custom.Partition.Date/Time"}),
    #"Added Custom1" = Table.AddColumn(#"Expanded Added Custom", "Custom.1", each if[#"Added Custom.Partition.Date/Time"] = null then [#"Partition.Date/Time"] else [#"Added Custom.Partition.Date/Time"]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Added Custom.Partition.Date/Time"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Removed Columns",{{"Partition.Date/Time", type datetime}, {"Custom.1", type datetime}}),
    #"Added Custom2" = Table.AddColumn(#"Changed Type1", "Custom.2", each [#"Partition.Date/Time"]-[Custom.1]),
    #"Changed Type2" = Table.TransformColumnTypes(#"Added Custom2",{{"Custom.2", type duration}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type2",{{"ID", Order.Ascending}}),
    #"Removed Columns1" = Table.RemoveColumns(#"Sorted Rows",{"Partition.Index", "Custom", "Custom.1"})
in
    #"Removed Columns1"

 Capture.PNG

 

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.
Anonymous
Not applicable

Hi,

 

Thank for this, Im gonna test it first then I'll get back to you after.

 

Hope this will work.

nandukrishnavs
Community Champion
Community Champion

@Anonymous - 

 

output.JPG

 

Try the below DAX measure.

 

Duration =
VAR _Task =
    CALCULATE ( SELECTEDVALUE ( 'Table'[Task] ) )
VAR _ID =
    CALCULATE ( SELECTEDVALUE ( 'Table'[ID] ) )
VAR _Start =
    CALCULATE (
        SELECTEDVALUE ( 'Table'[DATE/TIME] ),
        ALLEXCEPT ( 'Table', 'Table'[ID] ),
        'Table'[Task] = "Start"
    )
VAR _ProductionTime =
    CALCULATE (
        SELECTEDVALUE ( 'Table'[DATE/TIME] ),
        ALLEXCEPT ( 'Table', 'Table'[ID] ),
        'Table'[Task] = "Production"
    )
VAR _End =
    CALCULATE (
        SELECTEDVALUE ( 'Table'[DATE/TIME] ),
        ALLEXCEPT ( 'Table', 'Table'[ID] ),
        'Table'[Task] = "End"
    )
VAR _ProdDuration =
    DATEDIFF ( _Start, _ProductionTime, MINUTE )
VAR _EndDuration =
    DATEDIFF ( _ProductionTime, _End, MINUTE )
VAR result =
    SWITCH ( _Task, "Start", 0, "Production", _ProdDuration, "End", _EndDuration )
RETURN
    result

 

If you find this solution useful, please mark this as an accepted solution.

 

Regards,

Nandu Krishna

 


Regards,
Nandu Krishna

Anonymous
Not applicable

Hi @nandukrishnavs,

 

Is this also applicable if I add another task like MEETING and there would be times that MEETIING would be the first task than LUNCH and or LUNCH would be the first task than MEETING?

@Anonymous  - If you have multiple tasks and if it goes through proper order, then you can try the below DAX measure.

 

 

Duration =
VAR _CurrentTaskTime =
    CALCULATE ( SELECTEDVALUE ( 'Table'[DATE/TIME] ) )
VAR _PreviousTaskTime =
    CALCULATE (
        MAX ( 'Table'[DATE/TIME] ),
        ALLEXCEPT ( 'Table', 'Table'[ID] ),
        'Table'[DATE/TIME] < _CurrentTaskTime
    )
VAR _timeDifference_IN_Minutes =
    IF (
        ISBLANK ( _PreviousTaskTime ),
        0,
        DATEDIFF ( _PreviousTaskTime, _CurrentTaskTime, MINUTE )
    )
RETURN
    _timeDifference_IN_Minutes

Regards,
Nandu Krishna

Hi Nandu,

 

I am working on a similar measure. Thank you for the solution it worked perfectly for me.

Also, I need to calculate average of the total duration of the ID for every month as a KPI.

Can you please help how to calculate average on a DAX measure?

 

Thank you,

Teja

Anonymous
Not applicable

Hi @nandukrishnavs ,

 

This works really fine, however, when another user ID stamps another task, the duration is still counting. It should be back to 00:00:00 or the timer should start from the last stamp per userID.

 

Please see below screenshot:

Capture90.PNG

 

In above screenshot, notice that the ID is different but the measure still counts from the last stamp which is a different ID.

Anonymous
Not applicable

Warning
---------
Please do not do this with a complex measure as suggested above. You'll be sorry rather sooner than later (trust me).

Please use Power Query to prepare the data upfront.

If you ignore this, please don't come back and don't tell me you have not been fore-warned 🙂

Best
D
Anonymous
Not applicable

hi @Anonymous ,

 

I can't use power query as I am using streaming dataset for this.

Anonymous
Not applicable

Hm... Interesting. You say you can't use PQ and the marked answer to your question contains PQ code. I really find it weird and funny at the same time 🙂

Best
D
Anonymous
Not applicable

Hi @Anonymous ,

 

Oh sorry, I think I've marked the wrong solution. My mistake. Thanks for noticing.

amitchandak
Super User
Super User

@Anonymous , what is the expected output

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube
Anonymous
Not applicable

The output would be like this.

 

Capture5.PNG

Anonymous
Not applicable

This must be a calculated column and should be performed in Power Query, not DAX. Once the model has a table with tasks and their durations, you can start writing measures.

 

And here's how to build correct models where DAX is SIMPLE to write:

https://www.youtube.com/watch?v=78d6mwR8Gt

https://www.youtube.com/watch?v=_quTwyvDfG0


Best
D

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors