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
Anonymous
Not applicable

Flag Final Status Changing in Time - Based on value change in time, mark only final expected one

I need to set flag on date when final change in status of trip happened. I am getting daily information on trips that will happen in future, however already booked trips can be cancelled. To understand market, I want to have for each Trip and Date only final effective day flagged on new column "Real Book Day"

 

Data set here >> 

 

flag_final_status_change.PNG

 

Thanks a lot for help!

1 ACCEPTED SOLUTION
v-shex-msft
Community Support
Community Support

Hi @Anonymous ,

You can add two calculated columns to achieve your requirement: (first formula is used to find out records whose status changed to 'book', the second one used to find out the last one)

Status Change = 
VAR prevDate =
    CALCULATE (
        MAX ( T2[Report_Date] ),
        FILTER (
            ALLSELECTED ( T2 ),
            [Trip_Id] = EARLIER ( T2[Trip_Id] )
                && [Report_Date] < EARLIER ( [Report_Date] )
        )
    )
VAR currStatus =
    LOOKUPVALUE (
        T2[Trip_Status],
        T2[Trip_Id], [Trip_Id],
        T2[Report_Date], [Report_Date]
    )
VAR prevStatus =
    LOOKUPVALUE (
        T2[Trip_Status],
        T2[Trip_Id], [Trip_Id],
        T2[Report_Date], prevDate
    )
RETURN
    IF ( prevStatus = 1 && currStatus IN { 2, 3 }, 1 )

Real Book Day =
VAR _rank =
    CALCULATE (
        COUNT ( T2[Status Changes] ) + 1,
        FILTER (
            ALLSELECTED ( T2 ),
            [Trip_Id] = EARLIER ( T2[Trip_Id] )
                && [Report_Date] > EARLIER ( T2[Report_Date] )
        )
    )
RETURN
    IF ( _rank = 1, [Status Changes] )

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

View solution in original post

3 REPLIES 3
v-shex-msft
Community Support
Community Support

Hi @Anonymous ,

You can add two calculated columns to achieve your requirement: (first formula is used to find out records whose status changed to 'book', the second one used to find out the last one)

Status Change = 
VAR prevDate =
    CALCULATE (
        MAX ( T2[Report_Date] ),
        FILTER (
            ALLSELECTED ( T2 ),
            [Trip_Id] = EARLIER ( T2[Trip_Id] )
                && [Report_Date] < EARLIER ( [Report_Date] )
        )
    )
VAR currStatus =
    LOOKUPVALUE (
        T2[Trip_Status],
        T2[Trip_Id], [Trip_Id],
        T2[Report_Date], [Report_Date]
    )
VAR prevStatus =
    LOOKUPVALUE (
        T2[Trip_Status],
        T2[Trip_Id], [Trip_Id],
        T2[Report_Date], prevDate
    )
RETURN
    IF ( prevStatus = 1 && currStatus IN { 2, 3 }, 1 )

Real Book Day =
VAR _rank =
    CALCULATE (
        COUNT ( T2[Status Changes] ) + 1,
        FILTER (
            ALLSELECTED ( T2 ),
            [Trip_Id] = EARLIER ( T2[Trip_Id] )
                && [Report_Date] > EARLIER ( T2[Report_Date] )
        )
    )
RETURN
    IF ( _rank = 1, [Status Changes] )

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.
Anonymous
Not applicable

Xiaoxin, thank you very much, solution looks good. But i am getting out of memory error message in First formula: There's not enough memory to complete this operation. Please try again later when there may be more memory available. 

 

My tables are quite large (700k lines every day for last year). Any chance to optimize, that it will itenerate and write by Trip Id and From rather then try to calculate in memory at once?

 

HI @Anonymous ,

My formula contains a few looping calculations so it may spend more system resources. If it iterates on huge amount of records, it may cause the memory issue.

You can try to add an index field to your table and use the index and 'status change' to find out 'real book day':

Real Book Day =
VAR _max =
    CALCULATE (
        MAX ( T2[Index] ),
        FILTER (
            ALLSELECTED ( T2 ),
            [Trip_Id] = EARLIER ( T2[Trip_Id] )
                && [Status Changes] = 1
        )
    )
RETURN
    IF ( T2[Index] = _max, [Status Changes] )

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

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.