Forum Discussion
Timestamp when true
- 3 years ago
Hi philerob2014 ,
Maybe like this?
Before:
After:
I applied the following logic:
if [Substate] = "Pending return" and [timestamp] <> null then [timestamp] else if [Substate] = "Pending return" and [timestamp] = null then DateTime.LocalNow() else if [Substate] <> "Pending return" and [timestamp] <> null then null else null
Here the full code:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCkjNS8nMS1coSi0pLcpT0lEyMjAy0jU00jUwUzA0sjIwACKlWJ1opbTEnOJUNHlDLPJgDoapuERjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Substate = _t, timestamp = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Substate", type text}, {"timestamp", type datetime}}), #"Replace Values" = Table.ReplaceValue(#"Changed Type",each [timestamp],each if [Substate] = "Pending return" and [timestamp] <> null then [timestamp] else if [Substate] = "Pending return" and [timestamp] = null then DateTime.LocalNow() else if [Substate] <> "Pending return" and [timestamp] <> null then null else null, Replacer.ReplaceValue,{"timestamp"}) in #"Replace Values"/Tom
https://www.tackytech.blog/
https://www.instagram.com/tackytechtom/
Hi philerob2014 ,
Ok, I think I now understand a bit better what you are after. For your scenario I suggest you use Power Query:
Before:
After:
I applied the following logic:
if [Column] = true and [timestamp] <> null then [timestamp] else if [Column] = true and [timestamp] = null then DateTime.LocalNow() else if [Column] = false and [timestamp] <> null then null else null
Here the code for the advanced editor:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WKikqTVXSUTIyMDLSNTTSNTBTMDSyMjAAIqVYnWiltMScYnR5QyzyYA7ULFR2LAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column = _t, timestamp = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column", type logical}, {"timestamp", type datetime}}),
#"Replace Values" = Table.ReplaceValue(#"Changed Type",each [timestamp],each if [Column] = true and [timestamp] <> null then [timestamp] else if [Column] = true and [timestamp] = null then DateTime.LocalNow() else if [Column] = false and [timestamp] <> null then null else null, Replacer.ReplaceValue,{"timestamp"})
in
#"Replace Values"
In case you haven't worked with Power Query before, I'd suggest to start with a blank query and look into the steps above. I am sure you will be able to figure it out how to align your code 🙂 Otherwise, you could also just create a new column with the if logic above. If you are happy with it and you want to overwrite your timestamp column, I'd suggest to check out this blog post, where you can learn how to substitute values (instead of creating a new column)
Ahhhh, you are brilliant! But what would be my referenced field for "[timestamp]". That was created outside of power query editor so I didn't think the second step could reference that. tackytechtom
- tackytechtom3 years agoMost Valuable Professional
Hi philerob2014 ,
I interpreted "Problem 2: If the row already has a timestamp from the previous refresh and is still true, leave it. If its no longer true, make it blank again." that there is already a timestamp column in place. I might have misunderstood your query.
You miight wantt to provide some example data and expected result?
/Tom
https://www.tackytech.blog/
https://www.instagram.com/tackytechtom/- philerob20143 years agoFrequent Visitor
Sure, let me clarify exactly what I need so its clear. Your solutions are beautifully written, I just dont understand how to connect them.
If [Substate] = "Pending return"
get original time and date that this condition was first true on report refresh. Purge date when condition is not true. If condition is true over multiple refreshes, keep the original date.
- tackytechtom3 years agoMost Valuable Professional
Hi philerob2014 ,
Maybe like this?
Before:
After:
I applied the following logic:
if [Substate] = "Pending return" and [timestamp] <> null then [timestamp] else if [Substate] = "Pending return" and [timestamp] = null then DateTime.LocalNow() else if [Substate] <> "Pending return" and [timestamp] <> null then null else null
Here the full code:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCkjNS8nMS1coSi0pLcpT0lEyMjAy0jU00jUwUzA0sjIwACKlWJ1opbTEnOJUNHlDLPJgDoapuERjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Substate = _t, timestamp = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Substate", type text}, {"timestamp", type datetime}}), #"Replace Values" = Table.ReplaceValue(#"Changed Type",each [timestamp],each if [Substate] = "Pending return" and [timestamp] <> null then [timestamp] else if [Substate] = "Pending return" and [timestamp] = null then DateTime.LocalNow() else if [Substate] <> "Pending return" and [timestamp] <> null then null else null, Replacer.ReplaceValue,{"timestamp"}) in #"Replace Values"/Tom
https://www.tackytech.blog/
https://www.instagram.com/tackytechtom/