Forum Discussion
Anonymous
7 years agoNot applicable
subtracting time from date/time power query
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 canno...
- 7 years ago
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
- 7 years ago
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"
Andras1
1 year agoFrequent Visitor
I usually create a custom column with the following: [Your date column] +/- #duration(days, hours, minutes, seconds).
To subtract 1 day, 2 hours, 3 minutes, and 4 seconds from your date column use the following code in a custom column: [Your date column] - #duration(1,2,3,4)