Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
bartschulz
Frequent Visitor

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 weeknumbers. But how do i get also the year and weeknumber like 202025 (then) and 202026 (else)

 

using M code

 

Eventualy i want to sort on the weeknumbers chonology

1 ACCEPTED SOLUTION
kriscoupe
Solution Supplier
Solution Supplier

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

View solution in original post

2 REPLIES 2
kriscoupe
Solution Supplier
Solution Supplier

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

@kriscoupe

 

Thanks for the explanation. I saw your solution earlier but did not understand the  *100. Now I know why you use it. 

I was looking the wrong way thought in way like &year&week but your way works.

 

The reason for the then line is that I want to see which payments I am due, instead of  calculate them with the current week payments.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors