How to use Countif function to count rows that has a specific value in either of column A or B in Google sheet? - google-sheets

I am trying to count the number of rows that has a specific value let say "John" in either Column C or Column D. If the two columns have both the same value, then only one of them is counted.
Here is the example sheet. column C & D has the data.
Column G contains the function countif
https://docs.google.com/spreadsheets/d/1i9I2bhtlHAMWqVqdE7hbkgLRLCCUrfnOMSQcf9Gj4_0/edit#gid=0

Try:
=ArrayFormula(COUNTIF(C5:D12,F5:F7)-COUNTIFS(C5:C12,F5:F7,D5:D12,F5:F7))

Related

Count of items in column A not in column B (Google sheets)

I have two columns of numbers in Google sheets. I’m trying to find a formula to give me a count of items in column A that are not in column B. The numbers in the columns are descending and unique in each column but can be duplicated across columns. The columns can also have different amounts of items in them.
Column A has 5 4 3 1
Column B has 4 2 1
The answer is this case would be 2 as the numbers 5 and 3 in column A are not in column B.
I’ve tried using sum, if and countif but can’t come up with a solution. Also not sure if this would be an array formula or not.
Without a helper column you can use reduce and lambda functions.
=reduce(0, A:A, LAMBDA(prev, a_value, prev + if(not(a_value = ""), iferror(min(0, match(a_value, B:B, false)), 1), 0)))
Fill column c with this formula:
=iferror(min(0,match(A1,B:B,false)),1)
If A1 is in column b the match function will return an value, which is then reduced to 0 by the min function. Otherwise match will return an error, and the iferror will return 1.
Then you just need to sum column c.

Count Values under Columns with Same Name Google Sheets

I have a Google sheet which has columns with the same name and there are different values under each column. I want to count the same value that appear under the same column name.
1
2
3
1
2
R
B
C
R
D
D
C
R
B
D
For example, I would like to get the number "R" that appear under column "1", so I would expect a count of 2 for "R" appearing under columns 1.
Here is a link to Google Sheet with actual data.
I have tried countif and countifs in Google Sheets, but can't figure out how to get the count right based on column name.
Try this formula, it outputs an array which shows how many of each letters are contained in each column name:
=LAMBDA(NUMBERS,LETTERS,
LAMBDA(UNUM,ULET,
{
{"",TRANSPOSE(UNUM)};
{ULET,
MAKEARRAY(COUNTA(ULET),COUNTA(UNUM),LAMBDA(ROW,COL,
COUNTIF(FILTER(LETTERS,NUMBERS=INDEX(UNUM,COL)),INDEX(ULET,ROW))
))
}
}
)(UNIQUE(FLATTEN(NUMBERS)),UNIQUE(FLATTEN(LETTERS)))
)($A$1:$AE$1,$A$2:$AE$18)
Assume that your sample datarange is A1:AE18.
apply UNIQUE() and FLATTEN() to A1:AE1, to get the unique entries of column names.
apply UNIQUE() and FLATTEN() to A2:AE18, to get the unique entries of data.
use LAMBDA() to name the dataranges and output of step 1 & 2 as:
NUMBERS (=A1:AE1),
LETTERS (=A2:AE18),
UNUM (=UNIQUE(FLATTEN(NUMBERS))),
ULET (=UNIQUE(FLATTEN(LETTERS))).
create Arrays with {}, which...
1st column's value is a blank, followed by TRANSPOSE(UNUM) in the row,
1st row's value is a blank, followed by ULET in the column.
inside the above said range, use MAKEARRAY() to create results.
MAKEARRAY() set an array by defining the length of ROW and COL, which we uses...
COUNTA(ULET) as the number of rows and,
COUNTA(UNUM) as the number of columns.
inside MAKEARRAY(), you also need a LAMBDA() to apply what to do with each CELL of the new created array, each CELL is accessed by the ROW and COL index.
in our case, we set up the row and col number of the new array using ULET and UNUM. Therefor, the index of each CELL of the new array will be equal to the index of each value inside ULET and UNUM, we can than take that as reference and use COUNTIF() with FILTER() to calculate the number of repeats of each letter in each column name.
You can try this:
= ARRAYFORMULA(
query(
query(
SPLIT(TRANSPOSE(SPLIT(
QUERY(
TRANSPOSE(
QUERY(
TRANSPOSE(
IF(Original!A2:AE18<>"",
"😊"&Original!A1:AE1&"♥"&Original!A2:AE18, )
),,999^99)
),,999^99),
"😊")),
"♥"),
"Select Col1,Col2,count(Col2) group by Col1,Col2"),
"Select max(Col2),Col3 group by Col2,Col3 pivot Col1")
)
Note: (Got inspired by player0 useful answers)
Output:
We can read from the table that: R is appearing 40 times under the column named '1', 24 times under the colum named '2', etc...
The following is a more compact approach than the previous answers:
=arrayformula(query(split(flatten(A1:AE1&"|"&A2:AE18),"|"),"select Col2,count(Col2) group by Col2 pivot Col1"))
N.B. I'm assuming the order of the grouped values in each column is irrelevant, so the QUERY default of lexicographical ordering is fine.

Grouping Data with SUM of Value No Greater than a Specific Number (Google Sheets)

I have series of numbers as you can see in the following table in a Google Spreadsheet:
I want each name to form a group with other names next to it with sum of value no more than 26. If a sum is greater than 26, then it will be just forced to form to another group with another name.
So that it might look like this:
In this example, A and B can form a group together since the total sum of A and B is no more than 26. C is forced to form a group just by itself since if we add D with C, the sum will be more than 26. D is also forced to form a group just by itself since if we add D with E, the sum will be more than 26.
How can I create a table with Array Formula so it looks like that?
This is the link for the sample case:
https://docs.google.com/spreadsheets/d/1BXfJrfbtdsGP4fr9dsKxFq54nM4PnKLHauAOy5UAPbY/edit#gid=0
The sample case is in Sheet2
Column A and B are given condition
Column D to G are the desired outcome
Edited: sample case is changed from Sheet1 to Sheet2 so that the problem can be seen more clearly. Sheet1 is hidden to prevent confusion.
You can do it using two formulas:
for group in col G =ArrayFormula(IF(B2:B,INT(MMULT(TRANSPOSE((ROW(B2:B)<=TRANSPOSE(ROW(B2:B)))*B2:B),SIGN(B2:B))/26)+1,))
for group sum in col F =ArrayFormula(IF(LEN(B2:B),SUMIF(G2:G,G2:G,B2:B),))
or using one formula in col F which will be slower:
=ArrayFormula(IF(LEN(B2:B),{SUMIF(INT(MMULT(TRANSPOSE((ROW(B2:B)<=TRANSPOSE(ROW(B2:B)))*B2:B),SIGN(B2:B))/26)+1,INT(MMULT(TRANSPOSE((ROW(B2:B)<=TRANSPOSE(ROW(B2:B)))*B2:B),SIGN(B2:B))/26)+1,B2:B),INT(MMULT(TRANSPOSE((ROW(B2:B)<=TRANSPOSE(ROW(B2:B)))*B2:B),SIGN(B2:B))/26)+1},))

How to count rows of a particular column using formula in Google Sheet

I have a google sheet that contains case number column and MC column. How to count the row of case number based on the MC Column and automatically sum up in the number of cases column. This is the screenshot of my data:
try:
=QUERY(A2:C; "select C,count(C) where C is not null group by C")

How to Count Duplicate values based on other cell values in google sheet

How can I count Duplicates values based on other cell values look bellow
I have 3 duplicates in Column B but in Column C only 2 duplicates vales mention So I want to count remain duplicate Values in Column B which are not mentioned yet in Column C or in other words I want to count column B duplicates values which have an empty cell in Column C.
Look into Column D which get value 1 as we have only duplicate value in column B which have an empty cell in column C.
Hope you got my point.
try:
=ARRAYFORMULA(SUM(N(COUNTIFS(B2:B, B2:B, ROW(B2:B), "<="&ROW(B2:B))>1))-
COUNTIF(C2:C, "Duplicate"))
or:
=ARRAYFORMULA(SUM(N(FILTER(COUNTIFS(B2:B, B2:B, ROW(B2:B), "<="&ROW(B2:B))>1, C2:C=""))))

Resources