1

I have these 2 tables: enter image description here

On column B i'm trying to get one of the Header Names of a feature that is not empty on Table B. I want it to be selected randomly. The order of the items in Table A can be different than the order of the items in Table B, I'll need some sort of INDEX MATCH here too.

Excel Version: Office 365

Attempted Formula: I tried to base my formula on this:

=INDEX(datarange,RANDBETWEEN(1,COLUMNS(datarange)),1)

but there are more things to consider, like header name if the index match of the same fruit isn't empty, so I know it is more complex.

Any help will be greatly appreciated.

JvdV
  • 70,606
  • 8
  • 39
  • 70
Kobe2424
  • 147
  • 7

2 Answers2

3

Assuming you have Excel 365 and a volatile result is acceptable:

=LET(
    Fruits, Table_B[Fruit],
    Properties, Table_B[[Red]:[Green]],
    PropertiesHeaders, Table_B[[#Headers],[Red]:[Green]],
    ThisFruit, [@Fruits],

    ThisProperties, FILTER(Properties, Fruits = ThisFruit),
    ThisPropertiesFiltered, FILTER(PropertiesHeaders, ThisProperties <> 0),
    ThisPropertiesCount, COUNTA(ThisPropertiesFiltered),
    IndexRand, RANDBETWEEN(1,ThisPropertiesCount),
    IFERROR(INDEX(ThisPropertiesFiltered,IndexRand),"-")
)

ThisProperties is the row in Table_B for your fruit. I left out the column for the fruit names.

ThisPropertiesFiltered is the names of the properties that the fruit has. I filtered the header names based on if the fruit row had a non-zero value or not.

IndexRand gets a random number between 1 and the number of available properties. Note, if there are zero available properties, ThisPropertiesFiltered returns #CALC! so ThisPropertiesCount will return 1. This is handled later on.

Last we use INDEX to get the random property name. IFERROR returns "-" if no properties were available.

Here are the tables:

Table_A:

Fruits Result
Watermelon Heavy
Melon Green
Banana Tropic
Peach Red
Apple Green

Table_B:

Fruit Red Yellow Tropic Heavy Green
Apple x x
Banana x x
Peach x
Melon x
Watermelon x x
Dave Thunes
  • 260
  • 2
  • 9
  • amazing. thank you guys, both answers helped me, I don't know which to mark as the correct answer. I have another question regarding random value, much easier I believe, should I post it to you guys or open a new thread? – Kobe2424 Oct 28 '21 at 15:25
2

Since you have access to dynamic arrays you could try:

enter image description here

Formula in B2:

=LET(X,FILTER(E$1:I$1,INDEX(E$2:I$6,MATCH(A2,D$2:D$6,0),0)<>"","No Feature"),INDEX(X,RANDBETWEEN(1,COUNTA(X))))

Or without LET():

=@SORT(SORT(CHOOSE({1;2;3},E$1:I$1,FILTER(E$2:I$6,D$2:D$6=A2),RANDARRAY(1,5)),3,1,1),2,-1,1)

If you are working through actual tables this should spill down results under Random Feature automatically. However, if one does not use tables, you could nest the above in BYROW() if you are an 365-insider:

=BYROW(A2:A6,LAMBDA(r,LET(X,FILTER(E$1:I$1,INDEX(E$2:I$6,MATCH(r,D$2:D$6,0),0)<>"","No Feature"),INDEX(X,RANDBETWEEN(1,COUNTA(X))))))

This would not work with the 2nd option where we used '@' to parse only the topleft value of our array (implicit intersection).


The idea is that:

  • A combination of INDEX() & MATCH() will 'slice' the row of interest out of the lookup-table based on our input.
  • In the 2nd step we'd use FILTER() to only leave those headers where the elements from the herefor returned array are not empty. In the case all elements are empty, this function will return the value "No Feature" as a headsup for the users.
  • In our final step we combine INDEX() with RANDBETWEEN(). The latter will return a random integer between a LBound (1 in our case) and an Ubound which we based on the amount of returned elements.

I tried to visualize this below.


enter image description here

JvdV
  • 70,606
  • 8
  • 39
  • 70
  • These are actual tables. defined as "Table_A" & "Table_B". how will I change the formula please to these tables? thank you so much! – Kobe2424 Oct 28 '21 at 15:11
  • 1
    @JvDV, You beat me to it! It's nice to see all the new formulas available to insiders. – Dave Thunes Oct 28 '21 at 15:12
  • @Kobe2424, change the range-references into the table-references. Just select them and drag a new table-reference. – JvdV Oct 28 '21 at 15:14
  • amazing. thank you guys, both answers helped me, I don't know which to mark as the correct answer. I have another question regarding random value, much easier I believe, should I post it to you guys or open a new thread? – Kobe2424 Oct 28 '21 at 15:25
  • New questions get new posts, but since you believe it's an easy one; make sure you research for the answer first since any new post should include what you have tried and what you need help with. As to which answer to accept is up to you. It does not matter. You can however vote up all answers that were helpfull to you @Kobe2424 – JvdV Oct 28 '21 at 15:26
  • I'll check now if nothing comes up I will create a new post. I'll also random between you guys who's get the accepted answer :) – Kobe2424 Oct 28 '21 at 15:30