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
Anonymous
Not applicable

Format date to show YYYY_MM (direct query). I need leading zero in Month 2020_01 for sorting

Struggling with Diret query and making a custom grouping "date" field. 

I turned the date (non-contiguous) into a Year_Month field but I need the leading 0 in the month so it sorts properly. I can't figure out FORMAT() and suspect even in this case Direct Query says No to that.

 

My formula:

NPD_Date_YYYY_MM = YEAR(DIM_Product[NPDDate]) & "_" & MONTH(DIM_Product[NPDDate])

 

Please, thank you and I give KUDOS and mark as Solved!

1 ACCEPTED SOLUTION

Hi @Anonymous ,

You can try to create a custom column in power query like this:

=Table.AddColumn(#"Removed Other Columns", "Custom", each if 
Date.Month([FullDateAlternateKey]) < 10 then
Text.From(Date.Year([FullDateAlternateKey])) & "0" & 
Text.From(Date.Month([FullDateAlternateKey]))
else 
Text.From(Date.Year([FullDateAlternateKey])) & 
Text.From(Date.Month([FullDateAlternateKey])))

PQ.png

 

Best Regards,
Yingjie Li

If this post helps then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

4 REPLIES 4
mahoneypat
Employee
Employee

This expression gets your desired column with leading zeros.

 

YM Format = FORMAT('Date'[Date], "YYYY_MM")
 
You can also address this by making a sort column with a YearMonth index column.
 
Regards,
Pat
 




Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Anonymous
Not applicable

@mahoneypat ,

 

That will not work with Direct Query. Format() is not allowed. 

Hi @Anonymous ,

You can try to create a custom column in power query like this:

=Table.AddColumn(#"Removed Other Columns", "Custom", each if 
Date.Month([FullDateAlternateKey]) < 10 then
Text.From(Date.Year([FullDateAlternateKey])) & "0" & 
Text.From(Date.Month([FullDateAlternateKey]))
else 
Text.From(Date.Year([FullDateAlternateKey])) & 
Text.From(Date.Month([FullDateAlternateKey])))

PQ.png

 

Best Regards,
Yingjie Li

If this post helps then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

Solved it. This seems like it would be a drag on the performance because I had to make a YEAR and MONTH column for the date field, then use IF() to bring them together. 

 

A more eloquent solution would be appreciated. 

 

Cheers!

 

MY SOLUTION

NPD_DateYYYMM = IF (DIM_Product[NPD_Month] < 10 ,
[NPD_Year] & "0" &[NPD_Month] ,
[NPD_Year] & [NPD_Month] )

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.