Search code examples
formulalibreoffice-calc

In DGET function, how to use multiple cell ranges as search criteria?


I am using the DGET function in LibreOffice. I have the first table as shown below (top). I want to make second table (bottom). I can use DGET function where Database is the cell range containing top table and Database Field is "Winner".

Is it possible to have different cell ranges in Search Criteria, so that for each cell in row for Case #1 can have separate formula with a different search criteria as given in the first row of bottom table?

If I have to use separate continuous cell ranges for search criteria, then there would be [n*Chances] cell ranges, where n=total number of cases (~150 in my case) and Chances = possible number of Chance# (50 in my case).

Case | Chance#  |  Winner
-------------------------
1    |   7      |    Joe
1    |   9      |    Emil
1    |   10     |    Harry
1    |   11     |    Kate
2    |   1      |    Tom
2    |   3      |    Jerry
2    |   4      |    Mike
2    |   7      |    John

Case  |Chance#|Chance#|Chance#|Chance#|Chance#|Chance#|Chance#|Chance#|Chance#|Chance#|Chance#| 
      |="=1"  |="=2"  |="=3"  |="=4"  |="=5"  |="=6"  |="=7"  |="=8"  |="=9"  |="=10" |="=11" | ---- |="=50"  
 1    |       |       |       |       |       |       | Joe   |       |Emil   |Harry  | Kate  | ---- | 
 2    | Tom   |       |Jerry  |Mike   |       |       | John  |       |       |       |       | ---- | 

Solution

  • To do so, you need to change your approach, instead of using DGET, I'm using a rather more complex method:

    Considering your example:

    
           A       B       C       D       
    1      #       Case    Chance# Winner
    2      1       1       7       Joe
    3      2       1       9       Emil
    4      3       1       10      Harry
    5      4       1       11      Kate
    6      5       2       1       Tom
    7      6       2       3       Jerry
    8      7       2       4       Mike
    9      8       2       7       John
    10      
    11      Case\Chance#      1      2      3      4
    12            1
    13            2         Tom            Jerry      Mike  
    
    

    I use the following:

    
    =IF(SUMPRODUCT(($B$2:$B$9=$A12)*($C$2:$C$9=B$11)*($A$2:$A$9))> 0,INDEX($D$2:$D$9,SUMPRODUCT(($B$2:$B$9=$A12)*($C$2:$C$9=B$11)*($A$2:$A$9))),"")
    

    Let's ignore the IF, and focus on the real deal here:

    First, Get the row that matches your condition, $B$2:$B$9=$A12 and $C$2:$C$9=B$11 will result in a TRUE/FALSE arrays, multiply them to get a 0/1 array with only a single 1 for the match, now multiply by the ID to get the row number in your table. SUMPRODUCT will get you a single value (the row) from the result array. Finally use index to retrieve the desired value.

    The IF statement tests if a match do exist (SUMPRODUCT > 0), to filter out the cell with no match.