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
rsreepathi
Frequent Visitor

Return the latest date based on status change

Hi,

 

This is my first post, below is my data and this is what I want to achieve:

 

for each ID->find the latest status-> corresponding date field

 

NameIDdatestatus
setting 1293/9/2021 1.42 PM0
setting 1293/9/2021 1.43 PM0
setting 1293/9/2021 1.44 PM0
setting 1293/9/2021 1.45 PM1
setting 1293/9/2021 1.46 PM1
setting 1293/9/2021 1.47 PM1
setting 1293/9/2021 1.48 PM1
setting 1293/9/2021 1.49 PM1
setting 2303/9/2021 1.42 PM0
setting 2303/9/2021 1.43 PM0
setting 2303/9/2021 1.44 PM0
setting 2303/9/2021 1.45 PM1
setting 2303/9/2021 1.46 PM1
setting 2303/9/2021 1.47 PM0
setting 2303/9/2021 1.48 PM0
setting 2303/9/2021 1.49 PM0
setting 2303/9/2021 1.50 PM1
setting 2303/9/2021 1.51 PM0

 

Based on the above data for setting 1-> the latest state change is from 0 to 1-> date field to return is 3/9/2021 1.45 PM

similarly for setting 2 -> the latest state change is from 1 to 0 -> date field to return is 3/9/2021 1.50 PM

 

data to extract

setting 1->3/9/2021 1.45PM

setting 2 ->3/9/2021 1.50PM

 

I am able to get the state to go from either 0 to 1 or 1 to 0, any help would be appreciated.

 

Thanks 

RS

1 ACCEPTED SOLUTION
jameszhang0805
Resolver IV
Resolver IV

@rsreepathi 
Please try the below code:

jameszhang0805_0-1615449809796.png

LatestDate = 
VAR _Table =
    ADDCOLUMNS (
        SUMMARIZE ( 'Data', Data[Name], Data[date], Data[status] ),
        "@PreviousStatus",
            VAR _PreviousDate =
                CALCULATE (
                    MAX ( 'Data'[date] ),
                    ALL ( 'Data' ),
                    'Data'[date] < EARLIER ( Data[date] )
                )
            VAR _PreviousStatus =
                CALCULATE (
                    MAX ( 'Data'[status] ),
                    ALLEXCEPT ( Data, 'Data'[Name], Data[date] ),
                    'Data'[date] = _PreviousDate
                )
            RETURN
                _PreviousStatus
    )
VAR _FilteredTable =
    FILTER ( _Table, 'Data'[status] <> [@PreviousStatus] && 'Data'[status] = 1 )
RETURN
    IF( HASONEVALUE( Data[Name] ) , CALCULATE ( MAX ( 'Data'[date] ), _FilteredTable ) )       

View solution in original post

3 REPLIES 3
rsreepathi
Frequent Visitor

thank you @jameszhang0805 that worked!

jameszhang0805
Resolver IV
Resolver IV

@rsreepathi 
Please try the below code:

jameszhang0805_0-1615449809796.png

LatestDate = 
VAR _Table =
    ADDCOLUMNS (
        SUMMARIZE ( 'Data', Data[Name], Data[date], Data[status] ),
        "@PreviousStatus",
            VAR _PreviousDate =
                CALCULATE (
                    MAX ( 'Data'[date] ),
                    ALL ( 'Data' ),
                    'Data'[date] < EARLIER ( Data[date] )
                )
            VAR _PreviousStatus =
                CALCULATE (
                    MAX ( 'Data'[status] ),
                    ALLEXCEPT ( Data, 'Data'[Name], Data[date] ),
                    'Data'[date] = _PreviousDate
                )
            RETURN
                _PreviousStatus
    )
VAR _FilteredTable =
    FILTER ( _Table, 'Data'[status] <> [@PreviousStatus] && 'Data'[status] = 1 )
RETURN
    IF( HASONEVALUE( Data[Name] ) , CALCULATE ( MAX ( 'Data'[date] ), _FilteredTable ) )       
amitchandak
Super User
Super User

@rsreepathi , with name this measure should work

calculate(max(table[date]), filter(Table, Table[status]=1))

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.