Forum Discussion

bartschulz's avatar
bartschulz
Frequent Visitor
6 years ago
Solved

If statement in M code year and weeknumber

if [VERVALDATUM origineel] < DateTime.LocalNow() then Date.WeekOfYear (Date.AddDays (DateTime.LocalNow(),-7)) else Date.WeekOfYear ([VERVALDATUM origineel]) This statement works to get the wee...
  • kriscoupe's avatar
    6 years ago

    Hi bartschulz,

     

    A good way to approach this is to take the year multiplied by 100 and then add the week numbers that you always get that 6 digit format even when week numbers are single digit numbers ie year is 2020 *100 = 202000 + 1 = 202001 for week 1. So something like

     

     

    if [VERVALDATUM origineel] < DateTime.LocalNow()
    then Date.Year([VERVALDATUM origineel]) * 100 + Date.WeekOfYear (Date.AddDays (DateTime.LocalNow(),-7))
    else Date.Year([VERVALDATUM origineel]) * 100 + Date.WeekOfYear ([VERVALDATUM origineel]

     

     

    I don't fully understand the middle criteria why it defaults to using a week before today but hopefully you catch my drift!

     

    Kris