Alternating Columns on a Array Formula in Excel - 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.

Related

Excel #DIV/0! issue with calculating increase

I have the following formula in cell "D2"
=IF((C2-$B2)/$B2>0.2,1,0)
In short in column D I want to return a "1" if the value in Column C is 20% or more than the value in Column B or "0" if not.
For row 2 it works perfectly. However, for row 3 it returns a #DIV/0! due to column B being 0 but I would like it to return a "1" as the value in column C is more than 20% than the value in column B. For row 4 I would like to return a "0" as column C is not more than 20% than the value in column B.
Many thanks in advance, Alan.
It's a simple inequation. Multiply both parts by $B2 and you'll skip the division by 0 problem. Like this:
=IF((C2-$B2)>0.2*$B2,1,0)
Also, you can make it a little shorter taking off the IF function (and the extra brackets i previously left there to make it easier to compare):
=(C2-$B2>0.2*$B2)*1

EXCEL: Take the average of a column in row (n-5,n-1) if row n fulfills a condition

There are 50 rows. The first column (A), each row has has a ID number. They are not sorted in any way and should not be either and there are no duplicated ID numbers. When the ID is 0 then consider 4 rows before that row, take the mean value of those values in clumn C between those 4 rows. If it helps It is always the last row that has value ID = 0.
What I have done so far is that I can find the ROW number of the ID = 0 using the following formula:
ROW(INDEX(A3:A53,MATCH(TRUE,INDEX(A3:A53=0,),0)))
Now I don't know how to navigate 4 rows upwards and take the corrosponding values in Column C.
If you use an extra column (say column Z), then paste this code into that column starting on row 4 (NB it won't work properly for the first 3 rows because there are not 4 above to check).
=IF($A4 =0, AVERAGE($C1:$C4), "Not Relevant")
so put that code in Z4, then press ctrl+E to flash fill it downwards.
Hopefully, on each line where the ID is 0, there will be an average of that line and the previous 3 in column Z on that row.
Use:
=AVERAGE(INDEX(A3:A53,MATCH(TRUE,INDEX(A3:A53=0,),0)-5):INDEX(A3:A53,MATCH(TRUE,INDEX(A3:A53=0,),0)-1))

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.

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

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.

Using COUNTIFS for a series of values at once

Working a step higher then COUNTIFS, I appose a challenge to write a formula without VBA code. The basic data is combined from 1000s of rows with:
Column A: rows with values from 1 to 3
Column B: rows with values from 1 to 250.
For this purpose lets say, we are looking at all cells of value "1" in column A, that suit value "5" in column B. To find all matches, we'd use COUNTIFS command.
1 1
2 5
1 5
1 7
1 10
3 45
2 12
1 2
2 1
=COUNTIFS(A1:A9;1;B1:B9;5)
The answer here is 1.
Next thing, the "5" in column B belongs to a group, e.g. group from 1 to 9. What would the best way be, to count all the matches in this example, so that for all "1"'s in column A, we'd have to find all matches with values from 1 to 9 in column B?! In the upper example that would result in "4". The obvious solution is with a series of IF commands, but that's unefficient and it easy to make a mistake, that get's easily overseen.
=COUNTIFS(A1:A9;1;B1:B9;"<="&9)
Works only as the upper limit. If I give the third criteria range and condition as ">="&1 it does not work - returns 0.
Gasper
Where the data is in A1:B9, using a lookup table in D1:E10 with letters A-J in column D and numbers 0 to 9 in column E and the following formula in B11 referencing letters entered in A11 and A12:
=COUNTIFS(A1:A9,1,B1:B9,">="&VLOOKUP(A11,$D$1:$E$10,2,FALSE),B1:B9,"<="&VLOOKUP(A12,$D$1:$E$10,2,FALSE))
works, changing the letters in A11 and A12 gives the correct count according to what they correspond to in the looku in D1:E10.
When you say give third criteria range do you mean:
=COUNTIFS(A1:A9;1;B1:B9;"<="&9,B1:B9;">=1")
If so then try:
=COUNTIFS(A1:A9;1;B1:B9;AND("<="&9,;">=1"))
ie have two conditional ranges with the second range having both conditions combined with AND()
Maybe what you want(ed) is:
=COUNTIFS(A:A;1;B:B;">=1";B:B;"<=9")
Almost there. I noticed that three criteria ranges and conditions work only if I use "=" sign in a condition. As soon as I use
=COUNTIFS(A1:A9;1;B1:B9;"<="&9,B1:B9;">=1")
it returns 0. My goal is to eventualy replace the number in a condition with a VLOOKUP command, so the final equation should be smth like
=COUNTIFS(A1:A9;1;B1:B9;"<="&VLOOKUP(...),B1:B9;">=VLOOKUP(...)")
But the "<" and ">" signs mess with this. Still looking for a solution.
This is my entire line, if it offers any further indication. The AND() commands is at the end - and it still results in 0
=COUNTIFS(INDIRECT(CONCATENATE("baza!$";SUBSTITUTE(ADDRESS(1;MATCH("card_type_id";baza!$A$1:$AAA$1;0);4);"1";"");"$2:$";SUBSTITUTE(ADDRESS(1;MATCH("card_type_id";baza!$A$1:$AAA$1;0);4);"1";"");"$15000"));IF(C6="računska";1;0);INDIRECT(CONCATENATE("baza!$";SUBSTITUTE(ADDRESS(1;MATCH(IF($C$4="CC_SI_klasifikacija";"building_classification_id";0);baza!$A$1:$AAA$1;0);4);"1";"");"$2:$";SUBSTITUTE(ADDRESS(1;MATCH(IF($C$4="CC_SI_klasifikacija";"building_classification_id";0);baza!$A$1:$AAA$1;0);4);"1";"");"$15000"));AND("<="&VLOOKUP($C$5;$K$203:$N$223;4;FALSE);">="&VLOOKUP($C$5;$K$203:$N$223;3;FALSE)))

Resources