SUM of comma separated numbers for each unique value - google-sheets

I'm trying to return the SUM of a set of numbers, which are comma seperated in another cell.
I'm listing out the unique values in column 1, and trying to SUM the numbers in column 2.
Client A 0,56.3,0,450,90,22.6,22.6,0,180,0,90,67.6,67.6,67.6,90,225,450
The expected outcome is 1879.3 for Client A.
But how can I do this for every new client and set of numbers added? I have tried a combination of SUM and SPLIT, but I can't get it work for every value in column A in an arrayformula.
Here is the file: https://docs.google.com/spreadsheets/d/1k8o_Ft59R44yQKOSY99AuAk8HDGTcRyiw2N9umONv3U/edit?usp=sharing
Thanks!

You could try:
=INDEX(QUERY(SPLIT(FLATTEN(A1:A&"|"&SPLIT(B1:B,",")),"|"),"Select Col1, Sum(Col2) where Col1 is not null and Col2 is not null group by Col1 label Col1 'Client', Sum(Col2) 'Total'"))
I've put the formula in F1 in your spreadsheet:

Related

SUMPRODUCT of last nth values with criteria

I have two columns (see screenshot)
How can i create a formula that sum the second LATEST column values with a criteria from column A?
For example i need the sum of the last 6 values (from column B) of the cells (in column A) that start with HH, so values starting from the bottom.
I know how to make a sum of all values (from column B) containing HH (from column A)
=SUMIF(A1:A;"HH"&"*";B1:B)
P.S. HH and * are separate because i'll substitute the HH with a cell
but now i need to delimit this to the last N values (let say last 3 values)
P.P.S.
=SUMPRODUCT((COUNTIFS(A1:A;"exact text";ROW(A1:A)*{1;1};">="&ROW(A1:A)*{1;1})<=3)*(A1:A="exact text");B1:B)
This works so far ONLY if i write the exact text, not with values like HH*
Maybe try
=sum(index(query({row(A1:A), A1:B}, "Select Col3 where Col2 contains 'HH' order by Col1 desc limit 6")))
and see if that works?
Note:
*the string HH can be also be in a cell (ex. D1)
=sum(index(query({row(A1:A), A1:B}, "Select Col3 where Col2 contains '"&D1&"' order by Col1 desc limit 6")))
*6 indicates the number of values you want to sum
EDIT: For your locale you'll need to use in G1
=sum(index(query({row($B$1:$B) \ $B$1:$C}; "Select Col3 where Col2 contains '"&E2&"' order by Col1 desc limit 3")))
and fill down. See if that works?
This should also work, not sure if it's any easier to understand/ less complicated than any other approach:
=SUM(SORTN(REGEXMATCH(B:B;E2)*C:C;3;0;ROW(B:B)*REGEXMATCH(B:B;E2);0))
Note the number 3 for the number of values you want from the bottom. and the reference to E2, which is "HH" as on your sample sheet.
use:
=QUERY(FILTER({IFNA(REGEXEXTRACT(SORT(B2:B; ROW(B2:B); 0);
"^([A-Za-z]{1,3})\d"))\SORT(C2:C; ROW(B2:B); 0)}; COUNTIFS(
REGEXEXTRACT(SORT(B2:B; ROW(B2:B); 0); "^([A-Za-z]{1,3})\d");
REGEXEXTRACT(SORT(B2:B; ROW(B2:B); 0); "^([A-Za-z]{1,3})\d");
ROW(H2:H43); "<="&ROW(H2:H43))<=3);
"select Col1,sum(Col2) group by Col1 label sum(Col2)''")
full explanation here

How to find the most frequent text value in a whole spreadsheet on Google sheets?

I know the code to find the most frequent value in a single column, but I'm trying to figure out the code that lets me find the most frequent value in multiple specific columns.
For example:
=ARRAYFORMULA(INDEX(A2:A17,MATCH(MAX(COUNTIF(A2:A17,A2:A17)),COUNTIF(A2:A17,A2:A17),0)))
^^this formula only allows me to have two arguments for COUNTIF
=INDEX(A2:A6,MODE(MATCH(A2:A6,A2:A6,0)))
^^this one only allows 4 arguments max in the index
I want an equation that'll allow me to find the most frequent text value for 16 specific columns.
Is that even possible?
BETTER FORMULA
You asked for formula when you have 2 top values with the same frequency
=SORTN(QUERY(FLATTEN(H2:K),
"select Col1, count(Col1)
where Col1 is not null group by Col1
order by count(Col1) desc label count(Col1) '' ",0),1,1,2,0)
First answer
Use only 1 QUERY formula
For top value
=QUERY(FLATTEN(A2:D),
"select Col1, count(Col1)
where Col1 is not null group by Col1
order by count(Col1) desc limit 1",0)
For all values
=QUERY(FLATTEN(A2:D),
"select Col1, count(Col1)
where Col1 is not null group by Col1
order by count(Col1) desc limit 1",0)
I show it in 2 steps then combine steps together in one formula:
First you flatten table and remove duplicates to get a column of values you check frequency for:
unique(flatten(B2:I25))
Then for each one you check number of occurences:
ArrayFormula(countif($B$2:$I$25,unique(flatten(B2:I25))))
Then you have to filter new table and get only rows with highest values:
=filter({first , second column};second column = max(second column)
Using earlier values:
=filter({unique(flatten(B2:I25)),ArrayFormula(countif($B$2:$I$25,unique(flatten(B2:I25))))},ArrayFormula(countif($B$2:$I$25,unique(flatten(B2:I25))))=max(ArrayFormula(countif($B$2:$I$25,unique(flatten(B2:I25))))))
My solution is available here:
https://docs.google.com/spreadsheets/d/1gMZDFWOY8qbA1Bhp8rZ_por3Skb0i_qQtlaSlCTKXgM/copy
If your columns are scattered around your sheet, you should use { } and make a table of them.

Split each row in a column whose value contains commas into transposed rows

So I have data in the following format:
I want to summarize this into another sheet like this:
So I can use TRANSPOSE(SPLIT(A1:A)) to do this individually
I can use ARRAYFORMULA(SPLIT(A:A)) to run this over a range, however I cant seem to use both of them together to the same effect
To summarize
Column A contains comma seperated values for car names
I want to get all the car names as rows in another sheet and add counts against them
You can get the whole mini-report with this one formula:
=ArrayFormula(QUERY(FLATTEN(TRIM(SPLIT(A2:A,","))),"Select Col1, COUNT(Col1) Where Col1 > '#' GROUP BY Col1 LABEL COUNT(Col1) ''"))
SPLIT will split every entry in A2:A at the comma (if there is one) and will result in an error for every blank row (dealt with later).
TRIM removes the spaces after any comma-splits.
FLATTEN creates one column from all results (words, blanks and errors).
QUERY(...,"Select Col1, COUNT(Col1) Where Col1 > '#' GROUP BY Col1 LABEL COUNT(Col1) ''") starts with everything in that FLATTEN column and a COUNT of each GROUPed by the name ruling out anything WHERE the value in Col1 is not > the '#' symbol (which will rule out all blanks and errors). Finally, LABEL will get rid of the superfluous header of "count" that would otherwise be generated for the COUNT(Col1) column of the QUERY.
Alternatively, you could rule out the errors first with a FILTER and then use the WHERE part of the QUERY to rule out blanks:
=ArrayFormula(QUERY(FLATTEN(TRIM(SPLIT(FILTER(A2:A,A2:A<>""),","))),"Select Col1, COUNT(Col1) WHERE Col1 Is Not Null GROUP BY Col1 LABEL COUNT(Col1) ''"))
Managed to solve it from this question
https://webapps.stackexchange.com/questions/114791/google-sheets-split-and-unique
=unique(transpose(arrayformula(trim(split(join(",",A1:A),",")))))
The most efficient is probably QUERY. Unlike JOIN, this has no limit on the number of characters in the result.
Basic approach
=ARRAYFORMULA(TRANSPOSE(SPLIT(
QUERY(A:A&",",,9^9),
", "
)))
Unique
=ARRAYFORMULA(UNIQUE(TRANSPOSE(SPLIT(
QUERY(A:A&",",,9^9),
", "
))))
Counting the number in a group
=ARRAYFORMULA(QUERY(
TRANSPOSE(SPLIT(QUERY(A:A&",",,9^9),", ")),
"select Col1, count(Col1) group by Col1 label count(Col1)''"
))

Google Sheets - How to COUNTIF multiple values in each one cell of column

Each cell has multiple values that separated by comma and I want to count a value.
Let say I want to count Mapping Your Future. How to do that?
delete everything you got and use:
=INDEX(QUERY(TRIM(FLATTEN(SPLIT(TEXTJOIN(",", 1, questionnaire!G:G), ","))),
"select Col1,count(Col1)
where Col1 is not null
group by Col1
label count(Col1)''"))

How do I find the second MODE in google spreadhseet

I have a list of numbers and I am using =MODE to find the number which appears most often, my question is how do I find the second most often occurring number in the same list?
In Google Sheets, you can use the QUERY function to retrieve this sort of information (and much more) quite easily. Assuming your data is numerical values only in column A with no header:
=QUERY({A:A,A:A},"select Col1, count(Col2) where Col1 is not null group by Col1 order by count(Col2) desc",0)
will return a list of the items in column A, and their associated frequencies, sorted from highest to lowest. Note: if column A contains text strings, you need to use where Col1 != '' rather than where Col1 is not null.
Now you can use INDEX to retrieve the exact value you require; so to retrieve the second most frequent value, you need the third value in the first column (as QUERY will populate a header row in the output):
=INDEX(QUERY({A:A,A:A},"select Col1, count(Col2) where Col1 is not null group by Col1 order by count(Col2) desc",0),3,1)

Resources