Forum Discussion
Creating a powerquery to fix a table
I am trying to make a query step to change the left example to the right example. The problem is that the client keeps sending them table with new rows that have updated results rather than just updating the previous row
Notice records R779, R365 and R033
Left is actual table, right is how I want it after a query step
R654 | (blank) | PENDING | PENDING | (blank) | R654 | (blank) | PENDING | PENDING | (blank) | |
R779 | (blank) | PENDING | PENDING | (blank) | R779 | No | PENDING | PENDING | (blank) | |
R779 | No | (blank) | (blank) | (blank) | R303 | (blank) | PENDING | PENDING | (blank) | |
R303 | (blank) | PENDING | PENDING | (blank) | K628 | (blank) | PENDING | PENDING | (blank) | |
K628 | (blank) | PENDING | PENDING | (blank) | R635 | Yes | PENDING | PENDING | (blank) | |
R635 | (blank) | PENDING | PENDING | (blank) | K640 | (blank) | PENDING | PENDING | (blank) | |
R635 | Yes | (blank) | (blank) | (blank) | R033 | No | CONFIRMED | CONFIRMED | CONFIRMED | |
K640 | (blank) | PENDING | PENDING | (blank) | R177 | (blank) | PENDING | PENDING | (blank) | |
R033 | (blank) | CONFIRMED | CONFIRMED | |||||||
R033 | No | (blank) | (blank) | CONFIRMED | ||||||
R177 | (blank) | PENDING | PENDING | (blank) |
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.
7 Replies
- Riny_van_EekelenPlatinum Contributor
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.
- SergeiBaklanDiamond 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_EekelenPlatinum 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.
- SergeiBaklanDiamond Contributor
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.