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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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.


Go to My LinkedIn Page


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.


Go to My LinkedIn Page


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.


Go to My LinkedIn Page


Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors