Download page Calculating the number of workdays in a period of time.
Calculating the number of workdays in a period of time
Use case
You have a table containing a period of time (start date and end date). You need to count the number of workdays during this time period.
More Advanced option:
You have an additional table with national holidays which you also shouldn't consider as workdays.
Solution
Switch the page to the edit mode.
Insert the Table Transformer macro and paste the table within the macro body.
Select the macro and click Edit.
Switch to the SQL query tab.
Enter the following SQL query:
SELECT *,
'Days' -
2 * (('Days' / 7)::integer) -
CASE
WHEN 'Days' % 7 = 0 THEN 0
WHEN 'Start date'::Date->getDay() = 0 THEN 1
WHEN 'Start date'::Date->getDay() + 'Days' % 7 = 7 THEN 1
WHEN 'Start date'::Date->getDay() + 'Days' % 7 > 7 THEN 2
ELSE 0
END AS 'Work days'
FROM
(SELECT *,
ROUND((T1.'End date' - T1.'Start date') / "1d") + 1 AS 'Days'
FROM T1)
SELECT *,
'Days' - 2 * (('Days' / 7)::integer) -
CASE
WHEN 'Days' % 7 = 0 THEN 0
WHEN 'Start date'::Date->getDay() = 0 THEN 1
WHEN 'Start date'::Date->getDay() + 'Days' % 7 = 7 THEN 1
WHEN 'Start date'::Date->getDay() + 'Days' % 7 > 7 THEN 2
ELSE 0
END
-
(SELECT COUNT(*) FROM
(SELECT *,'Holiday'::Date->getDay() AS 'Day' FROM T2)
WHERE 'Holiday' >= TT.'Start date' AND 'Holiday' <= TT.'End date'
AND 'Day' > 0 AND 'Day' < 6)
AS 'Work days'
FROM
(SELECT *, ROUND(('End date' - 'Start date') / "1d") + 1 AS 'Days' FROM T1) AS TT
SQL
Cookie Notice
This website uses cookies for analytics, personalization and advertising. By continuing to browse, you agree to our use of cookies. Learn more