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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
gudns
Frequent Visitor

How to get how much time a row was in the same state

So, my problem is:

 

I have a bunch of equal IDs, and I want to check when a value is changed in another column. When changed, I need to return how much time the value was the same before changing and say what it was and what it became:


Each ID appears for the same amount, and each one have a status column, here's an example with one of the IDs:

 

idstatusdate_check
1135381099869705xa
1135381099869705xb
1135381099869705xc
1135381099869705xd
1135381099869705ye
1135381099869705yf
1135381099869705yg
1135381099869705zh
1135381099869705zi
1135381099869705zj
1135381099869705zk
1135381099869705zl
1135381099869705zm

 

Basically, I need to check when X became Y, then return the difference of dates e and a (I already have these dates). The same for when Y becomes Z.

1 ACCEPTED SOLUTION

 

NewColumn = 
VAR CurrentStatus_ = Table1[status]
VAR FollowingDate_ =
    CALCULATE (
        MIN ( Table1[date_check] );
        Table1[date_check] > EARLIER ( Table1[date_check] );
        ALLEXCEPT ( Table1; Table1[id] )
    )
VAR FollowingStatus_ =
    CALCULATE (
        VALUES ( Table1[status] );
        Table1[date_check] = FollowingDate_;
        ALLEXCEPT ( Table1; Table1[id] )
    )
VAR Status_Transition_ = CurrentStatus_ <> FollowingStatus_
RETURN
    IF (
        Status_Transition_;
        VAR FollowingStatus_StartDate_ =
            CALCULATE (
                MIN ( Table1[date_check] );
                Table1[status] = FollowingStatus_;
                ALLEXCEPT ( Table1; Table1[id] )
            )
        VAR CurrentStatus_StartDate_ =
            CALCULATE (
                MIN ( Table1[date_check] );
                ALLEXCEPT ( Table1; Table1[id]; Table1[status] )
            )
        RETURN
            IF (
                NOT ISBLANK ( FollowingStatus_StartDate_ );
                FollowingStatus_StartDate_ - CurrentStatus_StartDate_
            )
    )

Code formatted with   www.daxformatter.com

View solution in original post

12 REPLIES 12

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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