Forum Discussion
Creating a powerquery to fix a table
- May 27, 2020
Not sure there is the source data - in another file or in Table/range of the current file. In general that's doesn't matter, just to build the sample
Let select columns from A to E and name selection as Range. Query it.
Add index column to the table.
Sort on first column to combine same ID:s together, right after that sort on Index in descending order to keep the latest records first.
Add one more index column to fix result in memory (or wrap previous step with Table.Buffer() ).
Select first column, Remove duplicates.
Remove bot columns with indexes.
Load result back into the sheet.
Ocasio27 Unless I have misunderstood your requirement isn't is so that you want to merge records for non-blank and updated fields?
For instance, for R033 the first record has: [blank, CONFIRMED, CONFIRMED, blank]
The second record for R033 has: [No, blank, blank, CONFIRMED]
Your desired output suggests that you want the result to be:
[No, CONFIRMED, CONFIRMED, CONFIRMED]
SergeiBaklan 's solution takes the second record as the result, if I'm not mistaken. Inspired by his approach of double indexing, I added a few steps that unpivots the table first, and re-pivots it a few steps later, resulting in the desired output as you described, though the sorting order changed a bit.
- SergeiBaklanMay 28, 2020Diamond Contributor
More exactly, not the second but the latest appeared in the list. Second index is only to fix in memory in UI without adding Table.Buffer manually.
- Riny_van_EekelenMay 28, 2020Platinum Contributor
SergeiBaklan True! Should have written "latest record". But in this example the second record = the latest record, as there were only two occasions of R033. I would have written "the third record" if there had been three.
Will have to look into Table.Buffer as I haven't come across that one yet in my discoveries of PQ.
- Ocasio27May 28, 2020Iron Contributor
There is a possibility of a third, even forth record (they really want to make things hard for me).
Will the code work anyway?
- Riny_van_EekelenMay 28, 2020Platinum Contributor
SergeiBaklan True! Should have written "latest record". But in this example the second record = the latest record, as there were only two occasions of R033.
Will have to look into the Table.Buffer as I haven't come across that one yet in my discoveries of PQ.