Google-sheets – How to get a cell’s parent ID in Google Sheets

formulasgoogle sheetsgoogle-sheets-arrayformulavlookup

I'm trying to bulk add a bunch of building product categories to my site's MySQL database. There are about 650 entries and about 4 levels in the hierarchy. For it to work, each entry needs an ID, and then to fall within the hierarchy, have the ID of its parent in an additional column.

Please have a look at my Google sheet as attached. There are two sheets: the first one is what I'm trying to achieve, and the second one is just a visual tabbed representation of the hierarchy. In the first sheet, the hierarchy column indicates the respective hierarchies desired. I think it would be self-explanatory if you were to open it.

Google Sheet

Best Answer

=ARRAYFORMULA(IFERROR(VLOOKUP(LEFT(B2:B, LEN(B2:B)-2), {B2:B, A2:A}, 2, 0)))

0