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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Anonymous
Not applicable

Calculating Days between dates

Hello All, 

 

I have 2 data sets - first is employees data in which I have empoyee ID, start date and end date.

 

employee data.PNG

 

and second data set which i have is a calendar table in which week start date and week end date is mentioned and i have a column which contains the dates on which there was holiday.

 

period data.PNGholiday data.PNG

 

So what I need to find is i have to check each employee independently and check from calendar table and holiday table that, if start date of employee is less than week start date and end date of employee is greater than week end date and also check the condition if there was any holiday in that week or not and I have also to exclude weekend.

lets suppose the employee start date is 1/1/2017 and end date is 1/1/2020 and there was no holiday in first week of 2019, so in first week of 2019 which starts from 1/1/2019 to 1/6/2019 i have to calculate how many days an employee came excluding weekends

 so the result comes out to be 4 days and i have to calculate this for each employee

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

HI @Anonymous ,

 

For my sample, I just extract the 2019 holidays. You could reference it to modify to meet your actual situations. There are two formulas. One is calculating total working days and another one is the measure of weekly working days.

Calendar =
VAR t =
    CALENDAR ( DATE ( 2019, 1, 1 ), DATE ( 2019, 12, 31 ) )
RETURN
    ADDCOLUMNS ( t, "Working day", WEEKDAY ( [Date], 2 ) )
Weekly working days =
VAR Star_Date =
    CALCULATE (
        MIN ( 'Calendar'[Date] ),
        FILTER (
            ALLEXCEPT ( 'Calendar', 'Date'[Period] ),
            'Calendar'[Date] >= MAX ( 'Date'[Date from] )
                && 'Calendar'[Date] <= MAX ( 'Date'[Date to] )
                && 'Calendar'[No-working day] = 0
        )
    )
VAR End_Date =
    CALCULATE (
        MAX ( 'Calendar'[Date] ),
        FILTER (
            ALLEXCEPT ( 'Calendar', 'Date'[Period] ),
            'Calendar'[Date] >= MAX ( 'Date'[Date from] )
                && 'Calendar'[Date] <= MAX ( 'Date'[Date to] )
                && 'Calendar'[No-working day] = 0
        )
    )
RETURN
    DATEDIFF ( Star_Date, End_Date + 1, DAY )
Total working days =
VAR Star_Date =
    MAX ( Employees[Start Date] )
VAR End_Date =
    MAX ( Employees[End Date] )
VAR today =
    TODAY ()
VAR diff =
    CALCULATE (
        DATEDIFF ( Star_Date, End_Date, DAY ),
        FILTER ( 'Calendar', 'Calendar'[No-working day] = 0 )
    )
VAR diff_today =
    CALCULATE (
        DATEDIFF ( Star_Date, today, DAY ),
        FILTER ( 'Calendar', 'Calendar'[No-working day] = 0 )
    )
RETURN
    IF (
        End_Date = BLANK (),
        diff_today,
        IF ( End_Date > DATE ( 2019, 1, 1 ), diff )
    )

5.PNG

 

 

Best Regards,
Xue Ding
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-xuding-msft
Community Support
Community Support

HI @Anonymous ,

 

For my sample, I just extract the 2019 holidays. You could reference it to modify to meet your actual situations. There are two formulas. One is calculating total working days and another one is the measure of weekly working days.

Calendar =
VAR t =
    CALENDAR ( DATE ( 2019, 1, 1 ), DATE ( 2019, 12, 31 ) )
RETURN
    ADDCOLUMNS ( t, "Working day", WEEKDAY ( [Date], 2 ) )
Weekly working days =
VAR Star_Date =
    CALCULATE (
        MIN ( 'Calendar'[Date] ),
        FILTER (
            ALLEXCEPT ( 'Calendar', 'Date'[Period] ),
            'Calendar'[Date] >= MAX ( 'Date'[Date from] )
                && 'Calendar'[Date] <= MAX ( 'Date'[Date to] )
                && 'Calendar'[No-working day] = 0
        )
    )
VAR End_Date =
    CALCULATE (
        MAX ( 'Calendar'[Date] ),
        FILTER (
            ALLEXCEPT ( 'Calendar', 'Date'[Period] ),
            'Calendar'[Date] >= MAX ( 'Date'[Date from] )
                && 'Calendar'[Date] <= MAX ( 'Date'[Date to] )
                && 'Calendar'[No-working day] = 0
        )
    )
RETURN
    DATEDIFF ( Star_Date, End_Date + 1, DAY )
Total working days =
VAR Star_Date =
    MAX ( Employees[Start Date] )
VAR End_Date =
    MAX ( Employees[End Date] )
VAR today =
    TODAY ()
VAR diff =
    CALCULATE (
        DATEDIFF ( Star_Date, End_Date, DAY ),
        FILTER ( 'Calendar', 'Calendar'[No-working day] = 0 )
    )
VAR diff_today =
    CALCULATE (
        DATEDIFF ( Star_Date, today, DAY ),
        FILTER ( 'Calendar', 'Calendar'[No-working day] = 0 )
    )
RETURN
    IF (
        End_Date = BLANK (),
        diff_today,
        IF ( End_Date > DATE ( 2019, 1, 1 ), diff )
    )

5.PNG

 

 

Best Regards,
Xue Ding
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
amitchandak
Super User
Super User

@Anonymous , I would advise to have date calendar and update your week end holiday in that.

And then check 2nd page of the pbix. How to get working days between dates

https://www.dropbox.com/s/y47ah38sr157l7t/Order_delivery_date_diff.pbix?dl=0

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.