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

We've captured the moments from FabCon & SQLCon that everyone is talking about, and we are bringing them to the community, live and on-demand. Starts on April 14th. Register now

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
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.