0

If an array is provided as an input to a normally scalar argument of some functions like:

  • INDEX
  • GCD
  • GOOGLETRANSLATE
  • OFFSET
  • QUERY
  • IMPORTRANGE
  • AND

It doesn't return an array (even if wrapped by ARRAYFORMULA); it only gets the value for the first item in the array. Take this example,

Key Value
1 A
2 B
3 C
4 D
5 E
6 F
7 G

I want to get row 1 and row 5's Value. I tried

=INDEX(A2:B16,{1;5},2)

Syntax for INDEX is INDEX(array, row,column). When a array is provided as a row argument, It only returns the value for first item, 1 instead of returning the value for {1;5}.

Actual output Expected output
A A
E

How to fix?

The God of Biscuits
  • 2,029
  • 2
  • 3
  • 10
TheMaster
  • 45,448
  • 6
  • 62
  • 85
  • This is a self answered question as this is commonly asked and I couldn't find any good canonical answer. – TheMaster Oct 31 '22 at 08:31
  • I think the wording of the first part of the question description could be tightened up a bit: 'If an array is provided as a input to a normally *scalar* argument of some functions like [those listed] it doesn't return an array (even if wrapped by ARRAYFORMULA)'. Clearly most of the functions listed do have at least one argument which requires an *array or range*, and this isn't the one being referred to. – The God of Biscuits Nov 01 '22 at 23:14
  • @TheGodofBiscuits Would you like to suggest a edit? – TheMaster Nov 03 '22 at 10:28
  • 1
    I have just done so - see what you think. – The God of Biscuits Nov 03 '22 at 10:59

1 Answers1

1

To achieve the result you're looking for, you can use BYROW to supply the argument once per array:

=BYROW({1;5},LAMBDA(row,INDEX(A2:B16,row,2)))

BYROW sends the array argument provided once per row to the function inside LAMBDA. As a general formula,

=BYROW(range, LAMBDA(row, your_formula(row)))

If you want to send two arguments, use MAP instead.

=MAP({1;5},{1;2},LAMBDA(arr_1,arr_2,INDEX(A2:B16,arr_1,arr_2)))

This will get row 1, column 1 and row 5, column 2 respectively.

Actual output
1
E

MAP supports unlimited number of arguments and therefore can be used with complex formulas.

Caveat: Only one value per function can be returned. If you want to return more values, use SPLIT/TRANSPOSE/SPLIT technique mentioned here

TheMaster
  • 45,448
  • 6
  • 62
  • 85