Return non-adjacent columns with XLOOKUP
=XLOOKUP(J1,A2:A8,CHOOSECOLS(A2:G8,2,6,7))Excel · result on the sample sheet: 1 row (J2:L2)
Describe what you need and get a working HSTACK formula for Excel and Google Sheets, tested on real data before you copy it. HSTACK places ranges side by side to build one table, so you can pick columns that aren't next to each other, put them in a new order, or add a calculated column, all in one formula that updates with the source.
Each formula below was calculated on its own sample sheet. Open one to see the sheet, why it works, and to try it yourself.
=XLOOKUP(J1,A2:A8,CHOOSECOLS(A2:G8,2,6,7))Excel · result on the sample sheet: 1 row (J2:L2)
=HSTACK(UNIQUE(A2:A13),MAXIFS(C2:C13,A2:A13,UNIQUE(A2:A13)))Excel · result on the sample sheet: 3 rows (E2:F4)
=SORT(HSTACK(RANK.EQ(B2:B9,B2:B9),A2:B9),1)Excel · result on the sample sheet: 8 rows (D2:F9)
=HSTACK(A2:B7,XLOOKUP(A2:A7,D2:D6,E2:E6,"Missing"))Excel · result on the sample sheet: 6 rows (G2:I7)
=HSTACK(UNIQUE(A2:A12),COUNTIF(A2:A12,UNIQUE(A2:A12)))Excel · result on the sample sheet: 4 rows (D2:E5)
=HSTACK(UNIQUE(B2:B9),SUMIF(B2:B9,UNIQUE(B2:B9),C2:C9))Excel · result on the sample sheet: 4 rows (E2:F5)
=HSTACK(UNIQUE(A2:A13),SUMIFS(C2:C13,A2:A13,UNIQUE(A2:A13)))Excel · result on the sample sheet: 4 rows (E2:F5)
=LET(old,XLOOKUP(D2:D8,A2:A7,B2:B7,"New"),FILTER(HSTACK(D2:D8,old,E2:E8),old<>E2:E8))Excel · result on the sample sheet: 4 rows (G2:I5)
You describe the task in plain English, and it writes the formula, using HSTACK where that's the right tool, for your table or a sample one. The formula is run in our engine in Excel and Google Sheets mode before you see it, so you get its result, not just a guess.
HSTACK places ranges side by side to build one table, so you can pick columns that aren't next to each other, put them in a new order, or add a calculated column, all in one formula that updates with the source.
Yes. Each example below opens a page where you try the formula in a live grid and it's checked as you go, and the HSTACK function page has a practice exercise.