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
ddrury426
New Member

Need help with formula - column with multiple date formats

I am in the process of cleaning a dataset and found that I can't convert one of my columns to a date since it has multiple date formats in the results (sample column below).  I'm trying to create a custom column to convert all to a short date, but I'm having trouble.  What would be the best way to accomplish this?

 

DATE

44266.334028

3/11/2021 8:01

null

44308.750694

43858.473611

1/28/2020 11:22

 

1 ACCEPTED SOLUTION

it's not a formula. It's a step in your query. Here is working example of the code with a table of one column DATE as a Source. I also added final step to make type date. Try to add 2 final steps into your query and replace Source reference with your previous step name. 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("JYvBDcAgDMRWqfKuIJeEkLIKYgPUX/dvCz/LsnsnM3FPqsYSNM5OmoEsLDiiMZa6nzkXmClHqoX9si00SiSr6tjpt8Z/8wE0ERrjBQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [DATE = _t]),
    transformation = Table.TransformColumns(
        Source,
        {"DATE",
        each if _ = null then _ else Date.From(try Number.Round(Number.From(_), 0) otherwise DateTime.From(_))}
    ),
    #"Changed Type" = Table.TransformColumnTypes(transformation,{{"DATE", type date}})
in
    #"Changed Type"

 

View solution in original post

5 REPLIES 5
ronrsnfld
Super User
Super User

Try:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("JYvBDcAgDMRWqfKuIJeEkLIKYgPUX/dvCz/LsnsnM3FPqsYSNM5OmoEsLDiiMZa6nzkXmClHqoX9si00SiSr6tjpt8Z/8wE0ERrjBQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [DATE = _t]),
    #"to Date" = Table.TransformColumns(Source, {"DATE", each try Date.From(Number.From(_)) otherwise DateTime.Date(DateTime.From(_,"en-US")), type nullable date})
in
    #"to Date"

 

AlienSx
Super User
Super User

Hi, @ddrury426 

    Table.TransformColumns(
        Source,
        {"DATE",
        each if _ = null then _ else Date.From(try Number.Round(Number.From(_), 0) otherwise DateTime.From(_))}
    )
ddrury426
New Member

That doesn't seem to help.  The column ends up coming in as a text field that I can't change the data type on.  I'd like to keep the null values as null, and when I enter the formula, I end up with an error: 

 

Expression.Error: We cannot convert a value of type Function to type Type.
Details:
Value=[Function]
Type=[Type]

it's not a formula. It's a step in your query. Here is working example of the code with a table of one column DATE as a Source. I also added final step to make type date. Try to add 2 final steps into your query and replace Source reference with your previous step name. 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("JYvBDcAgDMRWqfKuIJeEkLIKYgPUX/dvCz/LsnsnM3FPqsYSNM5OmoEsLDiiMZa6nzkXmClHqoX9si00SiSr6tjpt8Z/8wE0ERrjBQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [DATE = _t]),
    transformation = Table.TransformColumns(
        Source,
        {"DATE",
        each if _ = null then _ else Date.From(try Number.Round(Number.From(_), 0) otherwise DateTime.From(_))}
    ),
    #"Changed Type" = Table.TransformColumnTypes(transformation,{{"DATE", type date}})
in
    #"Changed Type"

 

It works!  Thank you!

ddrury426_0-1679921680056.png

 

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.

Top Solution Authors
Top Kudoed Authors