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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
IF
Post Prodigy
Post Prodigy

Week 53

Hi,

 

I get data from SAP BW and use the weekly table to get the data.

 

This code generates 53.2019 and 53.2018, which is not correct. How could I improve it?

 

The code is:

let

    Source = {Number.From(Date.AddWeeks(DateTime.Date(DateTime.LocalNow()), -119))..Number.From(Date.AddWeeks(DateTime.Date(DateTime.LocalNow()), -1))},

    #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), {"Date"}, null, ExtraValues.Error),

    #"Changed Type" = Table.TransformColumnTypes(#"Converted to Table",{{"Date", type date}}),

    #"Added Custom" = Table.AddColumn(#"Changed Type", "YearWeek", each Text.PadStart(Number.ToText(Date.WeekOfYear([Date])),2,"0")

& "." &

Number.ToText(Date.Year([Date]))),

    #"Removed Duplicates" = Table.Distinct(#"Added Custom", {"YearWeek"}),

    #"Sorted Rows" = Table.Sort(#"Removed Duplicates",{{"Date", Order.Descending}}),

    #"Inserted Week of Year" = Table.AddColumn(#"Sorted Rows", "Week of Year", each Date.WeekOfYear([Date]), Int64.Type),

    #"Inserted Year" = Table.AddColumn(#"Filtered Rows", "Year", each Date.Year([Date]), Int64.Type),

    #"Added Custom1" = Table.AddColumn(#"Inserted Year", "WeekYear Sort", each [Year]*100+[Week of Year])

in

    #"Added Custom1"

 

The error code is:

47.jpg

 

Thanks in advance!

 

1 ACCEPTED SOLUTION

Hi @IF ,

 

That has to do with the start of the week check the post below with some explanations about it.

https://www.timeanddate.com/date/week-numbers.htmlhttps://www.quora.com/What-year-will-have-53-weeks

 

Don't forget to mark the correct answer to help others.


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



View solution in original post

5 REPLIES 5
IF
Post Prodigy
Post Prodigy

Hi,

Thanks for the  answer. I am not sure if I did it in the way that you explained, but this doesn't work:

let

Source = {Number.From(Date.AddWeeks(DateTime.Date(DateTime.LocalNow()), -119))..Number.From(Date.AddWeeks(DateTime.Date(DateTime.LocalNow()), -1))},

#"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), {"Date"}, null, ExtraValues.Error),

#"Changed Type" = Table.TransformColumnTypes(#"Converted to Table",{{"Date", type date}}),

#"Added Custom" = if Date.IsLeapYear([Date])
then

Text.PadStart(Number.ToText(Date.WeekOfYear([Date])),2,"0")

& "." &

Number.ToText(Date.Year([Date])) else if Date.WeekOfYear([Date]) = 53 then Text.PadStart("1",2,"0")

& "." &

Number.ToText(Date.Year([Date]) + 1 ) else Text.PadStart(Number.ToText(Date.WeekOfYear([Date])),2,"0")

& "." &

Number.ToText(Date.Year([Date])),


#"Removed Duplicates" = Table.Distinct(#"Added Custom", {"YearWeek"}),

#"Sorted Rows" = Table.Sort(#"Removed Duplicates",{{"Date", Order.Descending}}),

#"Inserted Week of Year" = Table.AddColumn(#"Sorted Rows", "Week of Year", each Date.WeekOfYear([Date]), Int64.Type),

#"Inserted Year" = Table.AddColumn(#"Filtered Rows", "Year", each Date.Year([Date]), Int64.Type),

#"Added Custom1" = Table.AddColumn(#"Inserted Year", "WeekYear Sort", each [Year]*100+[Week of Year])

in

#"Added Custom1"

 

All the best!

Hi @IF ,

 

I have explained myself incorretly you need to change only the custom formula. I have check the full code and in order to have things matching you need to also change additional column of the week and year check full code below:

 

let

    Source = {Number.From(Date.AddWeeks(DateTime.Date(DateTime.LocalNow()), -119))..Number.From(Date.AddWeeks(DateTime.Date(DateTime.LocalNow()), -1))},

    #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), {"Date"}, null, ExtraValues.Error),

    #"Changed Type" = Table.TransformColumnTypes(#"Converted to Table",{{"Date", type date}}),

    #"Added Custom" = Table.AddColumn(#"Changed Type", "YearWeek", each if Date.IsLeapYear([Date])
then

Text.PadStart(Number.ToText(Date.WeekOfYear([Date])),2,"0")

& "." &

Number.ToText(Date.Year([Date])) else if Date.WeekOfYear([Date]) = 53 then Text.PadStart("1",2,"0")

& "." &

Number.ToText(Date.Year([Date]) + 1 ) else Text.PadStart(Number.ToText(Date.WeekOfYear([Date])),2,"0")

& "." &

Number.ToText(Date.Year([Date]))),

    #"Removed Duplicates" = Table.Distinct(#"Added Custom", {"YearWeek"}),

    #"Sorted Rows" = Table.Sort(#"Removed Duplicates",{{"Date", Order.Descending}}),

    #"Inserted Week of Year" = Table.AddColumn(#"Sorted Rows", "Week of Year", each if Date.IsLeapYear([Date])  then Date.WeekOfYear([Date])
 else if Date.WeekOfYear([Date]) = 53 then 1
else

Date.WeekOfYear([Date])),
    #"Inserted Year" = Table.AddColumn(#"Inserted Week of Year", "Year", each if Date.IsLeapYear([Date])  then Date.WeekOfYear([Date])
 else if Date.WeekOfYear([Date]) = 53 then Date.Year([Date]) + 1
else

Date.Year([Date])),

    #"Added Custom1" = Table.AddColumn(#"Inserted Year", "WeekYear Sort", each [Year]*100+[Week of Year])
in
    #"Added Custom1"

Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



MFelix
Super User
Super User

Hi @IF 

 

Not sure if this is the correct option about your data but believe that you only consider 53 weeks on leap years, if this is the fact change your week.year column to the following code:

 

if Date.IsLeapYear([Date])  
then

Text.PadStart(Number.ToText(Date.WeekOfYear([Date])),2,"0")

& "." &

Number.ToText(Date.Year([Date])) else  if  Date.WeekOfYear([Date]) = 53  then  Text.PadStart("1",2,"0")

& "." &

Number.ToText(Date.Year([Date]) + 1 ) else Text.PadStart(Number.ToText(Date.WeekOfYear([Date])),2,"0")

& "." &

Number.ToText(Date.Year([Date]))

 

In this case I'm picking up the week were the number is 53 and making it on the first week of the other year.


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



Thanks. On the other hand I have just realized that 2015 has 53 week but not 2016. 🤔 

All the best!

Hi @IF ,

 

That has to do with the start of the week check the post below with some explanations about it.

https://www.timeanddate.com/date/week-numbers.htmlhttps://www.quora.com/What-year-will-have-53-weeks

 

Don't forget to mark the correct answer to help others.


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors