Excel counting taking into account adjacent cells (summing along a column, but looking across a row) - excel

I have a table that tells me whether a value is found in a source:
(image of excel cells)
Value
Source1
Source2
Source3
alpha
1
0
1
beta
0
1
1
gamma
1
0
0
delta
1
1
1
epsilon
0
1
0
zeta
0
1
0
What I'd like to do is count the number of times that each source uniquely finds a given value. For this example, there are:
one value unique to Source1 (gamma)
two values unique to Source2 (epsilon and zeta)
zero values unique to Source3
In practice, this calculation will be used on ~10 columns and 1000s of rows, so I need some formula help.
I've tried various combinations of sumifs, countifs, sumproducts, and array formulas, but I am stumped by the fact that the sum needs to look perpendicularly to the column.
Any help is much appreciated!

With Excel365 you can try below formula-
=SUM(--(MMULT($B$2:$D$7,SEQUENCE(COLUMNS($B$2:$D$2),,,0))*(B$2:B$7)=1))
For Non365 version of excel you try below array (CTRL+SHIFT+ENTER) formula. In this case you must enter same number of one 1 of source column.
=SUM(--(MMULT($B$2:$D$7,TRANSPOSE({1,1,1}))*(B$2:B$7)=1))

Related

Excel formula that returns specific lists of values?

Help is appreciated!
I have two columns of data. The first is a participant ID column. The second adjacent column are the binary coded respective responses (example below). I am interested in all of the 1's.
Does anyone know a formula which can extract a list of all of the participants who have scored a 1?
E.g., (Particpant ID is value on the left, and the participants' score is to the right).
1 0
2 1
3 1
4 0
5 0
6 1
FILTER() formula will do that. Try-
=FILTER(A2:A7,B2:B7=1)

How do I randomly distribute a set of numbers across a single column?

I have a set of continuous "1" values across column A Eg: 11111. I'd like to distribute this randomly across column B with spaces in between Eg: 1 1 1 1. Not really sure how to go about this. I'd like to do this for 600 values in column B. Any help is appreciated!
This formula could work:
=IF(ROUND(RAND(),0)=1,1,"")
It will generate a random number between 0 and 1, round it to the nearest integer (so 0 or #), return 1 if the result is 1 and nothing if it is 0
Populate as many cells as you need in the column you want
Edit:
To limit the number of 1s appearing use the following formula in cell B2 and copy down:
=IF(AND(ROUND(RAND(),0)=1,COUNTIF(B$1:B1,1)<6),1,"")
Where 6 is the maximum number of ones that will appear, you can change it to any integer or reference a cell that has the maximum number of 1s you want to appear

How to do a little math in the criteria_range of Countifs Functions (using OR in Countif)

The Excel File is like this
A B
1 0
0 1
1 1
0 1
0 0
1 0
I want to use Countifs function to count how many rows have at least one "1" in any columns, like
=Countifs(A:A+B:B,">=1")
or
=Countifs(or(A:A=1,B:B=1))
I know I can add a Column C, let Column C = Column A + B, and then just count Column C; or I can count the total rows and count rows with "0" in both columns, and then calculate Total Row - Both "0". But in real Scenario, I have more complicated situation, so I prefer not using these two solutions.
Use a SUMPRODUCT function to provide cyclic calculation.
=SUMPRODUCT(--((A1:A6)+(B1:B6)>=1))
SUMPRODUCT does not like trying to calculate text values and full column references slow it down so keep your ranges to a minimum. Using the INDEX function can help isolate a dynamic range of true numbers.
Another solution using array formula:
=SUM(IF(A1:A6=1,1,IF(B1:B6=1,1,0)))
Being an array formula, you'll have to enter this formula by pressing Ctrl+Shift+Enter together.
Use =COUNT(A:A)-COUNTIFS(A:A,0,B:B,0) to count both 0 columns and subtract it from the total rows:
Or you can use:
=COUNTIFS(A:A,1,B:B,1)+COUNTIFS(A:A,0,B:B,1)+COUNTIFS(A:A,1,B:B,0)
if it is not clear what it the total number of rows.

Generate a truth table in excel

I need to make a formula that gives you the truth table for a variable number of columns.
Example
The current recommended answer did not work for me. For a simpler method, I'd recommend the following formula:
=IF(MOD(FLOOR((ROW()-ROW(TopRight))/(2^(COLUMN(TopRight)-COLUMN())), 1),2)=0,0,1)
Where TopRight is the top right cell of the truth table.
For instance, if you're creating a truth table with 8 entries that starts in A3, replace TopRight with $H$3, then drag the formula across and down.
A basic explanation of what's going on: In truth tables, the rows alternate 1 or 0 every 2 ^ n number of rows, where n is the the number of columns that the given column is away from the rightmost column.
Replace the FirstCell with a static reference to the cell that contains the first 2^1 value e.g. $D$1 for a 4-bit table (16 values) and autofill to the rest of the grid (in the example A1:D16)
=IF(MOD(ROW()-ROW(FirstCell),POWER(2, ((COLUMN() - COLUMN(FirstCell)) * -1) + 1)) >= (POWER(2, ((COLUMN() - COLUMN(FirstCell)) * -1) + 1) / 2),1,0)
The logic behind this is:
If the current row modulus 2 power current column (* -1 as the first value is in the last column and + 1 because it starts from 0) is greater or equal to half of 2 power current column, put the value as 1, else put the value as 0.
The other answers might make Boole sad. This one aims to be more boolean.
You need to populate the first row (2) with 0's
For the LSB column (D) - Invert:
=NOT(D2)*1 (formula for cell D3, copied to D4:D17)
That will invert the value from the row above. The *1 numification is necessary to avoid seeing TRUE or FALSE
For all other columns - Add:
=XOR(AND(D2:$D2),C2)*1 (formula for cell C3, copied to all cells A3:C17)
For an ADD function, you want to XOR the value above in the column with the result of ANDing all the bits in all the columns to the right of it. (In other words: if all the bits to the right of the bit above are 1, then you should flip the value from the bit above. This ADD formula works for any number of columns.)
The AND range is referenced to one row up and one col right, to the $D LSB column, also one row up. So the $D anchor for the LSB column allows copying to any other column
Again, *1 is used for numification of the resulting TRUE/FALSE
Here's a Microsoft 365 one-liner:
=TRANSPOSE(LET(n,5,m,2^n,x,SEQUENCE(n,m,0),y,FLOOR(x/m,1),z,FLOOR((x-y*m)/2^(n-1-y),1),MOD(z,2)))
n is the number of columns needed, m then stores the length of each column.
The formula finds the row of x and stores the value in y, and then successively halves each row into the correct format, and outputs the result mod 2 to produce the truth table set of inputs.
TRANSPOSE is used because SEQUENCE places the numbers left-right, top-bottom.
0 0 0 0
0 0 0 1
0 0 1 0
0 0 1 1
0 1 0 0
.....
remember the numbers are only 0 or 1.
for column D: D2=1-D1
for column C: C2=IF(D1=1,1-D1,D1)
for column B: B2=IF((C1=1)*(D1=1),1-B1, B1)
.....
After did this, copy the numbers without formulas for your truth table to avoid Excel calculation.

How to count number of matches between two columns in Excel?

How to count number of matches between two columns in Excel?
For example if we have:
1 1
2 1
3 2
4 4
5 3
I need to get either a column like this:
1
0
0
1
0
and then I can sum the ones in there to get the count of matches.
Option 1: IF
=IF(A1=B1;1;0)
This formula will put 1 in the cell if A1 = B1, and 0 otherwise.
Option 2: COUNTIF
Write =A1=B1 in C1, etc., in the column cells. This will fill the C column with TRUE and FALSE values.
At the bottom of that column, add a cell =COUNTIF(C1:C100;TRUE) so that you count the cells between C1 and C100 which have value TRUE.
You may find the TechRepublic article Use COUNTIFS() to compare two data sets in Excel of use. See also Microsoft's documentation on countifs().

Resources