Starting December 3, join live sessions with database experts and the Microsoft product team to learn just how easy it is to get started
Learn moreGet certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now
Hello
I have a table with date/time column and time column. I want to subtract the time column from the date/time column in Power Query, but I receive this message
Expression.Error: We cannot apply operator - to types DateTime and Time.
Details:
Operator=-
Left=07/04/2019 14:00:00
Right=00:00:00
i used a simple formula when using the Adding aCustom Column
=[date/time] - [time]
I know it's possible with DAX, but i need to do it within Power Query
Thanking you in advance
Solved! Go to Solution.
Hi @Anonymous ,
If you want to achieve your desired output with Power Query, you could refer to my code below.
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwtNQ11DVVMDCyMjS1MjBQ0lEyMATSIGasDlyBIYoCA5iCWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Date/Time" = _t, Time = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date/Time", type datetime}, {"Time", type time}}), #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each DateTime.Time([#"Date/Time"])-[Time]), #"Split Column by Position" = Table.SplitColumn(Table.TransformColumnTypes(#"Added Custom", {{"Custom", type text}}, "en-US"), "Custom", Splitter.SplitTextByPositions({0, 1}, false), {"Custom.1", "Custom.2"}), #"Removed Columns" = Table.RemoveColumns(#"Split Column by Position",{"Custom.1"}), #"Split Column by Delimiter" = Table.SplitColumn(Table.TransformColumnTypes(#"Removed Columns", {{"Date/Time", type text}}, "en-US"), "Date/Time", Splitter.SplitTextByDelimiter("#(tab)", QuoteStyle.Csv), {"Date/Time.1", "Date/Time.2"}), #"Duplicated Column" = Table.DuplicateColumn(#"Split Column by Delimiter", "Date/Time.1", "Date/Time.1 - Copy"), #"Split Column by Delimiter1" = Table.SplitColumn(#"Duplicated Column", "Date/Time.1 - Copy", Splitter.SplitTextByDelimiter(" ", QuoteStyle.Csv), {"Date/Time.1 - Copy.1", "Date/Time.1 - Copy.2", "Date/Time.1 - Copy.3"}), #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter1",{{"Date/Time.1", type datetime}, {"Date/Time.2", type text}, {"Custom.2", type time}, {"Date/Time.1 - Copy.1", type date}, {"Date/Time.1 - Copy.2", type time}, {"Date/Time.1 - Copy.3", type text}}), #"Removed Columns1" = Table.RemoveColumns(#"Changed Type1",{"Date/Time.2", "Date/Time.1 - Copy.2", "Date/Time.1 - Copy.3"}), #"Changed Type2" = Table.TransformColumnTypes(#"Removed Columns1",{{"Date/Time.1 - Copy.1", type text}, {"Custom.2", type text}}), #"Added Custom1" = Table.AddColumn(#"Changed Type2", "Custom", each [#"Date/Time.1 - Copy.1"]&" "&[Custom.2]), #"Changed Type3" = Table.TransformColumnTypes(#"Added Custom1",{{"Custom", type datetime}}) in #"Changed Type3"
Here is the output.
In addition, @ImkeF may have other simpler solution for your requirement.
Best Regards,
Cherry
The key lies in not transforming the Time-column to Time, but to duration instead in the first "Changed Type"-step.
Then you can simply subtract it like so:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwtNQ11DVVMDCyMjS1MjBQ0lEyMATSIGasDlyBIYoCY5iCWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Date/Time" = _t, Time = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date/Time", type datetime}, {"Time", type duration}}), #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each [#"Date/Time"]-[Time]) in #"Added Custom"
Imke Feldmann (The BIccountant)
If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!
How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries
Hi,
You could use the code below in a custom column and it should work
DateTime.Time([Table])
Just switch the table to your own
Thanks for your reply.
I think there was a missunderstanding.
I have two columns one with Date/Time and another with Time only. Time column is populated by 00:00:00 or 01:00:00 and I want to subtract these from the Date/Time column
Date/Time Time UTC Time
01/05/2019 02:15:00 01:00:00 01/05/2019 01:15:00
01/01/2019 02:15:00 00:00:00 01/01/2019 02:15:00
Kind regards
Hi @Anonymous ,
If you want to achieve your desired output with Power Query, you could refer to my code below.
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwtNQ11DVVMDCyMjS1MjBQ0lEyMATSIGasDlyBIYoCA5iCWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Date/Time" = _t, Time = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date/Time", type datetime}, {"Time", type time}}), #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each DateTime.Time([#"Date/Time"])-[Time]), #"Split Column by Position" = Table.SplitColumn(Table.TransformColumnTypes(#"Added Custom", {{"Custom", type text}}, "en-US"), "Custom", Splitter.SplitTextByPositions({0, 1}, false), {"Custom.1", "Custom.2"}), #"Removed Columns" = Table.RemoveColumns(#"Split Column by Position",{"Custom.1"}), #"Split Column by Delimiter" = Table.SplitColumn(Table.TransformColumnTypes(#"Removed Columns", {{"Date/Time", type text}}, "en-US"), "Date/Time", Splitter.SplitTextByDelimiter("#(tab)", QuoteStyle.Csv), {"Date/Time.1", "Date/Time.2"}), #"Duplicated Column" = Table.DuplicateColumn(#"Split Column by Delimiter", "Date/Time.1", "Date/Time.1 - Copy"), #"Split Column by Delimiter1" = Table.SplitColumn(#"Duplicated Column", "Date/Time.1 - Copy", Splitter.SplitTextByDelimiter(" ", QuoteStyle.Csv), {"Date/Time.1 - Copy.1", "Date/Time.1 - Copy.2", "Date/Time.1 - Copy.3"}), #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter1",{{"Date/Time.1", type datetime}, {"Date/Time.2", type text}, {"Custom.2", type time}, {"Date/Time.1 - Copy.1", type date}, {"Date/Time.1 - Copy.2", type time}, {"Date/Time.1 - Copy.3", type text}}), #"Removed Columns1" = Table.RemoveColumns(#"Changed Type1",{"Date/Time.2", "Date/Time.1 - Copy.2", "Date/Time.1 - Copy.3"}), #"Changed Type2" = Table.TransformColumnTypes(#"Removed Columns1",{{"Date/Time.1 - Copy.1", type text}, {"Custom.2", type text}}), #"Added Custom1" = Table.AddColumn(#"Changed Type2", "Custom", each [#"Date/Time.1 - Copy.1"]&" "&[Custom.2]), #"Changed Type3" = Table.TransformColumnTypes(#"Added Custom1",{{"Custom", type datetime}}) in #"Changed Type3"
Here is the output.
In addition, @ImkeF may have other simpler solution for your requirement.
Best Regards,
Cherry
The key lies in not transforming the Time-column to Time, but to duration instead in the first "Changed Type"-step.
Then you can simply subtract it like so:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwtNQ11DVVMDCyMjS1MjBQ0lEyMATSIGasDlyBIYoCY5iCWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Date/Time" = _t, Time = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date/Time", type datetime}, {"Time", type duration}}), #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each [#"Date/Time"]-[Time]) in #"Added Custom"
Imke Feldmann (The BIccountant)
If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!
How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries
Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.
User | Count |
---|---|
87 | |
87 | |
84 | |
66 | |
49 |
User | Count |
---|---|
130 | |
110 | |
93 | |
70 | |
67 |