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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
moizsherwani
Continued Contributor
Continued Contributor

Difficult Days Off During Pay Period Problem - Direct Query Mode

Hi guys,

 

I am haaving diificulty covering the last leg of a DAX measure (DirectQuery mode). It is to calculated the days a person has taken off during the pay period, so for example see table below

 

EMPLOYEE TABLE

EmployeeID - Employee Name

1                  - Adam

2                  - Bob

 

LEAVE TABLE

EmployeeID - StartDate  - EndDate    - DaysOff

1                  - 1/1/2018   - 1/10/2018 - 9

1                  - 3/1/2018   - 3/2/2018    - 1

1                  - 3/5/2018   - 3/7/2018     - 2

2                  - 3/10/2018 - 3/15/2018   - 5

2                  - 3/25/2018  - 3/26/2018   - 1

NOTE: EndDate is actually the day he will commence work so that day is not counted as a day off

 

I have a date table (datedim) which is not connected to any table and I will for example set the start and end date to 2/20/2018 to 3/20/2018 (Pay Period). I need as a result the total number of days that a person took off during this period. Summary below

 

Employee ID - DayOff

1                    - 3 (which is 1 + 2)

2                    - 5 

 

Please note there is the added complexity that an employee's days off can start from before the Pay Period and end within or even after it as well as starting within the Pay Period and ending after it.

 

Here is where I have gotten so far and it calculated the days off perfectly fine BUT it doesn't add them up for each employee

 

DaysOff =
Var MonthFirstDate = FIRSTDATE(datedim[date])
Var MonthEndDate = LASTDATE(datedim[date])
Var LeaveStartDate = min(LEAVE[startdate])
Var LeaveEndDate = min(LEAVE[enddate])
Var TempStartDate = SWITCH(TRUE(),
LeaveStartDate>=MonthFirstDate&&LeaveEndDate<=MonthEndDate,LeaveStartDate,
LeaveStartDate<=MonthFirstDate&&LeaveEndDate>=MonthEndDate,MonthFirstDate,
LeaveStartDate<=MonthFirstDate&&LeaveEndDate<=MonthEndDate&&LeaveEndDate>=MonthFirstDate,MonthFirstDate,
LeaveStartDate>=MonthFirstDate&&LeaveEndDate>=MonthEndDate&&LeaveStartDate<=MonthEndDate,LeaveStartDate,TODAY())
Var TempEndDate = SWITCH(TRUE(),
LeaveStartDate>=MonthFirstDate&&LeaveEndDate<=MonthEndDate,LeaveEndDate,
LeaveStartDate<=MonthFirstDate&&LeaveEndDate>=MonthEndDate,MonthEndDate+1,
LeaveStartDate<=MonthFirstDate&&LeaveEndDate<=MonthEndDate&&LeaveEndDate>=MonthFirstDate,LeaveEndDate,
LeaveStartDate>=MonthFirstDate&&LeaveEndDate>=MonthEndDate&&LeaveStartDate<=MonthEndDate,MonthEndDate+1,TODAY())

return SUMX(LEAVE,DATEDIFF(TempStartDate,TempEndDate,DAY))

Thanks,

Moiz
Was I able to answer your question? Mark my post as a solution to help others. Kudos if you liked the solution.
1 ACCEPTED SOLUTION
v-yuta-msft
Community Support
Community Support

Hi moizsherwani,

 

Try this DAX formula like below:

 

…

return calculate(SUMX(LEAVE,DATEDIFF(TempStartDate,TempEndDate,DAY)), allexcept(LEAVE[EmployeeID]))

 

Regards,

Jimmy Tao

View solution in original post

1 REPLY 1
v-yuta-msft
Community Support
Community Support

Hi moizsherwani,

 

Try this DAX formula like below:

 

…

return calculate(SUMX(LEAVE,DATEDIFF(TempStartDate,TempEndDate,DAY)), allexcept(LEAVE[EmployeeID]))

 

Regards,

Jimmy Tao

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.