May 27 2020 05:22 AM
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) |
May 27 2020 10:23 AM
SolutionNot 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.
May 27 2020 11:14 PM
@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]
@Sergei Baklan '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.
May 28 2020 12:35 AM
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.
May 28 2020 12:45 AM
@Sergei Baklan 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.
May 28 2020 12:50 AM
@Sergei Baklan 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.
May 28 2020 06:29 AM
There is a possibility of a third, even forth record (they really want to make things hard for me).
Will the code work anyway?
May 28 2020 07:56 AM
@Ocasio27 Tested it with your example data. It doesn't matter how many records you have per item. However, once a field has received a value other than (blank), the current query does not allow it to be reset to (blank), as it is interpreted as "no change" compared with the previous record. Perhaps possible, but then you'll have to tweak the query. And of course, you'll still have to adapt the query to your real data range, possibly with headers.
May 27 2020 10:23 AM
SolutionNot 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.