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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Ska110
Regular Visitor

Count unique days across multiple date ranges for a given criteria (DAX)

Hello,

 

Another newbie, any help very much appreciated.

I would like to count the unique days in a given month for one (or more), given criteria using DAX.

E.g., for a particular Person id, I would like to calculate the actual number of days that a person received a service in a given month, whilst excluding overlapping days and gaps in periods, i.e., returning the actual 'utilisation' days in each of the month columns. 

So, for the data below, the total number of days in April person 101 is 33, but with overlapping and gap exclusions the actual utilisation is 27 days and listed in April’s column. Very many thanks.

 

Person idStart dateEnd datenumber of daysFirstApr-22May-22Jun-22
10101/04/202220/04/20222012700
10122/04/202228/04/2022702700
10123/04/202228/04/2022602700
20201/05/202203/05/2022310100
20225/05/202231/05/2022700100
30301/04/202205/04/202251500
30301/04/202205/04/202250500
40401/06/202207/06/2022711508
40401/04/202215/04/20221501508
40428/06/202228/06/2022101508

 

Ska110_0-1698861972609.png

 

1 ACCEPTED SOLUTION
Jihwan_Kim
Super User
Super User

Hi, please check the below picture and the attached pbix file.

 

Jihwan_Kim_0-1698864434281.png

 

Jihwan_Kim_1-1698865104682.png

 

expected result measure: =
VAR _currentrowID =
    SELECTEDVALUE ( Data[Person id] )
VAR _currentyearmonth =
    SELECTEDVALUE ( 'Calendar'[Year-Month sort] )
VAR _t =
    SUMMARIZE (
        GENERATE (
            FILTER ( ALLSELECTED ( Data ), Data[Person id] = _currentrowID ),
            FILTER (
                'Calendar',
                'Calendar'[Date] >= Data[Start date]
                    && 'Calendar'[Date] <= Data[End date]
            )
        ),
        'Calendar'[Date],
        'Calendar'[Year-Month sort]
    )
RETURN
    COUNTROWS ( FILTER ( _t, 'Calendar'[Year-Month sort] = _currentyearmonth ) )

 

 


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

4 REPLIES 4
Ska110
Regular Visitor

Many Thanks, this helped great. Sorry for the delayed response. 

Ska110
Regular Visitor

Many thanks for your swift response! Just one issue the column named 'First' should have a 1 in the first row of any new person id. In your model, person id 404 its the second row that returns the 1, i think because the date range order? So first should just be based on the person id column denoting the first row of any person id.  

HI,

I tried to copy the data from the post. But I missed to compare it with the result that you posted. I think you posted something like below.

 

Jihwan_Kim_0-1698896087403.png

 


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.
Jihwan_Kim
Super User
Super User

Hi, please check the below picture and the attached pbix file.

 

Jihwan_Kim_0-1698864434281.png

 

Jihwan_Kim_1-1698865104682.png

 

expected result measure: =
VAR _currentrowID =
    SELECTEDVALUE ( Data[Person id] )
VAR _currentyearmonth =
    SELECTEDVALUE ( 'Calendar'[Year-Month sort] )
VAR _t =
    SUMMARIZE (
        GENERATE (
            FILTER ( ALLSELECTED ( Data ), Data[Person id] = _currentrowID ),
            FILTER (
                'Calendar',
                'Calendar'[Date] >= Data[Start date]
                    && 'Calendar'[Date] <= Data[End date]
            )
        ),
        'Calendar'[Date],
        'Calendar'[Year-Month sort]
    )
RETURN
    COUNTROWS ( FILTER ( _t, 'Calendar'[Year-Month sort] = _currentyearmonth ) )

 

 


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
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 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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