Here you go. Buckle your seatbelts, as it will be a wild ride :-D. This can be written in a more compact form, but I broke down the expression into something that (I hope) is more readable
Sum(
MyCollection,
With(
{start2023: Max(start, Date(2023,1,1)), end2023:Min(end, Date(2023,12,31))},
With(
{
startWeekday:
With(
{wd:Weekday(start2023, StartOfWeek.Sunday)},
If(
wd = 1, start2023 + 1, // Sunday, move to following Monday
wd = 7, start2023 + 2, // Saturday, move to following Monday
start2023)),
endWeekday:
With(
{wd:Weekday(end2023, StartOfWeek.Sunday)},
If(
wd = 1, end2023 - 2, // Sunday, move to previous Friday
wd = 7, end2023 - 1, // Saturday, move to previous Friday
end2023))
},
With(
{
firstMondayAfterStart: startWeekday + 7 - Weekday(startWeekday, StartOfWeek.MondayZero),
firstMondayBeforeEnd: endWeekday - Weekday(endWeekday, StartOfWeek.MondayZero)
},
With(
{
fullWeeks: (firstMondayBeforeEnd - firstMondayAfterStart) / 7,
extraDaysStartWeek: 7 - Weekday(startWeekday, StartOfWeek.Sunday),
extraDaysEndWeek: Weekday(endWeekday, StartOfWeek.Monday)
},
fullWeeks * 5 + extraDaysStartWeek + extraDaysEndWeek)))))
Going from top to bottom in the With function calls:
- Since you only want dates from 2023, the first With 'clamps' the dates so that they are all within this year (start2023 and end2023)
- Since you only want week days, the we move forward Saturday and Sundays to the next Monday (for the start date) and move back to the preceding Friday (startWeekday, endWeekday)
- To make the calculation easier, we take the first Monday after the start date, and the first Monday before (or at) the end date, so that we can calculate whole weeks (firstMondayAfterStart, firstMondayBeforeEnd)
- At that point we can find the number of whole weeks, and find the number of weekdays that we need to count from the start week, and the number of weekdays we need to count from the end week (fullWeeks, extraDaysStartWeek, extraDaysEndWeek)
- Finally, we use those values to calculate the expression that will be used in the Sum function
Hope this helps!