Forum Discussion

andybamber's avatar
andybamber
Icon for Helper III rankHelper III
4 years ago
Solved

Help on How to Create a Measure Based on Start & End Date

Hello!   Hoping someone could help me with some advice on how to build a measure   I have a table of users, with start and/or end timestamps. The table is an access history for the users.   Som...
  • tamerj1's avatar
    4 years ago

    Hi andybamber 
    Here is the sample file with the solution https://www.dropbox.com/t/J56gVd2IbetcntOR
    For active users I assume a rolling number that, by time, increases when new users join and decreases when users leave. Therefore, when you say:
    "So for instance if I looked at November 2021 i would see a distinct count of 2"
    I would consider that you mean 2 members will be added to the moving (rolling) total. I hope this is what you mean. 
    One more thing need mention; this won't work properly if you have more than two start dates. The results won't be so accurate. If this is the case please provide data that reflects the actual scenario.

    New Users = 
    IF ( 
        HASONEVALUE ( Date_Table[MonthInCalendar] ),
        DISTINCTCOUNT ( 'Table'[User] )
    )
    Active Users = 
    VAR FirstDateInFilter =
        MIN ( Date_Table[Date] )
    VAR LastDateInFilter =
        MAX  ( Date_Table[Date] )
    RETURN
        IF (
            HASONEVALUE ( Date_Table[MonthInCalendar] ),
            CALCULATE (
                SUMX ( 
                    VALUES ( 'Table'[User] ),
                    VAR StartDate = 
                        CALCULATE ( MIN ( 'Table'[StartTimestamp] ) )
                    VAR EndDate =
                        CALCULATE ( MAX ('Table'[EndTimestamp] ) )
                    RETURN
                        IF (
                            StartDate <= LastDateInFilter
                                && OR ( ISBLANK ( EndDate ), EndDate >= FirstDateInFilter ),
                            1,
                            0
                        )
                ),
                REMOVEFILTERS ( Date_Table )
            )
        )

     

  • tamerj1's avatar
    tamerj1
    4 years ago

    Hi andybamber 

    Here is the updated file https://www.dropbox.com/t/1g0PtekIviN7lpGU
    Not sure if I correctly understand the what is required but I did the following:

    1. I modified the code [New Users] in oder to include duplicates in the count. So instead of counting user ID's we now count the number of rows.
    2. The [Active Users] Measure has been updated accordingly.
    3. The [Leaving Users] Measure has been added.
    4. Fixed the Total to show correct values.

    I hope this satisfies your requirements.

     

    New Users = 
    SUMX ( 
        VALUES ( Date_Table[MonthInCalendar] ),
        CALCULATE ( COUNTROWS ( 'Table' ) )
    )
    Leaving Users = 
    VAR CurrentDateRange = VALUES ( Date_Table[Date] )
    RETURN
        CALCULATE ( 
            SUMX ( 
                'Table',
                VAR EndDate = 'Table'[EndTimestamp]
                RETURN
                    IF (
                        DATE ( YEAR ( EndDate ), MONTH ( EndDate ), DAY ( EndDate ) ) IN  CurrentDateRange
                            && NOT ISBLANK ( 'Table'[EndTimestamp] ),
                        1
                    )
            ),
            REMOVEFILTERS ( Date_Table )
        )
    Active Users = 
    VAR LastDateInFilter =
        MAX  ( Date_Table[Date] )
    VAR FirstDateInFilter =
        DATE ( YEAR ( LastDateInFilter ), MONTH ( LastDateInFilter ) - 1, 1 )
    RETURN
        CALCULATE (
            SUMX ( 
                'Table',
                VAR StartDate = 
                    'Table'[StartTimestamp]
                VAR EndDate =
                    'Table'[EndTimestamp]
                RETURN
                    IF (
                        StartDate <= LastDateInFilter
                            && OR ( ISBLANK ( EndDate ), EndDate >= FirstDateInFilter ),
                        1
                    )
            ),
            REMOVEFILTERS ( Date_Table )
        )

     

    Have a great day!