Forum Discussion
KHSK
Advocate I
10 months agoNeed Power Query code to load unique IDs in the snapshot history Table
Extremely sorry for the confusion, I just updated the TO-BE table. Logic: If approval dates are same for VP& SVP for an ID, I need any one of them to be displayed uniquely. If approval dates ...
- 9 months ago
Hello,
The code below might get the right results. See annotations in the codelet Source = YOURDATA, // group the data by ID and snapshotdate combination groupby_id_snapshotdate = Table.Group( Source, {"ID", "SnapshotDt"}, {{"Table", each _, type table [ID=nullable number, ApprovalDate=nullable date, ApprovalRole=nullable text, Status=nullable text, SnapshotDt=nullable date]}}), // determine which row from the nested table should be returned add_SelectedRow = Table.AddColumn( groupby_id_snapshotdate, "SelectedRow", // if there is only one row, return the row each if Table.RowCount([Table]) = 1 then [Table]{0} // if the list of approvaldates is distinct that means there is more than one date in the list. else if List.IsDistinct([Table][ApprovalDate]) // return te row with the max date then Table.SelectRows([Table], (row) => row[ApprovalDate] = List.Max([Table][ApprovalDate])){0} // get // the list is not distinct = all dates are the same -> return first row else Table.First([Table])), // get the data from the records expand_records = Table.ExpandRecordColumn( add_SelectedRow, "SelectedRow", {"ApprovalDate", "ApprovalRole", "Status"}, {"ApprovalDate", "ApprovalRole", "Status"}), // remove the table column removeColumns = Table.RemoveColumns( expand_records,{ "Table"}) in removeColumns
vojtechsima
Super User
10 months agohey, KHSK ,
please nex time share your logic, I did try to figure it out here:
try this:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTIw1jMw1zMyMDIFcsICgIRjQUFRfllqCkjSQg8oD5aM1cGiPpiABiOQkAlciKAFMPUmxFpAjA8MDUj0AbIGYnyATT1eHyBrMAYJmRJvgQm6erwWxAIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Id = _t, ApprovalDate = _t, ApprovalRole = _t, Status = _t, SnapshotDt = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Id", Int64.Type}, {"ApprovalDate", type date}, {"ApprovalRole", type text}, {"Status", type text}, {"SnapshotDt", type date}}),
sortRows = Table.Sort(#"Changed Type",{{"SnapshotDt", Order.Descending}, {"Id", Order.Ascending}, {"ApprovalDate", Order.Descending}}),
group = Table.Group(sortRows, {"Id", "SnapshotDt"}, {{"rows", (tbl)=> let
latestApproval = List.Max(tbl[ApprovalDate]),
rows = Table.SelectRows( tbl, each [ApprovalDate] = latestApproval),
check = if Table.RowCount( rows ) > 1 then Table.SelectRows(rows, each [ApprovalRole] = "VP" ) else rows
in check
}}),
expand = Table.ExpandTableColumn(group, "rows", {"ApprovalDate", "ApprovalRole", "Status"}, {"ApprovalDate", "ApprovalRole", "Status"})
in
expand
Replace Source and Changed Types to yours steps. And adjust the sort at the end, but the logic is there.
KHSK
Advocate I
10 months agoHey vojtechsima, Please find the updated logic & the TO-BE table.