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
Spigaw
Helper II
Helper II

Format date directly in the function

Hello everyone,

 

I have a column with periods in the format yyyymm, for example 20209, 202010. I have to output a date from it in the format dd/mm/yyyy, for example 01/09/2020 and 01/10/2020.

 

The function I am currently using is as follows:

 

= Date.From(#date(Number.FromText(Text.Start(Number.ToText([Test]),4)),Number.FromText(Text.End(Number.ToText([Test]),2)),1))

 

However, the output is in numeric format when I load the data in Excel, for example 44075 and 44105.

 

Is there a way to get the right format in the function, without creating a new step in the query?

 

Thanks in advance for any help.

2 ACCEPTED SOLUTIONS
CNENFRNL
Community Champion
Community Champion

Screenshot 2021-09-29 122930.png

 

= Table.AddColumn(#"Changed Type", "Start of Month", each Date.From(Text.From([yyyymm])&"01"), type date)

Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

View solution in original post

v-eqin-msft
Community Support
Community Support

Hi @Spigaw ,

 

Based on your M synatx, It seemed that the data type has changed from text to Number firstly by default ,which is not necessary.

Eyelyn9_2-1633059395301.png

 

So please delete the Changed type step in order to simplify your code and then use the following synatx in the Advanced Editor:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMrBUitWBsAwNlGJjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Test = _t]),
    #"Added Custom" =Table.TransformColumnTypes( Table.AddColumn(Source, "Custom", each Date.From(#date(Number.FromText(Text.Start([Test],4)),Number.FromText(Text.End([Test],2)),1))   ),{{"Custom", type date}})
   in
    #"Added Custom"

Eyelyn9_1-1633059121772.png

 

Eyelyn9_0-1633059105634.png

 

Best Regards,
Eyelyn Qin
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

5 REPLIES 5
v-eqin-msft
Community Support
Community Support

Hi @Spigaw ,

 

Based on your M synatx, It seemed that the data type has changed from text to Number firstly by default ,which is not necessary.

Eyelyn9_2-1633059395301.png

 

So please delete the Changed type step in order to simplify your code and then use the following synatx in the Advanced Editor:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMrBUitWBsAwNlGJjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Test = _t]),
    #"Added Custom" =Table.TransformColumnTypes( Table.AddColumn(Source, "Custom", each Date.From(#date(Number.FromText(Text.Start([Test],4)),Number.FromText(Text.End([Test],2)),1))   ),{{"Custom", type date}})
   in
    #"Added Custom"

Eyelyn9_1-1633059121772.png

 

Eyelyn9_0-1633059105634.png

 

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

CNENFRNL
Community Champion
Community Champion

Simple enough

= Table.AddColumn(#"Changed Type", "Start of Month", each Date.From(Text.From([yyyymm])&"01"))

Screenshot 2021-09-29 102307.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

Thank you for the simpler function. However, your column still shows ABC123, and not a calendar, so the data type is still not a date. Is there a workaround for this?

Screenshot 2021-09-29 122930.png

 

= Table.AddColumn(#"Changed Type", "Start of Month", each Date.From(Text.From([yyyymm])&"01"), type date)
CNENFRNL
Community Champion
Community Champion

Screenshot 2021-09-29 122930.png

 

= Table.AddColumn(#"Changed Type", "Start of Month", each Date.From(Text.From([yyyymm])&"01"), type date)

Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

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.

Top Solution Authors
Top Kudoed Authors