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

Next up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now

Reply
spehe
Helper I
Helper I

Convert date format 'YYww' to FirstDateOfWeek

Hello, I have a column with data containing the last two digits of a year and a two digit week number. I would like to add a new column called FirstDateOfWeek where the first date of the specified week.

Example: 2207 should convert to 2022-02-18

 

I have tried this code that I got from ChatGPT, but it does not work.

Date.From(Date.From(DateTime.FromText("20" & Text.From([YearWeek], "00-00"), "yyyy-MM-dd")) - Duration.From(Duration.FromDays(Date.DayOfWeek(Date.From(DateTime.FromText("20" & Text.From([YearWeek], "00-00"), "yyyy-MM-dd")) - #datetime(2000, 12, 31, 0, 0, 0)) + Number.FromText("00-01")))

 

 

1 ACCEPTED SOLUTION
FreemanZ
Community Champion
Community Champion

hi @spehe ,

 

The gpt code is for Power Query and Ibendlin's code is for a calculated column with DAX. You may also try the following:

FirstWeekDate = 
VAR _year = 20&LEFT([date], 2)
VAR _yearstart = WEEKDAY(DATE(_year, 1, 1), 2)
VAR _week = RIGHT([date], 2)
VAR _days = _week*7 - _yearstart +1
RETURN DATE(_year, 1, 1)+_days

it worked like:

FreemanZ_0-1698047905644.png

 

about how to add calculated columns:

https://learn.microsoft.com/en-us/power-bi/transform-model/desktop-calculated-tables

 

View solution in original post

3 REPLIES 3
FreemanZ
Community Champion
Community Champion

hi @spehe ,

 

The gpt code is for Power Query and Ibendlin's code is for a calculated column with DAX. You may also try the following:

FirstWeekDate = 
VAR _year = 20&LEFT([date], 2)
VAR _yearstart = WEEKDAY(DATE(_year, 1, 1), 2)
VAR _week = RIGHT([date], 2)
VAR _days = _week*7 - _yearstart +1
RETURN DATE(_year, 1, 1)+_days

it worked like:

FreemanZ_0-1698047905644.png

 

about how to add calculated columns:

https://learn.microsoft.com/en-us/power-bi/transform-model/desktop-calculated-tables

 

lbendlin
Super User
Super User

What is a week, in your opinion?

 

WEEKNUM function (DAX) - DAX | Microsoft Learn

 

Assuming you want to use the European system Week 07 begins on Feb 14 2022

 

Here, the column [Test]  has the numerical value 2207

 

FirstDateOfWeek = 
var y = 2000+ROUNDDOWN([Test]/100,0)
var w = MOD([Test],100)
var z = GENERATESERIES(Date(y,1,1),Date(y,12,31))
var a = ADDCOLUMNS(z,"wn",weeknum([Value],21))
return minx(filter(a,[wn]=w),[Value])

 

Even if you assumed the week to start on a Friday (system 15)  it would still result in Feb 11, not Feb18.

Hello Ibendlin, thanks for helping me with this. As I am located in Sweden, for us, the week start on Monday, and we use system 21.
Adding your suggested code gives me an error message:

spehe_0-1698044680085.png

 

I am totally new to DAX, and I have no idea what it means.

 

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.