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
goncz
Frequent Visitor

Add custom column with date based on another column

I want to add a custom column in query editor that contains dates on the format "14/10/2020".

The specific date should depend on another column in the dataset with dates on the format "14/10/2020 00:00:00". So the new column should essentially remove the "00:00:00".

 

I've tried the following code, which always returns null:

 

= if[Endtime] = "01/01/2020 00:00:00" then [Date] = "01/01/20" else null

 

How should I go about this? 

2 ACCEPTED SOLUTIONS
AlB
Super User
Super User

Hi @goncz 

Make sure the EndTime column is of type datetime. Then simply crete a custom column converting to date. Place the following M code in a blank query to see the steps:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDUByIjAyMDBQMDKzBSio0FAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [EndTime = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"EndTime", type datetime}}),

    #"Added Custom" = Table.AddColumn(#"Changed Type", "Date", each Date.From([EndTime]), type date)
in
    #"Added Custom"

 

 

Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

 

SU18_powerbi_badge

View solution in original post

v-yiruan-msft
Community Support
Community Support

Hi @goncz ,

You can copy Endtime column and set the data ype of copied column as Date:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtA3BCITBQMrAxBSio0FAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Endtime = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Endtime", type datetime}}),
    #"Duplicated Column" = Table.DuplicateColumn(#"Changed Type", "Endtime", "Endtime - Copy"),
    #"Changed Type1" = Table.TransformColumnTypes(#"Duplicated Column",{{"Endtime - Copy", type date}})
in
    #"Changed Type1"

Best Regards

Community Support Team _ Rena
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-yiruan-msft
Community Support
Community Support

Hi @goncz ,

You can copy Endtime column and set the data ype of copied column as Date:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtA3BCITBQMrAxBSio0FAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Endtime = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Endtime", type datetime}}),
    #"Duplicated Column" = Table.DuplicateColumn(#"Changed Type", "Endtime", "Endtime - Copy"),
    #"Changed Type1" = Table.TransformColumnTypes(#"Duplicated Column",{{"Endtime - Copy", type date}})
in
    #"Changed Type1"

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
AlB
Super User
Super User

Hi @goncz 

Make sure the EndTime column is of type datetime. Then simply crete a custom column converting to date. Place the following M code in a blank query to see the steps:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDUByIjAyMDBQMDKzBSio0FAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [EndTime = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"EndTime", type datetime}}),

    #"Added Custom" = Table.AddColumn(#"Changed Type", "Date", each Date.From([EndTime]), type date)
in
    #"Added Custom"

 

 

Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

 

SU18_powerbi_badge

amitchandak
Super User
Super User

@goncz , Try like

if[Endtime] = DateTime.FromText("2020-01-01 00:00:00") then Date.FromText("2020-01-01") else null

Gabriel_Walkman
Continued Contributor
Continued Contributor

if [Endtime] <> null then [Date] ? Text.Left(Text.From([Endtime]),10))

I haven't checked in BI so I can have bugs, but I hope it makes you move forward. I suspect your "Endtime" isn't in text format, so you can't compare it to text. Check out the #date() and #datetime() if you need a specific date comparison.

Thanks @Gabriel_Walkman . The Endtime is in Date/Time format. Your code suggestion does not work, I get "Token Else expected" in:

 

if [Endtime] <> null then [Date] ? Text.Left(Text.From([Endtime]),10))

 

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.