Forum Discussion
Harvey85
2 years agoHelper I
Previous value in Power Query
Hello, I have the following table in Power Query: Key Camp_ID Lot Campaign Date Crop AA-14C-FF-8586-40 AA-14C-FF SFBA-STDOM-1-0 23 11/17/2023 Soy...
- 2 years ago
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("dc89C4MwEAbg/+LskVxMrKsmpNAqtU2hgjg4CB1KBenSf9/4FRXqciTvkSd3ZenFMSCXoDVEIgqBU89fMns2OonB3NUlA4S+yQJbEAkeCKPDxbRfr/JLL5GAQsOpmChhey7bUmyhaEgYn6mk7l7NqGX2JTtDKictsm2X7WrICNJZk233Hqzc2CkkmHS1pMu2lhgtbktAgl7iqxVvVztAAfm0ouh/cdkuxAmfocezqT8jpQCpgqNazeSyv5QYqGgzU/UD", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Key = _t, Camp_ID = _t, Lot = _t, Campaign = _t, Date = _t, Crop = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Key", type text}, {"Camp_ID", type text}, {"Lot", type text}, {"Campaign", Int64.Type}, {"Date", type date}, {"Crop", type text}}), #"Added Custom" = Table.AddColumn(#"Changed Type", "Previous", (k)=> try Table.Last(Table.SelectRows(#"Changed Type",each [Lot]=k[Lot] and [Date]<k[Date]))[Crop] otherwise null) in #"Added Custom"How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done". Once you examined the code, replace the Source step with your own source.
NOTE: The code above assumes your data is sorted. If that assumption is wrong you need to bring your own index column.