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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
YuanG
Helper I
Helper I

keep leading zero of convert data

Hi,

 

I want to convert my date data in to week number and year like 2016-01, i've creat a column calsemaine = FORMAT('Merge date'[Date];"yyyy-ww"), but the result show with no leading zero of my week number. It works with month and day, i don't know why it can't with week.

 

Thanks in advance!

2 ACCEPTED SOLUTIONS
Vvelarde
Community Champion
Community Champion

Hi @YuanG; a solution for this:

 

WeekNumberFormat =
IF (
    LEN ( FORMAT ( Calendario[Date]; "yyyy-ww" ) ) = 6;
    MID ( FORMAT ( Calendario[Date]; "yyyy-ww" )15 ) & "0"
        MID ( FORMAT ( Calendario[Date]; "yyyy-ww" )62 );
    FORMAT ( Calendario[Date]; "yyyy-ww" 
)




Lima - Peru

View solution in original post

KHorseman
Community Champion
Community Champion

@Sean I know I did it for year-and-month using FORMAT. As far as I can remember, the only example for year-and-week that you might be thinking of would be if you actually took the time to read through that crazy query code I use for my very slow standard date table. That has a custom column called WeekNumber, which is...

 

 

= Int64.From(
	Text.From(
		Date.Year(
			Date.EndOfWeek([Date])
		)
	)
	&
	Text.PadStart(
		Text.From(
			Date.WeekOfYear(
				Date.EndOfWeek([Date])
			)
		),
		2,
		"0"
	)
)

 

 

That formula returns an integer value without the hyphen in the middle. You should be able to modify it pretty easily to remove the integer conversion and add a hyphen character to the concatenation.

 

In DAX it's...

WeekNum = IF(
	WEEKNUM(DateTable[Date]) < 10, 
	YEAR(DateTable[Date]) & "-0" & WEEKNUM(DateTable[Date]),
	YEAR(DateTable[Date]) & "-" & WEEKNUM(DateTable[Date])
)

 





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

Proud to be a Super User!




View solution in original post

4 REPLIES 4
Vvelarde
Community Champion
Community Champion

Hi @YuanG; a solution for this:

 

WeekNumberFormat =
IF (
    LEN ( FORMAT ( Calendario[Date]; "yyyy-ww" ) ) = 6;
    MID ( FORMAT ( Calendario[Date]; "yyyy-ww" )15 ) & "0"
        MID ( FORMAT ( Calendario[Date]; "yyyy-ww" )62 );
    FORMAT ( Calendario[Date]; "yyyy-ww" 
)




Lima - Peru
Sean
Community Champion
Community Champion

@KHorseman If I recall you had a post addressing this Smiley Happy not with FORMAT but with an IF statement

 

I don't remember if it was M or DAX solution - and I can't seem to find that post? I'm pretty certain it was you though

 

Does it ring a bell?

KHorseman
Community Champion
Community Champion

@Sean I know I did it for year-and-month using FORMAT. As far as I can remember, the only example for year-and-week that you might be thinking of would be if you actually took the time to read through that crazy query code I use for my very slow standard date table. That has a custom column called WeekNumber, which is...

 

 

= Int64.From(
	Text.From(
		Date.Year(
			Date.EndOfWeek([Date])
		)
	)
	&
	Text.PadStart(
		Text.From(
			Date.WeekOfYear(
				Date.EndOfWeek([Date])
			)
		),
		2,
		"0"
	)
)

 

 

That formula returns an integer value without the hyphen in the middle. You should be able to modify it pretty easily to remove the integer conversion and add a hyphen character to the concatenation.

 

In DAX it's...

WeekNum = IF(
	WEEKNUM(DateTable[Date]) < 10, 
	YEAR(DateTable[Date]) & "-0" & WEEKNUM(DateTable[Date]),
	YEAR(DateTable[Date]) & "-" & WEEKNUM(DateTable[Date])
)

 





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

Proud to be a Super User!




Greg_Deckler
Super User
Super User

I would post this to the Issues forum.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.