I have a Google Sheet formula designed to pull the top 5 values out of a range of columns, display each of them separated by columns, calculate them as a percentage of a reference column, and append the name of the column each percentage is from. The formula looks like this:
=TEXTJOIN(", ", TRUE, ARRAYFORMULA(INDEX(C$1:AE$1, MATCH(LARGE(C2:AE2, SEQUENCE(5, 1)), C2:AE2, 0) + COLUMN(C2) - 3) & ": " & ROUND(LARGE(C2:AE2, SEQUENCE(5, 1)) / AH2 * 100, 2) & "%"))
It works correctly to pull the values
, but it appends the name of "ColumnC" in front of every percentage, instead of the column title that the value was from.
E.g: the result returned should be something like:
"Apples: 20%, Oranges: 15%, Lemons: 12%, Bananas: 10%, Grapes: 7%"
but it returns:
"Apples: 20%, Apples: 15%, Apples: 12%, Apples: 10%, Apples: 7%"
Is there a way I can fix this so it pulls the appropriate column header name each % falls under instead?
Thanks!
Edit: sample data looks like this, sorry if I balls this up as first time trying:
CODE | FruitBasket_Name | Apple | Banana | Peach | Pear | Grapefruit | Plum | Grape | Mango | Watermelon | Lemon | Lime | Mandarin | Jackfruit | Blueberry | Pineapple | Kiwi | Boysenberry | Fig | Lychee | Blood Orange | Papaya | Passionfruit | Cantaloupe | Serbian | Cranberry | Cherry | Clementine | Persimmon | Satsuma | Raspberry | Rockmelon | TotalFruit | Top5 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
10001 | FruitBasket_1 | 12 | 3 | 0 | 0 | 0 | 10 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 8 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 5 | 33 | Apple: 36.36%, Apple: 30.3%, Apple: 24.24%, Apple: 9.09%, Apple: 0% |
10002 | FruitBasket_2 | 606 | 15 | 179 | 276 | 8 | 379 | 87 | 12 | 66 | 98 | 44 | 118 | 964 | 4 | 94 | 77 | 160 | 13 | 13 | 40 | 54 | 14 | 80 | 90 | 7 | 69 | 10 | 175 | 6 | 1313 | 151 | 4200 |
Try the following QUERY()
formula-
=TEXTJOIN(", ",1,QUERY(SORT(TRANSPOSE({C1:AG1;INDEX(C2:AG2/AH2)}),2,0),"limit 5 format Col2 '0.00%'"))
Edit: Adding updated formula using
BYROW()
.
=BYROW(C2:AG,LAMBDA(r,IF(COUNTIF(r,">0")=0,,TEXTJOIN(", ",1,QUERY(TRANSPOSE({C1:AG1;INDEX(r/CHOOSEROWS(AH:AH,ROW(r)))}),"order by Col2 DESC limit 5 format Col2 '0.00%'")))))