Forum Discussion
barakm
2 years agoFrequent Visitor
Converting week and years in M code
Dear Team,I'm new to Power BI, trying to figure out the issue. I have a column named "Period Week" with data looks like 23.2024 means the 23th week of the year 2024 I wish to convert it a regular...
- 2 years ago
and try this code in power query
[ parts = Text.Split([Week Year], "."), weekNum = Number.FromText(parts{0}), yearNum = Number.FromText(parts{1}), startOfYear = #date(yearNum, 1, 1), dayOfWeek = Date.DayOfWeek(startOfYear, Day.Sunday), daysOffset = if dayOfWeek = 1 then 0 else 7 - dayOfWeek, firstSunday = Date.AddDays(startOfYear, daysOffset), weekStartDate = Date.AddDays(firstSunday, (weekNum - 1) * 7) ] [weekStartDate]
Anonymous
2 years agoNot applicable
I would add a new query with a list of all of the dates from your first year (let's say 2021) to the most recent year, so = List.Dates(#date(2021, 1, 1), Duration.TotalDays(Date.From(DateTime.LocalNow())-#date(2021, 1, 1)), 1)
Use the GUI option "To Table", name that column "Dates", and then add a column using the function each Date.WeekOfYear([Dates])
If you want to, group by Week, and choose the Max aggregation for [Dates].
Now you can return to your original query, split your [Period Week] column by ".", and make the types "Int64.Type" (whole number).
Now you can merge with your Dates query on Week Number and year.
--Nate