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
rush
Helper V
Helper V

Split Actual leave days booked in to months based on Start and End Dates

Hi

 

I have a leave table that has actual leave days booked which I would like to split them out into months that were taken when they overlap based on their Start and End Dates.

 

I have a date table that has a column working out workdays "Is a Work Day (Excl. Public Holidays & Weekends)". The date table is joined to the Leave table via Start Date.

 

Expected Result 

 

Please assist.

 

Thank you very much in advance.

e.g.

 

EMPLOYEELEAVE TYPELEAVE PERIODDATE REQUESTEDSTART DATEEND DATEACTUAL DAYS LEAVEDec Leave Jan 2020 Leave LEAVE STATUS
Staff 1AnnualFull Day2019/12/012019/12/172020/01/141899Approved
1 ACCEPTED SOLUTION

Check the file at: https://www.dropbox.com/s/1c3eg4y52d49uel/Leave%20test%20data.pbix?dl=0

 

Measure = CALCULATE(sumx(SUMMARIZE(filter(CROSSJOIN(Table2,Dim_Date),Dim_Date[Calendar_Date] >= Table2[StartDate] && Dim_Date[Calendar_Date]<= Table2[EndDate]),Dim_Date[Calendar_Date],Dim_Date[Is a Work Day (Excl. Public Holidays & Weekends)]),Dim_Date[Is a Work Day (Excl. Public Holidays & Weekends)]),CROSSFILTER(Dim_Date[Calendar_Date],Table2[StartDate],None))

 

View solution in original post

12 REPLIES 12
amitchandak
Super User
Super User

Please look at my blog on HR, this about employee duration, but the same can be used in your case. You can to count dates from date dimension instead of employee id in current/Active employee calculation

 

https://community.powerbi.com/t5/Community-Blog/HR-Analytics-Active-Employee-Hire-and-Termination-tr...

 

Appreciate your Kudos. In case, this is the solution you are looking for, mark it as the Solution.
In case it does not help, please provide additional information and mark me with @

Thanks. My Recent Blogs -Decoding Direct Query - Time Intelligence, Winner Coloring on MAP, HR Analytics, Power BI Working with Non-Standard TimeAnd Comparing Data Across Date Ranges
Proud to be a Datanaut Connect on Linkedin

  Thank you very much for the info but I can't seem to get it right.

I have created my relationships as stated (1 active and 1 inactive) and created the measure needed to count the dates but it is giving me back syntax errors, please see below: 

 

 

No. of days taken = CALCULATE(COUNTx(FILTER(v_LEAVE, v_LEAVE[StartDate]<=max('Dim_Date'[Calendar_Date]) && (ISBLANK( v_LEAVE[EndDate]) || v_LEAVE[EndDate]>max('Dim_Date'[Calendar_Date]))) ,
('Dim_Date'[Calendar_Date])) , v_LEAVE[StartDate])), CROSSFILTER(v_LEAVE[StartDate],'Dim_Date'[Calendar_Date],None))

 

@amitchandak

Try like

No. of days taken = CALCULATE(COUNTx(FILTER(v_LEAVE, v_LEAVE[StartDate]<=max('Dim_Date'[Calendar_Date]) 
				&& (ISBLANK( v_LEAVE[EndDate]) || v_LEAVE[EndDate]>max('Dim_Date'[Calendar_Date]))) ,
 , v_LEAVE[StartDate]), CROSSFILTER(v_LEAVE[StartDate],'Dim_Date'[Calendar_Date],None))

@amitchandakThanks for the correction but it brings up blank values based on that formula.

Can you share sample data and sample output.  I will try.

@amitchandakThank you very much.

Please see link to excel file with sample data and expected output.

 

This will have to be a measure to cater for the various months when comparing.

 

Sample Data 

The formula will be like this

Measure = CALCULATE(sumx(SUMMARIZE(filter(CROSSJOIN(Table2,Dim_Date),Dim_Date[Calendar_Date] >= Table2[StartDate] && Dim_Date[Calendar_Date]<= Table2[EndDate]),Dim_Date[Calendar_Date],Dim_Date[Is a Work Day (Excl. Public Holidays & Weekends)]),Dim_Date[Is a Work Day (Excl. Public Holidays & Weekends)]))

 

Check solution at :https://www.dropbox.com/s/1c3eg4y52d49uel/Leave%20test%20data.pbix?dl=0

 

In case this not working, let me know

 

@amitchandakThan you but it is only working out for the Start Date and not the End Date.

 

Example. Staff01 books leave for the dates below which the measure calculates correctly only for the Start Date (Nov-2019) and not the End Date (Dec-2019).

 

Example.PNG

Check the file at: https://www.dropbox.com/s/1c3eg4y52d49uel/Leave%20test%20data.pbix?dl=0

 

Measure = CALCULATE(sumx(SUMMARIZE(filter(CROSSJOIN(Table2,Dim_Date),Dim_Date[Calendar_Date] >= Table2[StartDate] && Dim_Date[Calendar_Date]<= Table2[EndDate]),Dim_Date[Calendar_Date],Dim_Date[Is a Work Day (Excl. Public Holidays & Weekends)]),Dim_Date[Is a Work Day (Excl. Public Holidays & Weekends)]),CROSSFILTER(Dim_Date[Calendar_Date],Table2[StartDate],None))

 

@amitchandakThank you very much but for some odd reason it is not bringing back the results in my live dataset.

 

Relationship.PNG

 

My DAX query:

 

No. of days taken = 

CALCULATE(sumx(SUMMARIZE(filter(CROSSJOIN(v_LEAVE,Dim_Date),Dim_Date[Calendar_Date] >= v_LEAVE[StartDate] && Dim_Date[Calendar_Date]<= v_LEAVE[EndDate]),Dim_Date[Calendar_Date],Dim_Date[Is a Work Day (Excl. Public Holidays & Weekends)]),Dim_Date[Is a Work Day (Excl. Public Holidays & Weekends)]),CROSSFILTER(Dim_Date[Calendar_Date],v_LEAVE[StartDate],None))

 

Check the Active join. In my case start date join was Active. Also, I made End date join 1 to Many. But later should not have an impact.

@amitchandakThere was another relationship that my leave table was joined to (being my Dim_Staff table) which caused it to not display the results.

Thank you very much for the solution.

 

Have a fantastic day.

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.