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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
Anonymous
Not applicable

How to calculate duration from previous row per ID

Hi Team,

 

Newbie here,

 

I am using below measure to calculate the duration of each row based on the previous row. However, I can't seem to tweak the measure to be able to calculate duration per ID.

 

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

 

RESULT:

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. It should be when  another user ID stamps another task, the duration should be back to 00:00:00 or the timer should start starting from the previous stamp per userID.

 

It should be like this:

dsd.PNG

 

Thank you guys for answering ! 🙂 

 

 

1 ACCEPTED SOLUTION
v-xicai
Community Support
Community Support

Hi @Anonymous ,

 

You may change your formula like DAX below.

 

Duration =

VAR _PreviousTaskTime =

    CALCULATE (

        MAX ( 'Table'[DATE/TIME] ),

        FILTER (

            ALLSELECTED ( 'Table' ),

            'Table'[ID] = MAX ( 'Table'[ID] )

                && 'Table'[DATE/TIME] <MAX( 'Table'[DATE/TIME])

        )

    )

RETURN

    _timeDifference_IN_Minutes

    = IF (

        ISBLANK ( _PreviousTaskTime ),

        0,

        CALCULATE (

            DATEDIFF ( _PreviousTaskTime, MAX ( 'Table'[DATE/TIME] ), MINUTE ),

            FILTER ( ALLSELECTED ( 'Table' ), 'Table'[ID] = MAX ( 'Table'[ID] ) )

        )

    )

 

Best Regards,

Amy 

 

Community Support Team _ Amy

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

2 REPLIES 2
v-xicai
Community Support
Community Support

Hi @Anonymous ,

 

You may change your formula like DAX below.

 

Duration =

VAR _PreviousTaskTime =

    CALCULATE (

        MAX ( 'Table'[DATE/TIME] ),

        FILTER (

            ALLSELECTED ( 'Table' ),

            'Table'[ID] = MAX ( 'Table'[ID] )

                && 'Table'[DATE/TIME] <MAX( 'Table'[DATE/TIME])

        )

    )

RETURN

    _timeDifference_IN_Minutes

    = IF (

        ISBLANK ( _PreviousTaskTime ),

        0,

        CALCULATE (

            DATEDIFF ( _PreviousTaskTime, MAX ( 'Table'[DATE/TIME] ), MINUTE ),

            FILTER ( ALLSELECTED ( 'Table' ), 'Table'[ID] = MAX ( 'Table'[ID] ) )

        )

    )

 

Best Regards,

Amy 

 

Community Support Team _ Amy

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Greg_Deckler
Super User
Super User

See my article on Mean Time Between Failure (MTBF) which uses EARLIER: http://community.powerbi.com/t5/Community-Blog/Mean-Time-Between-Failure-MTBF-and-Power-BI/ba-p/3395...


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

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

MayPowerBICarousel1

Power BI Monthly Update - May 2024

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