2

if I have a list of string like '0401 A', '0319 B' '0801 C' and they appears in different columns and rows in an excel sheet, what is the formula to get the max & min of these strings. In this case, max and min would be '0801 C' and '0319 B' if its sorted alphabetically. INDEX-MATCH doesn't seems to work since the strings are in multiple rows and columns

player0
  • 124,011
  • 12
  • 67
  • 124
lbzl
  • 67
  • 7

3 Answers3

6

You can use this for Maximum:

=SORTN(FLATTEN(A:C), 1, 0, 1, False)

and this for Minimum:

=SORTN(FLATTEN(A:C), 1, 0, 1, True)

I've just learned about FLATTEN: it is native but undocumented function in Google Sheets, which will make a 2D-range into a column - very useful stuff. I learned about it recently from @MattKing here on SO.

enter image description here

A:C could also be a bit pre-filtered of empty cells wtih FILTER(A:C, (A:A <> "") + (B:B <> "") + (C:C <> "")) or something else.

kishkin
  • 5,152
  • 1
  • 26
  • 40
  • thanks for sharing. The link in your answer just points to the image in this post. Could you make it point to the post you're referring to? – MattKing May 03 '20 at 13:32
  • @MattKing fixed that. Thanks again! `FLATTEN` is recently my favorite hammer. – kishkin May 03 '20 at 13:41
  • 1
    you and I are gonna spread it across the land. Hopefully they don't scrap it and we end up leaving a huge swath of broken sheets in our wake. – MattKing May 03 '20 at 13:51
  • Thanks. I'm surprised there isn't a function like "MAX" in numerical values for string. – lbzl May 03 '20 at 18:02
0

Assume your data housed in A5, B2 and C3

In Max value result F1, enter formula :

=INDEX(T(INDIRECT({"B2","C3","A5"})),MATCH(MAX(0+LEFT(T(INDIRECT({"B2","C3","A5"})),4)),0+LEFT(T(INDIRECT({"B2","C3","A5"})),4),0))

In Min value result F2, enter formula :

=INDEX(T(INDIRECT({"B2","C3","A5"})),MATCH(MIN(0+LEFT(T(INDIRECT({"B2","C3","A5"})),4)),0+LEFT(T(INDIRECT({"B2","C3","A5"})),4),0))

enter image description here

bosco_yip
  • 3,762
  • 2
  • 5
  • 10
0

if each column contains only one value:

=ARRAYFORMULA(SORTN(TRIM(TRANSPOSE(QUERY(TRANSPOSE(A:C);;9^9))); 1; 0; 1; 0))

=ARRAYFORMULA(SORTN(TRIM(TRANSPOSE(QUERY(TRANSPOSE(A:C);;9^9))); 1; 0; 1; 1))

if there are multiple values in each column:

=ARRAYFORMULA(SORTN(TRIM(TRANSPOSE(SPLIT(QUERY(TRANSPOSE(QUERY(TRANSPOSE(
 IF(A:C="";;"♦"&A:C));;9^9));;9^9); "♦"))); 1; 0; 1; 0))

=ARRAYFORMULA(SORTN(TRIM(TRANSPOSE(SPLIT(QUERY(TRANSPOSE(QUERY(TRANSPOSE(
 IF(A:C="";;"♦"&A:C));;9^9));;9^9); "♦"))); 1; 0; 1; 1))
player0
  • 124,011
  • 12
  • 67
  • 124