Concatenating from table to string, sum amounts - google-sheets

Consider the Google Sheets table below:
A B C D
1 category subcategory company amount
2 health care diagnostics AA 100
3 health care diagnostics AB 50
4 materials mining BA 75
5 financials banks CA 30
6 financials insurers CB 35
7 financials banks CC 10
8 financials banks CD 40
9 financials hedge fund CE 5
10 health care equipment DA 50
I would like to list, per subcategory, the companies in it, and the amount spent:
A B C
1 category companies amount
2 health care AA AB DA 200 <--- 100 + 50 + 50
3 materials BA 75
4 financials CA CB CC CD CE 120
Column A I will type myself / hardcode. But what formula in column B and C will give this result, depending on the value in A?

try:
=ARRAYFORMULA(IFNA(VLOOKUP(E3:E, QUERY({QUERY({A:D},
"select Col1,sum(Col4)
where Col1 is not null
group by Col1
label sum(Col4)'amount'", 1), {"companies";
TRIM(FLATTEN(QUERY(TRANSPOSE(QUERY(QUERY({A:D, C:C},
"select max(Col3)
where Col1 is not null
group by Col1
pivot Col5"),
"offset 1", 0)),,9^9)))}},
"select Col1,Col3,Col2", 1), {2,3}, 0)))
where E3:E are your hardcoded values

Related

How do I sum the cells of one column based on unique duplicate rows of other columns?

I need column D summed wherever column A-C are identical. Column E is what I want the output to look like. I am only using google sheet functions right now and have not learned how to write script. This formula is the closest I've gotten.
=SUM(filter(D:D;COUNTIF(A2:A&B:B&C:C;A2:A&B:B&C:C)>1))
However, it does not distinguish between different text strings only sums any duplicate.
Thanks for any help!
A
B
C
D
E
papaya
10/10/2022
500
42
42
papaya
15/12/2022
550
30
59
papaya
15/12/2022
550
29
59
Pineapple
16/11/2022
400
55
55
Pineapple
09/11/2022
400
63
78
Pineapple
09/11/2022
400
15
78
use:
=QUERY(A:E; "select A,B,C,sum(D) where D is not null group by A,B,C label sum(D)''")
update
use in M2:
=INDEX(LAMBDA(bc; g; i; IFERROR(g/VLOOKUP(bc; QUERY({bc\i*1};
"select Col1,sum(Col2) where Col2 > 0 group by Col1 label sum(Col2)''"); 2; )))
(B2:B&C2:C; G2:G; I2:I))

Add cell on different sheet when the adjacent is equal to another cell

I have 2 sheet2 on the same file:
the first collect the answers of different trial judge and the second should make the total
Like this:
First sheet
Name q1 q2 q3 total judge_id
Bob 1 5 8 14 1
Jeff 2 4 2 8 1
Bob 3 1 4 8 2
Bob 5 3 2 10 3
Jeff 6 1 8 15 3
Second sheet
judge_id 1 2 3 tot
Bob 14 8 15 37
Jeff 8 # 15 23
How can I sum only the row on a 'person' in particolar?
There is a fast way to do it without open Google Script?
try:
=ARRAYFORMULA({QUERY({A2:F},
"select Col1,sum(Col5) where Col1 is not null group by Col1
pivot Col6 label Col1'judge_id'"), {"tot"; MMULT(QUERY(QUERY({A2:F},
"select sum(Col5) where Col1 is not null group by Col1 pivot Col6"),
"offset 1", )*1, SEQUENCE(COUNTUNIQUE(F2:F), 1, 1, ))}})
Here's my take on it:
={
QUERY(A1:F,"SELECT A, SUM(E) WHERE A IS NOT NULL GROUP BY A PIVOT F LABEL A 'Judge ID'"),
QUERY(A1:F,"SELECT SUM(E) WHERE A IS NOT NULL GROUP BY A")
}

Concatenating from a table by filtering first

Consider the table below:
A B C D
1 category subcategory company amount
2 health care diagnostics AA 100
3 health care diagnostics AB 50
4 materials mining BA 75
5 financials banks CA 30
6 financials insurers CB 35
7 financials banks CC 10
8 financials banks CD 40
9 financials hedge fund CE 5
10 health care equipment DA 50
I want to (1) concatenate the companies for each category and (2) return the aggregated amount.
A B C
1 category companies amount
2 health care AA AB DA 200 <--- 100 + 50 + 50
3 materials BA 75
4 financials CA CB CC CD CE 120
I am aware that (2) can easily be done by using a pivot table, but (1) is not supported by pivots. So given the excel below, what formula do I need in B1 and C1 to get the desired output?
A B C
1 category
2 health care
3 materials
4 financials
I have looked into concatenate(), which is needed here, but I don't see how I can give arguments to that function, like pseudo:
=concatenate(C2:C10; "where (A:A) = 'health care')
Who can help me out?
=ARRAYFORMULA(QUERY({QUERY({A:D},
"select Col1,sum(Col4)
where Col1 is not null
group by Col1
label sum(Col4)'amount'", 1), {"companies";
TRIM(FLATTEN(QUERY(TRANSPOSE(QUERY(QUERY({A:D, C:C},
"select max(Col3)
where Col1 is not null
group by Col1
pivot Col5"),
"offset 1", 0)),,9^9)))}},
"select Col1,Col3,Col2", 1))

Get Max value from range (multiple sheets) grouped by Name

I have 3 sheets that have the exact same format
Sheet1
A B C D
George 10 2 8
Nick 15 89 0
Mike 13 1 50
Lucas 9 -5 12
Sheet2
A B C D
Nick 1 9 5
Mike 1 10 6
George 11 22 5
Lucas 10 5 2
Panos 55 0 1
Sheet3
A B C D
Panos 0 9 1
George 1 2 5
Nick 7 2 1
Lucas 1 5 1
I want to query the range {'Sheet1'!A1:D5; 'Sheet2'!A1:D5; 'Sheet3'!A1:D5}
And get something like MAX(Col2:Col4) Group By Col1
Which would return something like:
George 22
Nick 89
Mike 50
Lucas 12
Panos 55
I tried:
=sort(query({'Sheet1'!A1:D5; 'Sheet2'!A1:D5;'Sheet3'!A1:D5}, "select Col1, MAX(Col2:Col4) Group by Col1 Label MAX(Col2:Col4) '' " ),2, FALSE)
and
=sort(query({'Sheet1'!A1:D5; 'Sheet2'!A1:D5;'Sheet3'!A1:D5}, "select Col1, MAX(MAX(Col2),MAX(Col3), MAX(Col4)) Group by Col1 " ),2, FALSE)
Both didn't work. Any ideas?
Please try:
=query(sort(transpose(query({Sheet1!A1:D5;Sheet2!A1:D5;Sheet3!A1:D5},"select max(Col2), max(Col3), max(Col4) pivot Col1"))),"select Col1, max(Col2) group by Col1 label(Col1) ''")
To sum up your question, It requires finding the MAX across the columns to the right as well as down. As such, QUERY does NOT have such 2D function.
So, Use a Helper column E&F in each sheet:
Max of B&C:
E2:
=ARRAYFORMULA(IF(B2:B>C2:C,B2:B,C2:C))
Max of B,C&D:
F2:
=ARRAYFORMULA(IF(D2:D>E2:E,D2:D,E2:E))
Now, Use Query:
Query:
=ARRAYFORMULA(QUERY({Sheet1!A2:F;Sheet2!A2:F;Sheet3!A2:F}, "Select Col1,max(Col5) where Col1 is not null group by Col1 order by max(Col5) desc"))
Notes:
Change ranges to suit
You could also simply use MAX for each row without the ARRAYFORMULA
Theoretically, For a single cell solution, You could enter this formula to find the max of 3 real numbers
Another approach perhaps a bit simpler but needing two queries
=sort(unique(({Sheet1!A1:A5;Sheet2!A1:A5;Sheet3!A1:A5})))
to get the names starting in (say) F2
Then this to get the maximum values for each name in (say) G2 and pulled down
max(query({Sheet1!A$1:D$5;Sheet2!A$1:D$5;Sheet3!A$1:D$5},"select max(Col2),max(Col3),max(Col4) where Col1='"&F2&"'"))

Google Sheets filter formula get specific columns, plus between two dates

I am building somewhat of an invoice report tool where I would like to pull all of Client X's orders between the dates set in A1 and A2, but I only need 6 of the 35 columns.
So far I am able to use the =filter formula with ease but I get all the columns of course, and I have no idea how to only filter based on the dates in A1 and A2.
If my sample data is below, I want to be able to pull all records for Client 1, between the dates set in A1 and A2 and only show Col1, Col3, and Col 6 in my "report"
Row Col1 Col2 Col3 Col4 Col5 Col6
1 Client 1 $45.00 01/15/2016 123 Main Street 404-989-9999 John
2 Client 1 $75.00 01/17/2016 123 Main Street 404-989-9999 John
3 Client 3 $15.00 01/18/2016 456 Park Street 404-989-9999 Sue
4 Client 4 $35.00 01/18/2016 111 East Street 404-989-9999 Chris
5 Client 5 $95.00 01/19/2016 789 North Street 404-989-9999 Tim
6 Client 3 $65.00 01/20/2016 456 Park Street 404-989-9999 Sue
7 Client 3 $25.00 01/21/2016 456 Park Street 404-989-9999 Sue
8 Client 1 $45.00 01/22/2016 123 Main Street 404-989-9999 John
One way to achieve that would be the use of query()
=query(A2:F, "select A, C, F where A='Client 1' and C >= date'"&text(A1, "yyyy-mm-dd")&"' and C <= date '"&text(B1, "yyyy-mm-dd")&"' " ,0)
Change ranges to suit.
Another way (with filter) would look something like this:
=filter({A2:A, C2:C, F2:F}, A2:A="Client 1", C2:C >= A1, C2:C <= B1)
I hope that helps ?

Resources