Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Earliest date / Latest date with condition

Dear community members, 

 

I have the following situation: 

A person can have multiple of the same therapy session periods. Each therapy period has his own row in power bi (see sample data below). Now I would like to know what the earliest date and the latest date is per client per kind of therapy. But... when the DaysBetweenTherapyPeriods is more than 15 days, it should be seen as two (or more if it happens more often) individual periods. 

 

Can anyone help me with the formula for the rwo calculated columns? 

 

Original dataset sample: 

 

ClientIDTherapyClientIDTherapyStartDateEndDateDaysBetweenTherapyPeriods
1001AAA1001:AAA1-1-202131-3-2021 
1001AAA1001:AAA1-4-202110-6-20211
1001AAA1001:AAA1-7-202115-8-202121
1001AAA1001:AAA1-11-202131-12-202178
1001AAA1001:AAA1-1-2022 1
1001BBB1001:BBB1-1-202131-3-2021 
1001BBB1001:BBB10-4-202130-9-202110
1002AAA1002:AAA1-1-202031-12-2020 
1002AAA1002:AAA1-1-202130-11-20211
1002AAA1002:AAA1-1-2022 32

 

How the solution should look like (at least, what I hope to see...) :

 

ClientIDTherapyClientIDTherapyStartDateEndDateDaysBetweenTherapyPeriodsEarliestStartDateLatestEndDate
1001AAA1001:AAA1-1-202131-3-2021 1-1-202110-6-2021
1001AAA1001:AAA1-4-202110-6-202111-1-202110-6-2021
1001AAA1001:AAA1-7-202115-8-2021211-7-202115-8-2021
1001AAA1001:AAA1-11-202131-12-2021781-11-202131-12-2021
1001AAA1001:AAA1-1-2022 11-11-202131-12-2021
1001BBB1001:BBB1-1-202131-3-2021 1-1-202130-9-2021
1001BBB1001:BBB10-4-202130-9-2021101-1-202130-9-2021
1002AAA1002:AAA1-1-202031-12-2020 1-1-202030-11-2021
1002AAA1002:AAA1-1-202130-11-202111-1-202030-11-2021
1002AAA1002:AAA1-1-2022 321-1-2022 

 

Hope to hear soon from you.  

 

Best regards,

 

Sander

  • Anonymous's avatar
    Anonymous
    4 years ago

    HI Anonymous,

    You can try to use the new calculated column formulas, I add the 'previous date' and the 'last date' variable to prevent the expression calculated on wrong ranges.

    eStartDate = 
    VAR prevDate =
        CALCULATE (
            MAX ( 'Table'[StartDate] ),
            FILTER (
                'Table',
                [StartDate] <= EARLIER ( 'Table'[StartDate] )
                    && 'Table'[ClientIDTherapy] = EARLIER ( 'Table'[ClientIDTherapy] )
                    && [DaysBetweenTherapyPeriods] >= 15
            )
        )
    VAR _start =
        CALCULATE (
            MIN ( 'Table'[StartDate] ),
            FILTER (
                'Table',
                [StartDate] <= EARLIER ( 'Table'[StartDate] )
                    && [StartDate] >= prevDate
                    && 'Table'[ClientIDTherapy] = EARLIER ( 'Table'[ClientIDTherapy] )
                    && [DaysBetweenTherapyPeriods] < 15
            )
        )
    RETURN
        IF (
            [StartDate] <> BLANK ()
                && [EndDate] <> BLANK (),
            IF ( 'Table'[DaysBetweenTherapyPeriods] > 15, [StartDate], _start )
        )
    
    lEndDate = 
    VAR nextDate =
        CALCULATE (
            MIN ( 'Table'[EndDate] ),
            FILTER (
                'Table',
                [EndDate] > EARLIER ( 'Table'[EndDate] )
                    && 'Table'[ClientIDTherapy] = EARLIER ( 'Table'[ClientIDTherapy] )
                    && [DaysBetweenTherapyPeriods] >= 15
            )
        )
    VAR _lastDate =
        CALCULATE (
            MAX ( 'Table'[EndDate] ),
            FILTER (
                'Table',
                [EndDate] >= EARLIER ( 'Table'[EndDate] )
                    && 'Table'[ClientIDTherapy] = EARLIER ( 'Table'[ClientIDTherapy] )
            )
        )
    VAR _end =
        CALCULATE (
            MAX ( 'Table'[EndDate] ),
            FILTER (
                'Table',
                [EndDate] >= EARLIER ( 'Table'[EndDate] )
                    && [EndDate] <= nextDate
                    && 'Table'[ClientIDTherapy] = EARLIER ( 'Table'[ClientIDTherapy] )
                    && [DaysBetweenTherapyPeriods] < 15
            )
        )
    RETURN
        IF (
            [StartDate] <> BLANK ()
                && [EndDate] <> BLANK (),
            IF ( _end <> BLANK (), _end, IF ( nextDate <> BLANK (), [EndDate], _lastDate ) )
        )

    Notice: the highlight cell result should be 08/16/2021 instead of 07/01/2021 or they will violate the 15-day condition.

    Regards,

    Xiaoxin Sheng

4 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    HI Anonymous,

    Here are two calculated column formulas that can be used to extract the earliest start date and latest end date, you can try it if helps:

    eStartDate = 
    VAR _start =
        CALCULATE (
            MIN ( 'Table'[StartDate] ),
            FILTER (
                'Table',
                [StartDate] <= EARLIER ( 'Table'[StartDate] )
                    && 'Table'[ClientIDTherapy] = EARLIER ( 'Table'[ClientIDTherapy] )
                    && [DaysBetweenTherapyPeriods] < 15
            )
        )
    RETURN
        IF (
            [StartDate] <> BLANK ()
                && [EndDate] <> BLANK (),
            IF ( 'Table'[DaysBetweenTherapyPeriods] > 15, [StartDate], _start )
        )
    
    lEndDate = 
    VAR _end =
        CALCULATE (
            MAX ( 'Table'[EndDate] ),
            FILTER (
                'Table',
                [StartDate] >= EARLIER ( 'Table'[StartDate] )
                    && 'Table'[ClientIDTherapy] = EARLIER ( 'Table'[ClientIDTherapy] )
                    && [DaysBetweenTherapyPeriods] < 15
            )
        )
    RETURN
        IF (
            [StartDate] <> BLANK ()
                && [EndDate] <> BLANK (),
            IF ( _end <> BLANK (), _end, [EndDate] )
        )

    Notice: I add conditions to prevent the expression to calculate on the date ranges which are not finished because I'm not so sure the processing logic of these parts.

    Regards,

    Xiaoxin Sheng

    • Anonymous's avatar
      Anonymous
      Not applicable

      Dear Xiaoxin, 

       

      It is almost perfect... Until the situation occures that a period of more then 15 days will be followed by a new period (for the same client/product). This new period takes the dates of the wrong previous period. I made a visual and table of the output and also inserted the 'correct' data output in a table. Would it be possible to help me once more with the adjustment in the formulas? 

       

       Current data output with the new formulas:

      ClientIDTherapyClientIDTherapyStartDateEndDateDaysBetweenTherapyPeriodseStartDatelEndDate
      1001AAA1001:AAA1-1-202131-3-2021 1-1-202130-8-2021
      1001AAA1001:AAA1-4-202110-6-202111-1-202130-8-2021
      1001AAA1001:AAA1-7-202115-8-2021211-7-202130-8-2021
      1001AAA1001:AAA16-8-202130-8-202111-1-202130-8-2021
      1001AAA1001:AAA1-11-202131-12-2021631-11-202131-12-2021
      1001AAA1001:AAA1-1-2022 1  
      1001BBB1001:BBB1-1-202131-3-2021 1-1-202130-9-2021
      1001BBB1001:BBB10-4-202130-9-2021101-1-202130-9-2021
      1002AAA1002:AAA1-1-202031-12-2020 1-1-202030-11-2021
      1002AAA1002:AAA1-1-202130-11-202111-1-202030-11-2021
      1002AAA1002:AAA1-1-2022 32  

       

      How the output should be.

       

      ClientIDTherapyClientIDTherapyStartDateEndDateDaysBetweenTherapyPeriodseStartDatelEndDate
      1001AAA1001:AAA1-1-202131-3-2021 1-1-202110-6-2021
      1001AAA1001:AAA1-4-202110-6-202111-1-202110-6-2021
      1001AAA1001:AAA1-7-202115-8-2021211-7-202130-8-2021
      1001AAA1001:AAA16-8-202130-8-202111-7-202130-8-2021
      1001AAA1001:AAA1-11-202131-12-2021631-11-202131-12-2021
      1001AAA1001:AAA1-1-2022 1  
      1001BBB1001:BBB1-1-202131-3-2021 1-1-202130-9-2021
      1001BBB1001:BBB10-4-202130-9-2021101-1-202130-9-2021
      1002AAA1002:AAA1-1-202031-12-2020 1-1-202030-11-2021
      1002AAA1002:AAA1-1-202130-11-202111-1-202030-11-2021
      1002AAA1002:AAA1-1-2022 32  

       

      Hope to hear soon from you!

       

      Best regards, 

       

      Sander Nievergeld

      • Anonymous's avatar
        Anonymous
        Not applicable

        HI Anonymous,

        You can try to use the new calculated column formulas, I add the 'previous date' and the 'last date' variable to prevent the expression calculated on wrong ranges.

        eStartDate = 
        VAR prevDate =
            CALCULATE (
                MAX ( 'Table'[StartDate] ),
                FILTER (
                    'Table',
                    [StartDate] <= EARLIER ( 'Table'[StartDate] )
                        && 'Table'[ClientIDTherapy] = EARLIER ( 'Table'[ClientIDTherapy] )
                        && [DaysBetweenTherapyPeriods] >= 15
                )
            )
        VAR _start =
            CALCULATE (
                MIN ( 'Table'[StartDate] ),
                FILTER (
                    'Table',
                    [StartDate] <= EARLIER ( 'Table'[StartDate] )
                        && [StartDate] >= prevDate
                        && 'Table'[ClientIDTherapy] = EARLIER ( 'Table'[ClientIDTherapy] )
                        && [DaysBetweenTherapyPeriods] < 15
                )
            )
        RETURN
            IF (
                [StartDate] <> BLANK ()
                    && [EndDate] <> BLANK (),
                IF ( 'Table'[DaysBetweenTherapyPeriods] > 15, [StartDate], _start )
            )
        
        lEndDate = 
        VAR nextDate =
            CALCULATE (
                MIN ( 'Table'[EndDate] ),
                FILTER (
                    'Table',
                    [EndDate] > EARLIER ( 'Table'[EndDate] )
                        && 'Table'[ClientIDTherapy] = EARLIER ( 'Table'[ClientIDTherapy] )
                        && [DaysBetweenTherapyPeriods] >= 15
                )
            )
        VAR _lastDate =
            CALCULATE (
                MAX ( 'Table'[EndDate] ),
                FILTER (
                    'Table',
                    [EndDate] >= EARLIER ( 'Table'[EndDate] )
                        && 'Table'[ClientIDTherapy] = EARLIER ( 'Table'[ClientIDTherapy] )
                )
            )
        VAR _end =
            CALCULATE (
                MAX ( 'Table'[EndDate] ),
                FILTER (
                    'Table',
                    [EndDate] >= EARLIER ( 'Table'[EndDate] )
                        && [EndDate] <= nextDate
                        && 'Table'[ClientIDTherapy] = EARLIER ( 'Table'[ClientIDTherapy] )
                        && [DaysBetweenTherapyPeriods] < 15
                )
            )
        RETURN
            IF (
                [StartDate] <> BLANK ()
                    && [EndDate] <> BLANK (),
                IF ( _end <> BLANK (), _end, IF ( nextDate <> BLANK (), [EndDate], _lastDate ) )
            )

        Notice: the highlight cell result should be 08/16/2021 instead of 07/01/2021 or they will violate the 15-day condition.

        Regards,

        Xiaoxin Sheng