I'm using a table transformer to just calculate week days only. I had some very complicated SQL and this wasn't working reliably (with some weird rounding going on), and then I realised DATEDIFF offered a weekday option - but it doesn't seem to be discounting the weekends.
For example, I was using this:
SELECT DATEDIFF(day, "01/01/2024", NOW())
- 2 * (DATEDIFF(day, "01/01/2024", NOW()) / 7)
- CASE WHEN DAYOFWEEK("01/01/2024") IN (0, 6) THEN 1 ELSE 0 END
- CASE WHEN DAYOFWEEK(NOW()) IN (0, 6) THEN 1 ELSE 0 END
AS 'Days since start of year, excluding weekends'
But as I tested some future dates by swapping NOW() with literal dates, such as "03/02/2024", the calculation was not reliable. The date format I am using in the table transformer is dd/mm/yyyy
If I try and use this, then I don't get back the number of weekdays, only the full number of days. For example, today 29/01/2024 this is returning 28.
SELECT DATEDIFF(weekday, "01/01/2024", NOW())
AS 'Days since start of year, excluding weekends'
How do I just get the number of weekdays, Monday to Friday?