Reverse columns of spreadsheet - google-sheets

I want to reverse columns of spreadsheet. Sample is "p227", "s121", "p117", "p252", "s215" in column "A1:E1". Values are random. I want to reverse this column like "s215", "p252","p117", "s121", "p227".
I tried =TRANSPOSE(SORT(TRANSPOSE(A1:E1),1,false)). But output is "s255", "s121", "p212", "p187", "p121". Values are sorted. This doesn't reverse columns. Is there way to solve this? Should I use GAS?
Sample
A B C D E
1 p227 s121 p117 p252 s215
Expected result
A B C D E
1 s215 p252 p117 s121 p227
Thank you so much for your time.

Try
=TRANSPOSE(SORT(TRANSPOSE(A1:E1),TRANSPOSE(COLUMN(A1:E1)),0))
SORTs using COLUMN numbers in descending order
TRANSPOSE is needed as SORT only works on rows

Here's a general one
Method:
Transpose
Add row numbers
Sort descending by row number
Transpose
Remove row numbers
Formula:
=ArrayFormula(query(TRANSPOSE(sort({row(indirect("1:"&columns(A1:E2))),transpose(A1:E2)},1,false)),"select * offset 1"))
EDIT
On reflection this would have been a bit neater, adding column numbers before transposing and avoiding the Indirect:
=ArrayFormula(query(TRANSPOSE(sort(transpose({COLUMN(A1:E2);A1:E2}),1,false)),"select * offset 1"))

You can manually reassemble the columns into the order you like by using the array literal syntax:
={A:A,F:F,E:E,D:D,C:C,B:B}
Alternately, use QUERY() to re-order your columns.
QUERY(A:F, "SELECT A, F, E, D, C, B")

Related

Arrayformula to calculate sum for each entry in a list

I need to automatically calculate the respective score of list of entries in a list. Basically I need something in the lines of "If letters in column F exist in column A, then sum for each entry in column A and if letter not found in F then return 0". It should be an array type of formula.
I tried both query function, which didn't work the way I wrote it, and arrayformula, but it doesn't give me sum per row.
=query(b3:g,"select sum (G) when F contains B group by B")
=arrayformula(sumifs(G4:G,B4:B,F4:F))
I know you can use a combination with mmult, but I don't know how to combine that with matching the column values for the letters in the list.
The file is listed below and is structured as following.
The score list in column A is fixed, and each month data in F-G column is posted, if the score is 0 for that month then the letter doesn't show up in the F column. I need an automatic way to calculate so I get the numbers in sheet 2.
https://docs.google.com/spreadsheets/d/1eyyqNL-LKw5F4kdDnbAPzN28jc4kWKMCJ-GAkn2yoXg/edit?usp=sharing
MAP() or BYROW() will do the trick. Try below formula-
=MAP(B3:B17,LAMBDA(x,SUMIFS(G3:G,F3:F,x)))
For >300 use-
=MAP(B3:B17,LAMBDA(x,SUMIFS(G3:G,F3:F,x,G3:G,">300")))
To make your input reference dynamic can use B3:INDEX(B3:B,COUNTA(B3:B)) as array argument or MAP() function. Try-
=MAP(B3:INDEX(B3:B,COUNTA(B3:B)),LAMBDA(x,SUMIFS(G3:G,F3:F,x)))

Using Google Sheets ARRAYFORMULA to Concatenate Series of Item Based from Lowest to Highest Values per Row

I have a table with three Columns:
Column A: name of Item,
Column B: Lowest value of series,
Column C: the Highest value of series.
enter image description here
What I want to achieve is:
Generate series of item sequence from lowest number to highest number per row
So Apple 7 9 will yield: "Apple_7", "Apple_8", "Apple_9"
Concatenate/Join such sequence per row into Column D
So
Item
From
Until
Result
Apple
7
9
"Apple_7, Apple_8, Apple_9"
Berry
3
8
"Berry_3, Berry_4, Berry_5, Berry_6, Berry_7, Berry_8"
Doing it all using one Arrayformula, so that new row added can be automatically calculated.
Here is example sheet: https://docs.google.com/spreadsheets/d/1R5raKmmt5-aOIorAZGHjv_-fdySKWjCMB_FRQwm1vag/edit#gid=0
I tried in Column D:
arrayformula(textjoin(", ",true,arrayformula(A3:A&"_"&sequence(1,C3:C-B3:B+1,B3:B,1))))
Apparently, the sequence function only take value from Column B and join it in first row.
Any help will be appreciated.
Try below BYROW() formula (see your file, harun24hr sheet).
=BYROW(A3:INDEX(A3:A,COUNTA(A3:A)),LAMBDA(x,TEXTJOIN(";",1,INDEX(x&"_"& SEQUENCE(INDEX(C:C,ROW(x))-INDEX(B:B,ROW(x))+1,1,INDEX(B:B,ROW(x)))))))
Here A3:INDEX(A3:A,COUNTA(A3:A)) will return a array of values as well cell reference from A3 to last non empty cell in column A (Assume you do not have any blank rows inside data). If you have blank row, then you have to use different approach. See this post by #TheMaster
Then LAMBDA() will apply TEXTJOIN() and SEQUENCE() function for each cell of B as well as C column.
SEQUENCE() will make series from start to end number and by concatenating A column and will generate your desired strings.
Finally TEXTJOIN() will join all those strings with delimiter to a single cell.
try REDUCE:
=INDEX(QUERY(REDUCE(, A3:INDEX(A:A, MAX(ROW(A:A)*(A:A<>""))),
LAMBDA(x, a, {x; JOIN(, LAMBDA(i, f, u, i&"_"&SEQUENCE(1, u-f+1, f)&";")
(a, OFFSET(a,,1), OFFSET(a,,2)))})), "offset 1", ))

How to Make Array Formula To Lookup Matching Values

As you'll see in my sample sheet, I've already found the match between two columns (C and D) and put the results in E. So these values correspond to a value from C, but are in a different order.
Now I want the values from column A, but I need for the formula to match E's value to the correct row in column C first to get the correct corresponding subject in column A.
LMK if my description needs improvement, I just rewrote it about 3 times.
I think this does what you want. There's no index function for array so you have to reconstruct a vlookup.
=filter(If(E2:E="","",vlookup(E2:E,{C:C,A:A},2,false)),not(isblank(A2:A)))
try:
=INDEX(IFNA(VLOOKUP(E2:E, {C:C, A:A}, 2, )))

Google Sheets - Query - Running Total below dynamic results

Testing Sheet:
Wondering if there is a witty way to add a Total to the last row +1 of
a Query result.
See Sheet 'Lookup' for a static example of what I am asking for.
I don't know if there is a way to have a hidden column that calculates
transposed only under the last row of a query, or if there is a smart
way to work Query for this answer.
All great answers. Each on very useful in its use case.
Макс Махров gets the answer with using a query statement.
Now I was not keen on having an extra sheet to hold the totals so I added a row at the top which I can simply hide and used this formula:
query({Orders!A:E;A1:E1},"select Col1, Col3, Col4 where Col2 = '"&C3&"' order by Col4",1)
Only problem I have is trying to figure out how to add TEXT to the bottom row, it seems to only want numerical input.
How do I fix this? What am I glitching?
Thanks !
Mars
The trick is to make second query and count totals for selected product.
Plan of actions:
add new sheet with query on it, something like this: =QUERY(Orders!A:E,"select B, 0, sum(D) where B like '"&Lookup!C2&"' Group by B",0)
Prepare arrayformula which combines data in Lookup sheet: = ArrayFormula({Importrange(1),Importrange(2)}) Note that number of columns must retain the same.
Edit query so it takes Col1, Col2, Col3... instead of A, B, C...
Make word 'total' visible instead of zero. Set number format: 0;0;total Set it for range B9:B on Lookup sheet
Make Conditional Formatting with formula =and($B4 =0,isnumber($B4)) for range A4:C on Lookup sheet.
That's seems have to complete the task.
Hope it Helps!
Your Example
Working example.
Here is one way:
Put TOTAL way down in row 1000
Select the range A3:C999. Select data > filter to create filters
Select C3, set the filter to hide all blanks
A second way is to limit the query result to show only the top 8 results:
Change your query to =query(Orders!A:E, "select A, C, D where B = '"&C2&"' order by D desc limit 8",1) It will reverse-order column D (largest first), and set row limit to 8.
Change the formula of your TOTAL to =sumif(Orders!B:B,C2,Orders!D:D)
Try this formula in the column adjacent to your query:
=ArrayFormula({$C$4:offset($C$4,count($C$4:$C),0,1,1);sum($C$4:offset($C$4,count($C$4:$C),0,1,1))})
It duplicates your column of values (I haven't figured out a way around that yet) and then adds a total to the bottom of that column, and changes dynamically with the range from your query.
Here's a working version.
Interesting challenge! It got the old grey matter turning... ;)
Thanks,
Ben

Find and replace by multiple patterns

Suppose I've got a text values column (named Data), generated by =unique() function. Also, there is an array of patterns to find and replace for (Find and Replace columns).
Which formula should I use to scan each cell in Data for multiple patterns in Find and replace it, if match?
Data Find Replace Result
1 a c z a
2 b f y b
3 c e x z
4 d d
5 e x
6 c z
Tried =SUBSTITUTE() and =IF() functions, but it fails, when I set an array of patterns, instead of single one.
If the table you is in range A1:E7, try this formula
=TRANSPOSE(SPLIT(REGEXREPLACE(REGEXREPLACE(REGEXREPLACE(ARRAYFORMULA(CONCATENATE($B$2:$B$7&"|")),$C$2,$D$2),$C$3,$D$3),$C$4,$D$4),"|"))
You can read further about this in an older post and google docs forum.
Array solution
This formula takes range to replace, so it can be used for variable number of patterns:
=QUERY(ARRAYFORMULA({REGEXMATCH(A2,$B$2:$B$4),
REGEXREPLACE(A2,$B$2:$B$4,$C$2:$C$4)}),
"select Col2 order by Col1 desc limit 1")
or this one:
=INDEX(ArrayFormula(REGEXREPLACE(A2,$B$2:$B$4,$C$2:$C$4)),IFERROR(MATCH(A2,$B$2:$B$4,0),1))
or this:
=IFERROR(INDEX($C$2:$C$4,MATCH(A2,$B$2:$B$4,0)),A2)
The formula is need to be dragged down.
Single Formula & Array solution
Also this single ArrayFormula will do the trick:
=ArrayFormula(trim(transpose(query({IF(--REGEXMATCH(TRANSPOSE(A2:A7),$B$2:$B$4)=1,
REGEXREPLACE(TRANSPOSE(A2:A7),$B$2:$B$4,$C$2:$C$4),"");
TRANSPOSE(if(--not(REGEXMATCH(A2:A7,JOIN("|",B2:B4))),A2:A7,""))},,COUNTA(A2:A)))))
or this shorter formula:
=ArrayFormula(IFERROR(VLOOKUP(MATCH(A2:A7,B2:B4,0),{ROW(INDIRECT("a1:a"&COUNTA(C2:C4))),C2:C4},2,0),A2:A7))
Please, see explanations in Sample file

Resources