Forum Discussion
Calculate a value from a schedule for a specifi week
- 4 years ago
Hi Oleander ,
Please add the following custom function to convert the date to ISO week number.
Referencing: M functions to convert between ISO 8601 Week
let getISO8601Week = (someDate as date) => let getDayOfWeek = (d as date) => let result = 1 + Date.DayOfWeek(d, Day.Monday) in result, getNaiveWeek = (inDate as date) => let // monday = 1, sunday = 7 weekday = getDayOfWeek(inDate), weekdayOfJan4th = getDayOfWeek(#date(Date.Year(inDate), 1, 4)), ordinal = Date.DayOfYear(inDate), naiveWeek = Number.RoundDown( (ordinal - weekday + 10) / 7 ) in naiveWeek, thisYear = Date.Year(someDate), priorYear = thisYear - 1, nwn = getNaiveWeek(someDate), lastWeekOfPriorYear = getNaiveWeek(#date(priorYear, 12, 28)), // http://stackoverflow.com/a/34092382/2014893 lastWeekOfThisYear = getNaiveWeek(#date(thisYear, 12, 28)), weekNumber = if nwn < 1 then lastWeekOfPriorYear else if nwn > lastWeekOfThisYear then 1 else nwn in weekNumber in getISO8601WeekThen modify the formula of the custom column.
WeekList = {DateToISOWeek([Start date])..DateToISOWeek([End date])}Best Regards,
Winniz - 4 years ago
Hi,
This works though all dates must be in the same year.
//Göran
Hi there !
I got half way due to that I can get the week nr from your solution but as you say and a occuring issue is that our Week numbering is not the same .
If I use the ISOWeeknum weeknum),21 for us I get the week 16 from 2022-04-18 to 2022-04-24 . If I can solve this week calculation I'll be a lot closer to a solution.
Hi Oleander ,
Please add the following custom function to convert the date to ISO week number.
Referencing: M functions to convert between ISO 8601 Week
let
getISO8601Week = (someDate as date) =>
let
getDayOfWeek = (d as date) =>
let
result = 1 + Date.DayOfWeek(d, Day.Monday)
in
result,
getNaiveWeek = (inDate as date) =>
let
// monday = 1, sunday = 7
weekday = getDayOfWeek(inDate),
weekdayOfJan4th = getDayOfWeek(#date(Date.Year(inDate), 1, 4)),
ordinal = Date.DayOfYear(inDate),
naiveWeek = Number.RoundDown(
(ordinal - weekday + 10) / 7
)
in
naiveWeek,
thisYear = Date.Year(someDate),
priorYear = thisYear - 1,
nwn = getNaiveWeek(someDate),
lastWeekOfPriorYear =
getNaiveWeek(#date(priorYear, 12, 28)),
// http://stackoverflow.com/a/34092382/2014893
lastWeekOfThisYear =
getNaiveWeek(#date(thisYear, 12, 28)),
weekNumber =
if
nwn < 1
then
lastWeekOfPriorYear
else
if
nwn > lastWeekOfThisYear
then
1
else
nwn
in
weekNumber
in
getISO8601Week
Then modify the formula of the custom column.
WeekList = {DateToISOWeek([Start date])..DateToISOWeek([End date])}
Best Regards,
Winniz
- Oleander4 years ago
Helper I
Hi,
This works though all dates must be in the same year.
//Göran