Forum Discussion

ArashZ's avatar
ArashZ
Icon for Helper I rankHelper I
5 years ago
Solved

Changing status among users

Hi,   I have a table like below: Date Status text Status User 2021-01-02 Deactive 1 A 2021-01-24 Active 0 A 2021-02-04 Active 0 B 2021-03-10 Deactive 1 B 2021-03-1...
  • Anonymous's avatar
    Anonymous
    5 years ago

     

     

    DEFINE 
    
    MEASURE Data[# Deactive->Active] = 
    // Number of users that have at least one status
    // of Active in the current period and the previous
    // status (taking into account ALL the time before
    // this status) was Deactive.
    // For a measure that will do the oposite, from
    // active to deactive, just swich the values
    // of the two variables below.
    var FromState = 1 -- "Deactive"
    var ToState = 0 -- "Active"
    return
    SUMX(
        DISTINCT( Data[User] ),
        // This formula returns 1 if a suitable
        // change of state exists and BLANK if
        // not.
        CALCULATE(
            var DateTimesOfInterest =
                FILTER(
                    SUMMARIZE(
                        Data,
                        Data[DateTime],
                        Data[Status]
                    ),
                    Data[Status] = ToState
                )
            // This variable returns the number of 
            // DateTimes from the above table which
            // have a predecessor (record) where
            // the status equals Deactive.
            var SuitableDateTimeCount =
                COUNTX(
                    DateTimesOfInterest,
                    var CurrentDateTime = Data[DateTime]
                    return
                        // This count can only be either 1 or BLANK.
                        // If BLANK, COUNTX will return BLANK.
                        COUNTROWS(
                            FILTER(
                                TOPN(1,
                                    CALCULATETABLE(
                                        Data,
                                        Data[DateTime] < CurrentDateTime,
                                        ALLEXCEPT( Data, Data[User] )
                                    ),
                                    Data[DateTime],
                                    DESC
                                ),
                                Data[Status] = FromState
                            )
                        )
                )
            var ChangeOfStateExists =
                SuitableDateTimeCount > 0
            return
                if( ChangeOfStateExists, 1 )
        )
    )
    
    MEASURE Data[# Active->Deactive] = 
    var FromState = 0 -- "Active"
    var ToState = 1 -- "Deactive"
    return
    SUMX(
        DISTINCT( Data[User] ),
        CALCULATE(
            var DateTimesOfInterest =
                FILTER(
                    SUMMARIZE(
                        Data,
                        Data[DateTime],
                        Data[Status]
                    ),
                    Data[Status] = ToState
                )
            var SuitableDateTimeCount =
                COUNTX(
                    DateTimesOfInterest,
                    var CurrentDateTime = Data[DateTime]
                    return
                        COUNTROWS(
                            FILTER(
                                TOPN(1,
                                    CALCULATETABLE(
                                        Data,
                                        Data[DateTime] < CurrentDateTime,
                                        ALLEXCEPT( Data, Data[User] )
                                    ),
                                    Data[DateTime],
                                    DESC
                                ),
                                Data[Status] = FromState
                            )
                        )
                )
            var ChangeOfStateExists = SuitableDateTimeCount > 0
            return
                if( ChangeOfStateExists, 1 )
        )
    )