Query + Substitute - google-sheets

I need a multiple substitution for some columns.
Source: Sheet1
Dest: Sheet2
I'm proceeding like this:
In A1 of Sheet2
=QUERY(Sheet1!A2:D)
Output to substitute
A
B
C
D
1
/1
one
*1
2
/4
two
*2
3
/7
three
*3
4
/9
four
*4
5
/7
five
*5
In columns B and D I have to replace / and *
Here is what I need
Final Result
A
B
C
D
1
1
one
1
2
4
two
2
3
7
three
3
4
9
four
4
5
7
five
5
This is the function I'm using without the desired result
=SUBSTITUTE(SUBSTITUTE(QUERY(Sheet1!A2:D), B1:B, "/", "" ), D1:D, "*", "" )
As result I have 1 in cell A1
Please give me some help?
Thanks

try:
=INDEX(REGEXREPLACE(QUERY(Sheet1!A2:D)&"", "^\/|^\*", ))

Try:
=arrayformula(query({Sheet1!A:D,if(Sheet1!B:B<>"",value(regexreplace({Sheet1!B:B,Sheet1!D:D},"^\/|^\*",)),)},"select Col1,Col5,Col3,Col6",0))
Alternatively:
=arrayformula(query({Sheet1!A:D,value(regexreplace({Sheet1!B:B,Sheet1!D:D},"^\/|^\*",))},"select Col1,Col5,Col3,Col6 where Col1 is not null",0))
If you have a header column, change ,0 to ,1 at the end.

Related

Google Sheets auto increment column B with empty cells restarting from 1 at each new category String in Column A instead of continuous incrementing

I found this partial solution to my problem:
Google Sheets auto increment column A if column B is not empty
With this formula:
=ARRAYFORMULA(IFERROR(MATCH($B$2:$B&ROW($B$2:$B),FILTER($B$2:$B&ROW($B$2:$B),$B$2:$B<>""),0)))
What I need is the same but instead of continuous numbers I'd need it to restart incrementing from 1 at each new category string on an adjacent column (column A in example below, categories strings are A, B, C, D etc.).
For example:
Problem with formula in C12 and C15 (added numbers 1 and 2)
Needed result in column D, as with D11 and D19 restarts incrementing from 1 at new category string)
1
needed result
2
A
1
1
1
3
A
4
A
5
A
1
2
2
6
A
7
A
8
A
9
A
1
3
3
10
A
11
B
1
4
1
12
B
1
13
B
14
C
1
5
2
15
C
2
16
C
17
C
1
6
3
18
C
19
D
1
7
1
20
D
21
D
22
D
1
8
2
23
D
24
D
1
9
3
25
D
26
D
27
D
1
10
4
28
D
29
D
try:
=INDEX(IF(B2:B="",,COUNTIFS(A2:A&B2:B, A2:A&B2:B, ROW(A2:A), "<="&ROW(A2:A))))
or:
=INDEX(IF(B2:B="",,COUNTIFS(A2:A&IF(B2:B<>"", 1, ), A2:A&IF(B2:B<>"", 1, ), ROW(A2:A), "<="&ROW(A2:A))))
Here's another similar solution.
=ArrayFormula(if(B2:B="",,countifs(A2:A,A2:A,B2:B,"<>",row(A2:A),"<="&row(A2:A))))

How to use criteria based on a set of numbers with SUMIFS?

I have table like this
A | B | C | Val
5 | 5 | 5 | 10
5 | 4 | 5 | 20
4 | 4 | 4 | 5
3 | 3 | 4 | 7
Is there any way sumifs can use criteria based on a set of numbers?
E.g. I want to sum the Val column based on these cells:
A: 5
B: 4,5 -> means get 4 or 5
C: 5
Then the result is 10 + 20 = 30
A: 3,4,5
B: 3,4
C: 4
Then the result is 5 + 7 = 12
You can combine SUMPRODUCT and MMULT functions to get dynamic formula:
=SUMPRODUCT(
($D$2:$D$5)
*MMULT(--($A$2:$A$5=$I$2:$K$2),ROW($A$1:$A$3)^0)
*MMULT(--($B$2:$B$5=$I$3:$K$3),ROW($A$1:$A$3)^0)
*MMULT(--($C$2:$C$5=$I$4:$K$4),ROW($A$1:$A$3)^0)
)
There's no simple way to do it with SUMIFS as it does not let you express OR conditions with Array literals like in Excel.
However, Google Sheets does offer the QUERY function.
Assume that criteria for A is in H1:1, B is in H2:2, C is in H3:3:
=QUERY(A:D,"select sum(D) where
(A="&TEXTJOIN(" or A=",1,H1:1)&") and
(B="&TEXTJOIN(" or B=",1,H2:2)&") and
(C="&TEXTJOIN(" or C=",1,H3:3)&")")
This gives you a header too. To get just the sum you can INDEX it:
=INDEX(
QUERY(A:D,"select sum(D) where
(A="&TEXTJOIN(" or A=",1,H1:1)&") and
(B="&TEXTJOIN(" or B=",1,H2:2)&") and
(C="&TEXTJOIN(" or C=",1,H3:3)&")")
,2)
With this, you don't have to worry about headers or the number of criteria.
For a way to do it without QUERY see #basic's solution.
I think you can do it with relatively simple SUMPRODUCT(MMULT(... as shown here on this sheet.
=SUMPRODUCT(E4:E,MMULT(N(REGEXMATCH(TO_TEXT(B4:D),B2:D2)),SEQUENCE(3,1,1,0))=3)
Note the use of the number 3 in two places which is indicative of how wide your data range is. It can be made dynamic, but it's easier to see how the formula works with the 3s as "hard" numbers for now.

How to move data to right empty cells in Google Sheets?

I would like to design an organization chart and I am building a supervisor tree like this.
9 8 7 6 5 4 3 2 1 0
A B C
D
E F B C
By using vlookup I got the table like above.
Now I would like the table to be like this:
9 8 7 6 5 4 3 2 1 0
A B C
D
E F B C
I tried
=IFS(ISBLANK(I2),H2,ISBLANK(H2),G2,ISBLANK(G2),F2,ISBLANK(F2),E2,ISBLANK(E2),D2,ISBLANK(D2),C2,ISBLANK(C2),B2,ISBLANK(B2),A2)
But it turned out to be #REF
I just would like to move the data to the right, to get rid of those empty cells.
Does anyone know how to do it? Or can we realize it in BigQuery?
try:
=ARRAYFORMULA(TRIM(SUBSTITUTE(IFERROR(SPLIT(REPT("♦♠",
IF(A2:A<>"", MMULT(IF(A2:J<>"", 0, 1), TRANSPOSE(COLUMN(A2:J)^0)), ))&
TRANSPOSE(QUERY(TRANSPOSE(IF(A2:J<>"", "♦"&A2:J, )),,999^99)), "♦")), "♠", )))

Sum data in column with criteria in row

I wish to make a formula to sum up the value with 2 criteria, example show as below:-
A B C D E
1 1-Apr 2-Apr 3-Apr 4-Apr
2 aa 1 4 7 10
3 bb 2 5 8 11
4 cc 3 6 9 12
5
6 Criteria 1 bb
7 Range start 2-Apr-16
8 Range End 4-Apr-16
9 Total sum #VALUE!
tried formula
1 SUMIF(A2:A4,C6,INDEX(B2:E4,0,MATCH(C7,B1:E1,0)))
* Only return 1 cell value
2 SUMIF(A2:A4,C6,INDEX(B2:E4,0,MATCH(">="&C7,B1:E1,0)))
* Showed N/A error
3 SUMIFS(B2:E4,A2:A4,C6,B1:E1,">="&C7,B1:E1,"<="&C8)
* Showed #Value error
Hereby I attached a link of picture for better understanding :
Can anyone help me on the formula?
I figured out the solution with step evaluation:
=SUMIF(B1:F1,">="&C7,INDEX(B2:F4,MATCH(C6,A2:A4,0),0)) -
SUMIF(B1:F1,">"&C8,INDEX(B2:F4,MATCH(C6,A2:A4,0),0))

Using COUNTIFS on 3 different columns and then need to SUM a 4th column?

I have written this formula below. I do not know the correct part of this formula that will add the numbers I have in Column AB2:AB552. As it is, this formula is counting the number of cells in that range that has numbers in it, but I need it to total those numbers as my final result. Any help would be great.
=COUNTIFS(Cases!B2:B552,"1",Cases!G2:G552,"c*",Cases!X2:X552,"No",**Cases!AB2:AB552,">0"**)
Assuming you don't actually need the intermediate counts, the sumifs function should give you the final result:
=SUMIFS(Cases!AB2:AB552,Cases!B2:B552,1,Cases!G2:G552,"c",Cases!X2:X552,"No",Cases!AB2:AB552,">0")
Testing this with some limited data:
Row B G X AB
2 2 a No 10
3 1 c No 24
4 2 c No 4
5 1 c No 0
6 1 a Yes 9
7 2 c No 12
8 2 c No 6
9 2 b No 0
10 1 b No 0
11 1 a No 10
12 2 c No 6
13 1 c No 20
14 1 c No 4
15 1 b Yes 22
16 1 b Yes 22
the formula above returned 48, the sum of AB3, AB13, and AB14, which were the only rows matching all 4 criteria

Resources