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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
jbossuyt
Regular Visitor

How to count consecutive dates including 1 day gaps

Hi all,

I have a dataset of antiobiotic consumption per patient

 

ID             Date        Antiobiotic

Patient A May15      Penicillin

Patient A May 16     Penicillin

Patient A May 18     Penicillin

Patient A May 19     Penicillin

Patient B June 3       Amoxicilin
Patient B June 4       Amoxicilin

Patient B June 6       Amoxicillin

Patient B June 8       Amoxicillin

Patient B June 15     Amoxiclin

Patient B   June 16     Amoxicilin

 

What I need is to count the consecutive days antibiotic therapy is given to a patient. Sometimes there is a 1 day gap. The counting should ignore this gap and keep counting as if the gap was filled. But a gap of more than one day should be considered a true gap, the next sequence of dates should then be considered as a new counting.

So I would need a clustering per patient and per antiobotic, that counts the consecutive days the antibiotic is given, but keeps counting when there is a 1 day gap (ans also counts the gap as one day)

 

Desired result (the column "index" is added to identify the different periods of antibiotic therapy per patient per antiobiotic that are seperated in time by more than one day.)

 

ID             Antibiotic        Number of days    Index

Patient A  Penicillin              5 days                 1

Patient B   Amoxicillin          6 days                 1

Patient B   Amoxicillin          2 days                 2

 

Thank you in advance!

Jelle

1 ACCEPTED SOLUTION
Jihwan_Kim
Super User
Super User

Hi,

I am not sure how your semantic model looks like, but I tried to crate a sample pbix file like below.

Please check the below picture and the attached pbix file.

Jihwan_Kim_0-1721271377345.png

 

One of ways is, as the first step, to create a calculated column in data table.

Jihwan_Kim_1-1721272912929.png

 

OFFSET function (DAX) - DAX | Microsoft Learn

 

 

index_cc = 
VAR _prevdate =
    ADDCOLUMNS (
        data,
        "@prevdate",
            MAXX (
                OFFSET (
                    -1,
                    data,
                    ORDERBY ( data[date], ASC ),
                    ,
                    PARTITIONBY ( data[id], data[antibiotic] ),
                    MATCHBY ( data[id], data[date], data[antibiotic] )
                ),
                data[date]
            )
    )
VAR _condition =
    ADDCOLUMNS (
        _prevdate,
        "@condition",
            SWITCH (
                TRUE (),
                ISBLANK ( [@prevdate] ), 0,
                INT ( data[date] - [@prevdate] ) <= 2, 0,
                1
            )
    )
RETURN
    SUMX (
        FILTER (
            _condition,
            data[id] = EARLIER ( data[id] )
                && data[antibiotic] = EARLIER ( data[antibiotic] )
                && data[date] <= EARLIER ( data[date] )
        ),
        [@condition]
    ) + 1

 

 

The second step is to add relevant information to the table visualization and create a measure that shows Number of days.

Jihwan_Kim_2-1721273448449.png

INDEX function (DAX) - DAX | Microsoft Learn

 

 

Number of days: = 
VAR _t =
    SUMMARIZE ( data, 'calendar'[Date] )
VAR _groupstartdate =
    MAXX ( INDEX ( 1, _t, ORDERBY ( 'calendar'[Date], ASC ) ), 'calendar'[Date] )
VAR _groupenddate =
    MAXX ( INDEX ( 1, _t, ORDERBY ( 'calendar'[Date], DESC ) ), 'calendar'[Date] )
RETURN
    IF (
        HASONEVALUE ( 'id'[id] ),
        DATEDIFF ( _groupstartdate - 1, _groupenddate, DAY )
    )

 

 

 


If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

View solution in original post

2 REPLIES 2
ThxAlot
Super User
Super User

ThxAlot_0-1721280040964.png



Expertise = List.Accumulate(


        {Days as from Today},


        {Skills and Knowledge},


        (Current, Everyday) => Current & Day.LearnAndPractise(Everyday)


)



Jihwan_Kim
Super User
Super User

Hi,

I am not sure how your semantic model looks like, but I tried to crate a sample pbix file like below.

Please check the below picture and the attached pbix file.

Jihwan_Kim_0-1721271377345.png

 

One of ways is, as the first step, to create a calculated column in data table.

Jihwan_Kim_1-1721272912929.png

 

OFFSET function (DAX) - DAX | Microsoft Learn

 

 

index_cc = 
VAR _prevdate =
    ADDCOLUMNS (
        data,
        "@prevdate",
            MAXX (
                OFFSET (
                    -1,
                    data,
                    ORDERBY ( data[date], ASC ),
                    ,
                    PARTITIONBY ( data[id], data[antibiotic] ),
                    MATCHBY ( data[id], data[date], data[antibiotic] )
                ),
                data[date]
            )
    )
VAR _condition =
    ADDCOLUMNS (
        _prevdate,
        "@condition",
            SWITCH (
                TRUE (),
                ISBLANK ( [@prevdate] ), 0,
                INT ( data[date] - [@prevdate] ) <= 2, 0,
                1
            )
    )
RETURN
    SUMX (
        FILTER (
            _condition,
            data[id] = EARLIER ( data[id] )
                && data[antibiotic] = EARLIER ( data[antibiotic] )
                && data[date] <= EARLIER ( data[date] )
        ),
        [@condition]
    ) + 1

 

 

The second step is to add relevant information to the table visualization and create a measure that shows Number of days.

Jihwan_Kim_2-1721273448449.png

INDEX function (DAX) - DAX | Microsoft Learn

 

 

Number of days: = 
VAR _t =
    SUMMARIZE ( data, 'calendar'[Date] )
VAR _groupstartdate =
    MAXX ( INDEX ( 1, _t, ORDERBY ( 'calendar'[Date], ASC ) ), 'calendar'[Date] )
VAR _groupenddate =
    MAXX ( INDEX ( 1, _t, ORDERBY ( 'calendar'[Date], DESC ) ), 'calendar'[Date] )
RETURN
    IF (
        HASONEVALUE ( 'id'[id] ),
        DATEDIFF ( _groupstartdate - 1, _groupenddate, DAY )
    )

 

 

 


If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

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

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.