Forum Discussion
Bi2thelly
Helper II
4 years agoPower Query Inserting rows when condition met
I need to insert rows (preferably above but not necessary) when a condition is met on another row using PQ. I was lookining into Table.InsertRows but I'm having trouble using it. Condition I was tryi...
jbwtp
Memorable Member
4 years agoHi Bi2thelly,
Slightly alternative solution to already suggested. This does not care about inserting a row in a particular place, which is in most cases a mere reading convenience and gives no benefits to data processing in the report for a lot of complexity in the code.
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fY6xDsIwDET/JXMl+0xI0pmZHVRlQKhDFxYQ348DShq3ElIGx767d9PkLsxwgxMCkzBGnW/31/KeddB3YAL0IFE/R1+kp7NakHrhdX52WkSXh2+ybJKXR7OoEnVtIdE3yMbzw6zOjhLIlwD+Wz9xTRbs67cIUz6VHWwPCRTr2hLGYAi77qsz5w8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Serial ID" = _t, #"Freeze Date" = _t, Status = _t, #"Inactive Date" = _t, #"Active Date" = _t, Index = _t, #"Testing2.Serial ID" = _t, Testing2.Status = _t, Keep = _t, #"Activity Date" = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Serial ID", type text}, {"Freeze Date", type date}, {"Status", type text}, {"Inactive Date", type date}, {"Active Date", type date}, {"Index", Int64.Type}, {"Testing2.Serial ID", type text}, {"Testing2.Status", type text}, {"Keep", type text}, {"Activity Date", type date}}),
#"Filtered Rows" = Table.SelectRows(#"Changed Type", each ([Status] = "inactive" and [Serial ID]<>[Testing2.Serial ID])),
#"Removed Other Columns" = Table.SelectColumns(#"Filtered Rows",{"Serial ID", "Status", "Active Date", "Activity Date"}),
ResetActiveDate = Table.ReplaceValue(#"Removed Other Columns",null,null,(x, y, z) as date=> Date.From(DateTime.LocalNow()),{"Active Date", "Activity Date"}),
ResetStatus = Table.ReplaceValue(ResetActiveDate,null,null,(x, y, z) as text => "active",{"Status"}),
Output = Table.Combine({ResetStatus, #"Changed Type"})
in
Output
It is also unclear how do you define the Active and Activity date for the new rows, so you may need to change how they are set in the code.
Kind regards,
John
- Bi2thelly4 years ago
Helper II
You are right. I should not have included the Activity Date in the screenshot as it is produced afterward.