Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Anonymous
Not applicable

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

1 ACCEPTED SOLUTION
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 ) )
    )

1.png

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

View solution in original post

4 REPLIES 4
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] )
    )

1.png

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
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? 

 

photo.PNG 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
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 ) )
    )

1.png

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

Anonymous
Not applicable

Dear @Anonymous ,

 

Thanks for this quick response. This was just the 'little' push I needed. I found a workaround for the highlighted cell issue, so I can continue with my report! 

 

Many thanks! 

 

Best regards,

 

Sander

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors