Search code examples
google-sheets-formulaindex-match

Trying to return values from a table based on 4 criteria - not exact matches


I'm trying to return shipping fees depending on the marketplace, weight, height, length and width of the item.

I tried using index match with multiple criteria, but haven't been successful in making sure it takes all the criteria into account.

The issue is that the weight and dimensions won't be exact matches, they are categorized in certain batches.

If it would work in an array, I would manage with the current formula, but because it has ifs and ands, it has to be on every row.

Here's a link to a sample spreadsheet, one sheet has the calculation I'm currently using with the weight and dimension data. The other is the shipping prices laid out in a table.

https://docs.google.com/spreadsheets/d/1ybaS8KYUfiDkvG-5wGcFN3XsyC-SLfZrwChV-xNKMNk/edit?usp=sharing

This is the formula I'm currently using, it works fine but it's running a bit slow.

=IFNA(
IFS(AND(E3<=20,F3<=15,D3<=1,C3<=80),
      IFS('MAIN TEMPLATE'!$F$1="UK",0.86,
          'MAIN TEMPLATE'!$F$1="DE",1.19,
          'MAIN TEMPLATE'!$F$1="FR",1.6,
          'MAIN TEMPLATE'!$F$1="IT",2.01),
  AND(E3<=33,F3<=23,D3<=2.5),
    IFS(C3<=60,
      IFS('MAIN TEMPLATE'!$F$1="UK",1.03,
          'MAIN TEMPLATE'!$F$1="DE",1.38,
          'MAIN TEMPLATE'!$F$1="FR",1.87,
          'MAIN TEMPLATE'!$F$1="IT",2.12),
        C3<=210,
      IFS('MAIN TEMPLATE'!$F$1="UK",1.21,
          'MAIN TEMPLATE'!$F$1="DE",1.58,
          'MAIN TEMPLATE'!$F$1="FR",2.54,
          'MAIN TEMPLATE'!$F$1="IT",2.38),
        C3<=460,
      IFS('MAIN TEMPLATE'!$F$1="UK",1.59,
          'MAIN TEMPLATE'!$F$1="DE",1.88,
          'MAIN TEMPLATE'!$F$1="FR",3.32,
          'MAIN TEMPLATE'!$F$1="IT",3.07)),
  AND(E3<=33,F3<=23,D3<=4),
      IFS('MAIN TEMPLATE'!$F$1="UK",1.91,
          'MAIN TEMPLATE'!$F$1="DE",2.14,
          'MAIN TEMPLATE'!$F$1="FR",3.64,
          'MAIN TEMPLATE'!$F$1="IT",3.12),
  AND(E3<=33,F3<=23,D3<=6),
      IFS('MAIN TEMPLATE'!$F$1="UK",2.09,
          'MAIN TEMPLATE'!$F$1="DE",2.51,
          'MAIN TEMPLATE'!$F$1="FR",4.04,
          'MAIN TEMPLATE'!$F$1="IT",3.26),
  AND(E3<=35,F3<=25,D3<=12),
    IFS(C3<=150,
      IFS('MAIN TEMPLATE'!$F$1="UK",2.38,
          'MAIN TEMPLATE'!$F$1="DE",3.02,
          'MAIN TEMPLATE'!$F$1="FR",4.33,
          'MAIN TEMPLATE'!$F$1="IT",3.8),
        C3<=400,
      IFS('MAIN TEMPLATE'!$F$1="UK",2.51,
          'MAIN TEMPLATE'!$F$1="DE",3.11,
          'MAIN TEMPLATE'!$F$1="FR",4.74,
          'MAIN TEMPLATE'!$F$1="IT",4.06))))

Solution

  • Here's my suggestion. It uses BYROW to do apply to all the rows in your range. Then with COUNTA it finds that if the row is empty it won't calculate.

    With INDEX and XMATCH I'm limiting the range of fees to the column of the country, and with FILTER and the second BYROW comparing each value of A-D columns to see if they're smaller or equal to the parameters of the fees. Then with MIN it just find the smallest value for all the fees in which the package could be included:

    =BYROW(A2:D,LAMBDA(r,IF(COUNTA(r)=0,,
      MIN(FILTER(INDEX('Shipping table'!$E$2:$I$9,,XMATCH('MAIN TEMPLATE'!$F$1,'Shipping table'!$E$1:$I$1)),
        BYROW('Shipping table'!$A$2:$D$9,LAMBDA(each,PRODUCT(INDEX(--(each>=r))))))))))
    

    enter image description here