June 1, 2018
Hi everyone
I want to find out how many working days (excludes weekends and holidays) between two dates in power query editing. I know there is a formula to use in excel, however I am hoping to have the calculation done behind the scene. Can anyone shed some light on it? the file is attached.
I am still fairly new to power query, as I find it good to keep the file much smaller.
October 5, 2010
Hi Tina,
There isn't a native function in PQ to do this but Imke Feldman has written code to calculate this
Date.Networkdays function for Power Query and Power BI – (thebiccountant.com)
In the attached file I've used her code in a function and then called that function to work out the NetworkDays for your list of holiday requests.
The function requires a list of holiday dates so I amended the table you had so that each date is listed on its own row.
If you are interested, DAX does have the functionality to do this too with native DAX functions.
Regards
Phil
October 5, 2010
Hi Tina,
Yes it excludes weekends.
Our Power Pivot course covers DAX
https://www.myonlinetraininghu.....vot-course
Regards
Phil
Active Member
September 1, 2022
Hi Everyone,
I recently found this post because I'm looking to achieve something similar but not quite the same.
My aim is to calculate the number of hours that passed between 'Request Start Time' and 'Request End Time'. I want to exclude weekends but I don't need to exclude non working hours during the week (i.e. night hours) or extra holidays.
What would be nice to have, would be an additional calculation for cases when the start date is Sunday or end date is Saturday. Then I would actually like to add this weekend day as a working day so add 24 hours.
Similarly, if start date is Saturday or end date is Sunday, I would like to add 2 working days so 48 hours.
However, I think I will be able to find a workaround for this myself so it is not necessary to solve here.
What is causing me the most problems is the fact that most of the solutions I was able to find either don't give the option to exclude weekends only or are based on calculations of working days of the week than multiply the result by 24 to get the hours count. The issue is that if the start date and end date are on the same day, the result is 0 working days and 0*24 is also 0 which is not true result for hours. I really need the exact hours between these 2 dates.
My Excel Networkdays formula is supported by MOD to get the exact numbers of hours:
=(NETWORKDAYS(RequestStartDate,RequestEndDate)-1-MOD(RequestStartDate,1)+MOD(RequestEndDate,1))*24
Is it possible to achieve something similar in Power Query?
Thank you so much!
Moderators
January 31, 2022
Hi Katarzyna,
The attached workbook contains a small example, replicating the results of your formula in PQ. It's not a fancy function, but it's achieved by clicking in the User Interface and writing some fairly easy code for a few custom columns. I guess, you can make more dynamic, but all I wanted to demonstrate for now is that PQ can handle such calculations.
Riny
Active Member
September 1, 2022
Thank you Riny for looking into mu problem and suggesting a solution. I'm truly grateful for this!
However, I'm afraid this doesn't work 100% correctly If I understand your code properly (I'm still a beginner, hence all of these questions), you create a list of days between the start date and end date so that you can remove Saturdays and Sundays and basing on that, calculate hours worked on weekdays.
As a result, calculations are missing for records with a start date or end date on Sunday or Saturday. I'm wondering if there is any solution for that?
I'm attaching a new doc with more data and also with the very final result I'm aiming for.
Thank you again!
Kasia
Moderators
January 31, 2022
Active Member
September 1, 2022
Ah, got you. I had something else in mind, I guess I wasn't specific enough.
In general, I want to exclude the weekend days from the calculations because in theory there is no activity on Saturdays and Sundays. This is why my Excel formula is Networkdays.
However, there are some instances with start date or end date on Saturday or Sunday which clearly means that some activity happened on these days. This is why I not only want to have the calculations for these records but also I need to take a bit different approach. I need to make sure that Saturday or Sundays hours are also calculated - in Excel I simply add 24 or 48 hours for these records to my Networkdays formula.
What I meant previously was that the last part should be easy to be added if there is a solution to the main problem.
Moderators
January 31, 2022
Okay! I took a closer look at the new data and notice 10 records with start and end on a Sunday and 8 records start on a Saturday and end on either a Saturday or a Sunday. Your Final duration column adds only 24 hours for these. Shouldn't that be 48?
If so, the attached file now includes the week-end adjustments. Note that the file size became too big to upload here, so I deleted the bottom 2000 records.
Let me know if this works for you.
Active Member
September 1, 2022
Thank you Riny for looking into this again.
When both start date and end date are on Sunday or Saturday (e.i. the same day), the calculations should add 24 hours. The logic behind that is the fact that this activity just took one day and not whole weekend.
When activity started on Saturday and ended on Sunday, then it's 2 additional days, hence 48 hrs.
I was able to amend your code to get the exact result Excel gave me so all sorted.
Thank you so much!
1 Guest(s)