Structured Table References: Multiple non-contiguous columns?

Copper Contributor

I'm using XLOOKUP (which is AWESOME, btw) to return multiple columns from a table. (See file for example).

Annotation 2020-02-10 084655.jpg

 

 

What I'd like to do is return Sales Amount and Commission Amount, which are non-contiguous columns in the Table.

 

This seems like something that *has* to be possible, but I can't figure it out. 

 

Any tips? Solutions? 

 

Thanks!

9 Replies

@Unperterbed 

I believe you have to split your second formula as you can't have two return arrays. Unless I'm totally mistaken. See attached.

 

@Riny_van_Eekelen I think you're right, but that's what I'd like to avoid! I *love* that XLOOKUP can return multiple columns, and that structured references makes it so easy (e.g., Table1[ [Sales]:[Commission] ]), but it seems like an unnecessary constraint that the columns must always be adjacent to each other.

@Unperterbed 

If you use titles for the result, that could be

image.png

=INDEX(XLOOKUP($B15,Table1[Sales Person],Table1),,XMATCH($C14:$D14,Table1[#Headers],0))

Just be sure titles here are exactly the same as table headers (I corrected by copy/paste)

@Sergei Baklan Wow! Definitely a creative approach; I never would have thought to use XLOOKUP as a way to return an array to use with the INDEX function.

 

Even though that accomplishes exactly what I was looking for, it still seems far more complex than it should be (I say this, of course, in total ignorance of what it would take to develop what I'm imagining).

 

I suppose I'm just curious why I *can't* simply give a list of header names in the structured reference, such as:

 

 

=XLOOKUP(B15,Table1[Sales Person],Table1[ [Sales Amount] , [Commission Amount] ])

 

 

Should this be a feature request? It seems like it'd be a flexible, powerful, and intuitive feature


@Sergei Baklan In this case it doesn't need to be an array of ranges. It could just be a list or array indicating what columns from the array it wants returned. For example, taking something like {1,0,1,1,0,1} as another argument would specify that you want the 1st, 3rd, 4th, 6th col

@David_pdx 

Sorry, I didn't catch. For array as {1,0,1,1,0,1} FILTER returns columns marked in it as TRUE. In previous formula INDEX returns columns which numbers returned in array by XMATCH. But XLOOKUP doesn't return array of ranges. Did I miss something?

@Unperterbed 

Much as I love XLOOKUP, I think INDEX/XMATCH/XMATCH probably has the edge when it comes to returning two-way lookups as an array.  For example

= LET(
   headers, {"Sales Amount","Commission Amount"},
   fieldIndices, XMATCH(headers, tblSales[#Headers]),
   recordindices, XMATCH(SalesPeople, tblSales[Sales Person]),
   INDEX( tblSales, recordindices, fieldIndices) )

returns the grey-shaded area of

image.png

To sum the amounts and append the total column for each sales person

= LET(
\0, "Collect data",
    recordindices, XMATCH(SalesPeople, tblSales[Sales Person]),
    headers, {"Sales Amount","Commission Amount"},
    fieldIndices, XMATCH(headers, tblSales[#Headers]),
    subTable, INDEX( tblSales, recordindices, fieldIndices),
\1, "Calculate totals for each",
    totalAmounts, MMULT( subTable, {1;1} ),
\2, "Append totals",
    IF( {1,2,3} < 3, subTable, totalAmounts) )

Who was it that said a formula should be no longer than one's thumb?