Google Sheets – How to Use Query to Find Row of Same Name and Copy Value

formulasgoogle sheetsgoogle-sheets-arraysgoogle-sheets-querysorting

I am trying to use a query to copy values from one column to another.
Both source and destination columns have a name on their left columns, and the name must match. The source names could be duplicated, and in that case, I would like to have the entry on the bottom. There could be empty rows as well.

Please see the picture.

query

Best Answer

Please use the following
(Adjust ranges to your needs)

=SORTN(QUERY({INDEX(ROW(D2:D)),D2:E}, 
                  "select Col2, Col3 
                   where Col2 is not null 
                   order by Col2, Col1 desc"),
     9^9,2,1,1)

enter image description here

How the formula works

We add a virtual column based on each row to our query and order the query.
We then -in our SORTN function- use a very large number like 9^9 so as to include any future references of column D

Functions used: