Forum Discussion
Latest date conditional column
- Anonymous5 years ago
Strictly speaking, as the DefineDate, LatestDate and DefineInterval are not supposed to change for each record, I would move it out of the cycle to save CPU ticks as in the current code they are getting redefined for each row, which may be an issue for extremely large datasets:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ZcvJCQAgDAXRXv5ZMItLMcH+21AiSCTXx4wZZmWuQkIoUKxiGEGaSw8iLi1dmi5JDV95hR6gDyjC2g==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Report Refresh Date" = _t, cnt = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Report Refresh Date", type date}, {"cnt", Int64.Type}}), DefineDateInterval = {0, -2, -4, -7}, LatestDate = List.Max(Table.SelectRows(#"Changed Type", each [cnt] = 4)[Report Refresh Date]), DefineDate = List.Transform(DefineDateInterval, each Date.AddDays(LatestDate, _)), #"Added Custom" = Table.AddColumn ( #"Changed Type", "Custom", each if List.Contains(DefineDate, [Report Refresh Date]) and [cnt] = 4 then 1 else 0 ) in #"Added Custom"This also makes the code a bit lighter.
Kind regards,
JB
thank, you, Jimmy801 , that worked. Would you happen to know how I can enhance the logic in a way
so Custom brings result 1 not only to this one latest date where cnt=4 but also -7, -14, -21 days from it? the outcome should be
Hello Anonymous
check out this solution
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WstA3NNQ3MjAyUNJRMlGK1YlWMkcSMQaLmGGoMUUSMQKLmGDoMsbQZYSqJhYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Report Refresh Date" = _t, cnt = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Report Refresh Date", type date}, {"cnt", Int64.Type}}),
#"Added Custom" = Table.AddColumn
(
#"Changed Type",
"Custom",
each
let
DefineDateInterval = {0, -2, -4},
LatestDate = List.Max(#"Changed Type"[Report Refresh Date]),
DefineDate = List.Transform(DefineDateInterval, each Date.AddDays(LatestDate, _)),
Check =if List.Contains(DefineDate, [Report Refresh Date]) and [cnt] = 4 then 1 else 0
in
Check
)
in
#"Added Custom"
Use the variable DefineDateInterval to define de intervals of days to the latest date to check
Copy paste this code to the advanced editor in a new blank query to see how the solution works.
If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too
Have fun
Jimmy
- Anonymous5 years agoNot applicable
thanks a lot, Jimmy801 , this seems to be working with the example table but I have another table with bit different dates and that does not seem to be working there...
- Jimmy8015 years ago
Community Champion
Hello Anonymous
did you change the variable
DefineDateIntervalaccordingly? I inputed there 0, -2, and -4 to work with the example. You have to adapt that to your needs
Jimmy
- Anonymous5 years agoNot applicable
That is right, Jimmy801 , this part of mine is:
Table details exported to excel: https://drive.google.com/file/d/1D7_K9qVxRgMMWiFbq-n2pol1nJ1koXKe/view?usp=sharing