Find last non-zero cell's column reference in an UNORDERED range of values - excel

I have a range with numbers in a row in Excel, say:
A B C D E F G H I J K L
Line 1 => 0 0 0 3 2 8 3 6 0 0 0 0
All cells are non-blank but some contain zeros and some contain numbers. Also, the range cannot be ordered in either ascending or descending order.
I need a formula (not VBA) that will return the column of the last non-zero value in the range (i.e. column 8 for cell H1 above). I can get the actual value of cell H1 with this formula:
LOOKUP(2,1/(A1:A10<>0),A1:A10)
but I cannot find a way to get the column number for that cell.
Any ideas?

You are very close:
=LOOKUP(2,1/(A1:L1<>0),COLUMN(A1:L1))

Enter
=MAX(IF(YourRange=0,0,COLUMN(YourRange)))
as an array formula (CTRL-SHIFT-ENTER).

For me, the best solution for numeric values was this:
=IF(D497<>0,D497,IF(D496<>0,D496,IF(D495<>0,D495,IF(D494<>0,D494,IF(D493<>0,D493,D492)))))
it gives the last non zero value, for your require range that you can make by adding further rows to the formula.

Related

Display the column number of the last non-empty cell within a range (if values are not unique)

I have the following Excel spreadsheet:
A B C D E F G H I J K L
1
2
3
4 600 150 80 600 0 0 4
5 200 150 80 80 0 0 4
6
7
In Range K4:K5 I currently use the formula from this question to identify the last non-empty cell within the range and get the the column number of it back:
K4 = LOOKUP(2,1/(D4:J4<>0),COLUMN(D4:J4)-MIN(COLUMN(D4:J4))+1)
K5 = LOOKUP(2,1/(D5:J5<>0),COLUMN(D5:J5)-MIN(COLUMN(D5:J5))+1)
This formula works in the simple example above. However, once I use this formula in a bigger spreadsheet I get a lot of performance issues and numbers are not always updated correctly.
Therefore, I am wondering if there is alternative formula to get the column number of the last non-empty cell no matter if the values are unique or not?
As already described in the comments below the question the following solutions are available:
Option A)
If you want to get column number of the last non-empty cell:
=AGGREGATE(14,7,(COLUMN(D4:J4)-MIN(COLUMN(D4:J4))+1)/(D4:J4>0),1)
Option B)
If you want go tet the value in the last non-empty cell:
=INDEX(D4:J4,AGGREGATE(14,7,(COLUMN(D4:J4)-MIN(COLUMN(D4:J4))+1)/(D4:J4>0),1)
=INDEX(A4:J4,AGGREGATE(14,7,COLUMN(D4:J4)/(D4:J4>0),1)

How to select certain rows in Excel that meet logical criteria of A & B

I have an excel sheet in CSV that has 8 columns A-H and thousands of rows with values 0 or 1 depending on truth value.
I'm looking for the Excel function in which I can select rows where column A and B are true so that I can check another columns probability given A&B. IE P((A&B)|D) (where | means given).
I'm really new to excel and having difficulties finding how to only select rows that meet this criteria.
The following formula entered in I1 will return a 1 if both A1 and B1 are true.
=IF(AND($A1=1,$B1=1),1,0)
Copy it down or autofill to identify all rows where A and B are true.
The $ sign before A and B make the column references absolute meaning if you drag the formula to the right, the references to columns A and B will remain.
Because Excel implicitly interprets 0 = FALSE and 1 (or any other number) = TRUE the formula could be shortened to:
=IF(AND($A1,$B1),1,0)
The probability of C being 1 given that A and B are 1 can be calculated by counting all rows where A, B and C are all 1 and dividing by the number of rows where both A and B are 1:
=COUNTIFS($A:$A,"1",$B:$B,"1",C:C,"1")/COUNTIFS($A:$A,"1",$B:$B,"1")
Again, references to A and B are absolute, while C is relative so you can drag right to get probabilities for columns D to H.
COUNTIFS only counts the rows where all of the criteria are met and allows you to specify up to 127 range/criteria pairs.
EDIT
You could also use:
=AVERAGEIFS(C:C,$A:$A,1,$B:$B,1)
to get the probability.

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.

Alternating Columns on a Array Formula in Excel

I have a spreadsheet where data is spread in alternated columns. Columns A, C and E are flags indicating if the adjacent column has a valid data.
It is like this:
A B C D E F
1 1 32 0 67 1 34
The goal is to sum values where its left adjacent is 1. In this example, the sum should be 66, as A and E are both 1 and C is 0.
I can get an array with 1's and 0's indicating if a flag column is set or not:
=MOD(COLUMN(A1:F1),2)*A1:F1
And that gives me
{1, 0, 0, 0, 1, 0}
The thing is that I don't know what I can do from here. If I could slide all the data (by inserting a 0 at the beginning and removing the 0 at the last position), I could SUMPRODUCT it and get the result.
By the way, I can't use macros...
Ideas?
Notice that Formula bellow has two cell ranges one starts at column A the other at B.
=SUMIF(A1:F1,1,B1:G1)
If you are not famliar with SUMIF then what you need to know about this is that first term A1:F1 is where formula checks values for a condition. What condition you might ask values that equal to seconds term in this case =1. Lastly last term has the values that need to be sumed.
Also since you may have issuse of having 1 in an Even column( where you'd expect value not you condition), heve is a formula that makes sure that your 1 & 0 condition is in correct Column:
=SUM(IF((A1:F1=1)*ISODD(COLUMN(A1:F1)),B1:G1,0))
Simple version:
=SUM(A1*B1,C1*D1,E1*F1)
As 0 multiplied by anything is always zero then this only sums the columns preceded by a 1.

How to format rows to color group by like values in column 1

I have a worksheet that has information like this:
a
a
b
c
c
c
How do I format it so that all of the rows that have a value of a in the first column are one color, then all the rows that have a value of b in the first column are a different color, etc. ?
Edit not from OP to add clarification from comment:
Everything is already sorted alphabetically, and will stay that way, and I want multiple colors.
Create a helper column with a formula like this;
=MOD(IF(A3=A2,0,1)+B2,2)
In this example column A is the column of sorted values to be grouped by, and column B is the helper column. The formula is entered on row 3. Set the first row of the helper column to the value 0 and the others to the formula. This will result in alternating values in the helper column for each group, ie;
a 0
a 0
b 1
c 0
c 0
c 0
d 1
d 1
e 0
You can then set conditional formatting based on the column value. If the value is 1 then highlight the row; if it is 0 do not highlight it. Or use alternating colors or whatever. You can reference any of the articles on the web that describe how to conditional format the entire row based on the value in the column.
IF(A3=A2,0,1) compares the current row (3) and prior row (2) returning a 1 or 0.
MOD( [...] +B2,2) accomplishes the alternation between 0 and 1 when the grouping column value changes.
I think you need a helper column, say B seeded with 1 in row1, and =IF(A1=A2,B1,B1+1) in B2 and copied down to suit. Then formulae of the kind below should suit for conditional formatting:

Resources