Google sheet -> filtering a query - google-sheets

I created the following sheet:
https://docs.google.com/spreadsheets/d/1AJmzlBlrDZ0mfb_9aSm83fzZxrwYWOxWGxjNhlfqPLA/edit?usp=sharing
I am trying to find a formula to calculate the totals (C7, C8, C9) and wherever it is copied ie C17, C18, C19)
So for C7 I want to sum column D (Duration) wherever:
the Date (col A) is the same as the total row date (that is A7)
The Category (col E) is Internal
Similarly for So for C8 I want to sum column D (Duration) wherever:
the Date (col A) is the same as the total row date (that is A8)
The Category (col E) is External
and
for C9 I want to sum column D (Duration) wherever:
the Date (col A) is the same as the total row date (that is A9)
Has any category
I tried for C7 -
=QUERY({A:E, arrayformula(N(D:D))}, "select sum(Col6) where Col5='Internal' and Col1 = " & A7 & " label sum(Col6) ''",1)
but I get NA ??? any help ?
-

I wouldn't complicate life with a QUERY() here. Instead, just use FILTER().
Total Internal: =SUM(FILTER(D:D, A:A=A7, E:E="Internal"))
Total External: =SUM(FILTER(D:D, A:A=A8, E:E="External"))
Total All: =SUM(FILTER(D:D, A:A=A9))

Related

Arrayformula to replace sumifs based on criteria - grouping data into categories/types

In this spreadsheet
Cols G and H give the total of each Account Type, from the data (A1:D)
If sum Dr - sum Cr > 0, then only this is shown in col G.
=if(sumifs(C:C,A:A,F2)-sumifs(D:D,A:A,F2)>0, sumifs(C:C,A:A,F2)-sumifs(D:D,A:A,F2),"")
If sum Cr - sum Dr > 0, then only this is shown in col H
=if(sumifs(D:D,A:A,F2)-sumifs(C:C,A:A,F2)>0, sumifs(D:D,A:A,F2)-sumifs(C:C,A:A,F2),"")
I am looking for an array formula which replaces these summifs formula, so that if either new Account Types (e.g. Type E, Type F etc) are added or new rows of data are added, then the formula would automatically calculate the sum Dr or sum Cr, instead of having to copy the formula down
try this in cell G2:
=BYROW(F2:F,LAMBDA(fx,IF(fx="",,LAMBDA(cx,dx,{IF(cx-dx>0,cx-dx,),IF(dx-cx>0,dx-cx,)})(SUMIF(A:A,fx,C:C),SUMIF(A:A,fx,D:D)))))
-
Use query(), like this:
=arrayformula(
lambda(
aggregated,
lambda(
account, balance,
{
"Account Type", "Dr", "Cr";
account,
if( balance >= 0, balance, iferror(1/0) ),
if( balance < 0, -balance, iferror(1/0) )
}
)(
query(aggregated, "select Col1", 0),
query(aggregated, "select Col2", 0)
)
)(
query(
A3:D,
"select A, sum(C) - sum(D)
where A is not null
group by A
label sum(C) - sum(D) '' ",
0
)
)
)
You can set a QUERY that finds both entire columns like this:
=QUERY(A2:D,"SELECT A,SUM(C)-SUM(D),SUM(D)-SUM(C) where A is not null group by A",1)
And check with LAMBDA if there are negative values and change them to null:
=LAMBDA(a,INDEX(IF(a<0,,a)))(QUERY(A2:D,"SELECT A,SUM(C)-SUM(D),SUM(D)-SUM(C) WHERE A is not null group by A",1))
If the values in your result table are just for display, you can use the QUERY format clause to hide negative values, such that the whole thing can be generated within a single QUERY:
=QUERY(A2:D,"select A,sum(C)-sum(D),sum(D)-sum(C) where A is not null group by A label sum(C)-sum(D) 'Dr',sum(D)-sum(C) 'Cr' format sum(C)-sum(D) '0;',sum(D)-sum(C) '0;'",1)

How to type static date into formula calculation?

I am trying to create a formula that calculates the number of hours based on the pricing at the time the job was done and the total cost of the job in Google Sheets. Unfortunately, I'm running into a #ERROR when I try to run it and I think it has something to do with how the Dates are typed in and not the logic of the nested IF statements. Any thoughts? Can add more information if needed.
=IF((A2 >= 1/1/21 AND A2 <= 4/13/21), IF(E2 = "Elbow Grease", D2/165, D2/125), IF(E2 = "Elbow Grease", D2/195, D2/155))
A2 would be formatted like 1/1/21
E2 would be a string
D2 would be a numerical value (total cost)
try:
=IF((A2 >= "1/1/21") * (A2 <= "4/13/21"),
IF(E2 = "Elbow Grease", D2/165, D2/125),
IF(E2 = "Elbow Grease", D2/195, D2/155))

QUERY in ARRAYFORMULA to FILTER, JOIN and LEN

Given a table like this one:
A
B
C
D
E
1
Tese
(2021/039-07964)
(8146.000336/2021-60)
Tema 1092
2
Afetado
Tema 1092
Controvérsia 251
|3|Tese|(2021/0390796-6)||Tema 1093|Controvérsia 258|
|4|Afetado|||Tema 1093|Controvérsia 258|
|5|Tese|(2021/0390796-7)|(8146.000338/2021-50)||Controvérsia 238|
As long strings in Col A's rows are equal to "Tese", Col F should return a string (with line breaks) that compiles Col B to D strings that are not null in the same row.
So, row 1 of Col F should be:
F
(2021/0390796-4)(8146.000336/2021-60)Tema 1092
Row 2 of Col F should be:
F
(2021/039-07966)Tema 1093Controvérsia 258
And so on
I'm using a dropdown formula to do the job, which is working fine:
=IFERROR(IF(A1="Tese",JOIN(CHAR(10),FILTER(B1:E1, LEN(B1:E1))),""))
But it would be easier if it could be replaced by an array formula (the table is over 4,000 rows!).
I think it is possible to use a query, but I could not write the formula down.
The real table is here: link
Clear all contents of column G and try in G1
={"Informações completa da tese"; INDEX(IF(A2:A="Tese"; SUBSTITUTE(SUBSTITUTE(TRIM(TRANSPOSE(QUERY(TRANSPOSE(SUBSTITUTE(B2:F; " "; "$"));;ROWS(A2:A)))); " "; char(10)); "$"; " ");))}
See if that helps?

Google Spreadsheet, operations with above row cell in same column with arrayformula

I have arrayformula in the first row of a column so my values and calculations can start in Row 2 and for all the column length.
I have this situation:
https://docs.google.com/spreadsheets/d/11oDra7Vja4-5C0Uix7JTgLLSMG3gPj-6fkajXlWqqQk/edit?usp=sharing
I need a simply arithmetic operation:
Subtract above value of the same column for every row.
I'm using:
=arrayformula(IF(row(A:A)=1; "What I have now"; IF(ISBLANK(A:A); ""; A1:A-A2:A)))
but as you see is wrong.
How to do that?
UPDATED QUESTION:
And then in the second sheet I need a SUM operation with some blank cells in column:
How to do that?
https://docs.google.com/spreadsheets/d/11oDra7Vja4-5C0Uix7JTgLLSMG3gPj-6fkajXlWqqQk/edit#gid=931743679
If you want to have the array formula ion the header this is a bit weird as you need to allow the formula to technically access row 0, we can do this by constructing ranges.
=ArrayFormula(IF(
--(ROW(A1:A) > 2) + -ISBLANK(A1:A) = 1;
{0; A1:A} - {0; A2:A; 0};
""))
--(ROW(A1:A) > 2) + -ISBLANK(A1:A) = 1 Checks if the row is populated and not one of the first two rows in a way that works nicely with array formulas
{0; A1:A} - {0; A2:A; 0} does the following:
0 Data 156 123 110 95 42
- - - - - - -
0 156 123 110 95 42 0
= = = = = = =
0 33 13 15 53 42 42
N N Y Y Y Y N <- Is shown
^ ^ ^
| | Because Row is blank
| |
Because row not > 2, thus it is never evalauated even though the second would cause an error
I think this is quite tricky. The problem is that in an array formula the number of cells in each array must match - you can't mix an array starting in row 1 with an array starting in row 2 if they go all the way to the bottom of the spreadsheet.
Here is one way of getting the result you want
=arrayformula({"What I need";"";offset($A$1,1,0,count(A:A)-1)-offset($A$1,2,0,count(A:A)-1)})
You will need to change the ; and , for your locale.
I have built up an array using the {} notation to define the elements. In my locale a ; means go to the next row, so I have defined the first two cells directly as strings. After that I've chosen to use Offset to get the range A2:A5 (1 row down from A1, 0 rows across and 4 cells high) and subtract the range A3:A6 (2 rows down from A1, 0 rows across and 4 cells high) it so that gives me the other 4 cells.
B1 "What I need"
B2 ""
B3 A3-A2=33
B4 A4-A3=13
B5 A5-A4=15
B6 A6-A5=53
but will need an IF statement adding if there are any blank cells between the numbers.
In the particular case of your updated question where there are fewer numbers in column D than column C, the formula would be
=arrayformula({"Special Case";"";offset($D$1,1,0,count(D:D))+offset($C$1,2,0,count(D:D))})
But in the general case of there being blank cells anywhere, you would have to test everything
=arrayformula({"General Case";"";if(offset($D$1,1,0,rows(C:C)-2)="","",if(offset($C$1,2,0,Rows(C:C)-2)="","",offset($D$1,1,0,rows(C:C)-2)+offset($C$1,2,0,Rows(C:C)-2)))})

How to convert datetime interval to hours?

I have this spreadsheet:
A B C D E
1 08/13/2013 02:10 4
2 08/13/2013 02:19 10 00:09:00 160
In D2, I have this formula : =if(B2="";"";to_date(concatenate(A2;" ";B2))-to_date(concatenate(A1;" ";B1)))
In E2 I have this formula : =if(D2="";"";(C2-C1)/D2)
But E2 outputs the wrong result, 160. I want it to be 40 (=10-4/0.15). 0.15 is the value in D2 converted to hours.
How can I do that?
Do not quite understand what you're trying to calculate, but with the information that explains, the following formula can help:
=IF(D2 = "", "", ROUND((C2-C1)/(D2*24), 0))

Resources