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
Anonymous
Not applicable

How to match date to a range of dates and count number of days

Hello,

 

I have the following two tables. The first table has ranges of dates which correspond to the shift dates of each person. Each shift date range is always continuous, for example, from 3 January 2022 to 7 January 2022, from 11 January 2022 to 15 January 2022, etc.

 

Work Shift DateNameValueSubmissionsFeedback
03 January 2022Lizui142
04 January 2022Lizui175
05 January 2022Lizui188
06 January 2022Lizui156
07 January 2022Lizui175
11 January 2022Tegalpapak188
12 January 2022Tegalpapak156
13 January 2022Tegalpapak138
14 January 2022Tegalpapak133
15 January 2022Tegalpapak111
10 January 2022Lizui142
11 January 2022Lizui175
12 January 2022Lizui188
13 January 2022Lizui156
30 January 2022Frei Paulo165
31 January 2022Frei Paulo121
01 February 2022Frei Paulo134
02 February 2022Frei Paulo153
05 February 2022Gangarampur137
06 February 2022Gangarampur112
07 February 2022Gangarampur173
08 February 2022Gangarampur199

 

This the second table which shows the Reporting date, which is the date on which the person reported his shift location.

 

Reporting DateNameLocation
07 January 2022LizuiPalembang
14 January 2022TegalpapakSapporo
11 January 2022LizuiVijayawada
31 January 2022Frei PauloJohannesburg
07 February 2022GangarampurChongqing

 

The objective is to compare the Reporting date (from table 2) with the range of dates from the first table and if the Reporting date matches any of the dates, then the number of days in that date range is counted from table 1. The final results table is shown below. The table shows the name and the number of days spent per location per month, so if the shift period overlaps two months, then the number of days should be split between those two months, such as, for Frei Paulo.

 

Any help would be much appreciated!

 

NameYearMonthNumber of days per location per monthLocation
Lizui2022January5Palembang
Tegalpapak2022January5Sapporo
Lizui2022January4Vijayawada
Frei Paulo2022January2Johannesburg
Frei Paulo2022February2Johannesburg
Gangarampur2022February4Chongqing

 

1 ACCEPTED SOLUTION
v-yiruan-msft
Community Support
Community Support

Hi @Anonymous ,

I created a sample pbix file(see attachment) for you, please check whether that is what you want.

1. Get the min date and max date for table1 group by Name and continuous dates

yingyinr_1-1646728914321.png

2. Create a measure as below to get the number of days which reporting days between min date and max date

Number of days per location per month = 
VAR _year =
    SELECTEDVALUE ( 'Date'[Year] )
VAR _month =
    SELECTEDVALUE ( 'Date'[Month] )
RETURN
    CALCULATE (
        SUM ( 'Work Shift'[Value] ),
        FILTER (
            'Work Shift',
            YEAR ( 'Work Shift'[Work Shift Date] ) = _year
                && 'Work Shift'[Work Shift Date].[Month] = _month
                && 'Work Shift'[Mindate] <= MIN ( 'Reporting'[Reporting Date] )
                && 'Work Shift'[Maxdate] >= MIN ( 'Reporting'[Reporting Date] )
        )
    )

yingyinr_0-1646728879187.png

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

2 REPLIES 2
v-yiruan-msft
Community Support
Community Support

Hi @Anonymous ,

I created a sample pbix file(see attachment) for you, please check whether that is what you want.

1. Get the min date and max date for table1 group by Name and continuous dates

yingyinr_1-1646728914321.png

2. Create a measure as below to get the number of days which reporting days between min date and max date

Number of days per location per month = 
VAR _year =
    SELECTEDVALUE ( 'Date'[Year] )
VAR _month =
    SELECTEDVALUE ( 'Date'[Month] )
RETURN
    CALCULATE (
        SUM ( 'Work Shift'[Value] ),
        FILTER (
            'Work Shift',
            YEAR ( 'Work Shift'[Work Shift Date] ) = _year
                && 'Work Shift'[Work Shift Date].[Month] = _month
                && 'Work Shift'[Mindate] <= MIN ( 'Reporting'[Reporting Date] )
                && 'Work Shift'[Maxdate] >= MIN ( 'Reporting'[Reporting Date] )
        )
    )

yingyinr_0-1646728879187.png

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Greg_Deckler
Super User
Super User

@Anonymous Maybe:

Number of Days Measure =
  VAR __Name = MAX('Table2'[Name])
  VAR __Date = MAX('Table2'[Reporting Date])
  VAR __Table = 
    ADDCOLUMNS(
      FILTER('Table1',[Name] = __Name),
      "__Days",
          VAR __WorkDate = [Work Shift Date]
          VAR __Previous = MAXX(FILTER('Table1',[Name]=__Name) && [Work Shift Date] < __WorkDate)
        RETURN
          ([Work Shift Date] - __Previous) * 1.
  VAR __MinDate = MAXX(FILTER(__Table, [Work Shift Date] <= __Date && [__Days] <> 1),[Work Shift Date])
  VAR __MaxDate = MINX(FILTER(__Table, [Work Shift Date] <= __Date && [__Days] <> 1),[Work Shift Date])
RETURN
  (__MaxDate - __MinDate) * 1. + 1

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

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.