How can I find in Excel the last column where the value is different than in the prior column? - excel

I have many rows with measurements that progress from left to right with the rightmost measurement being the most recent one. Often the values become zero at some point and stay zero until the end.
I am trying to find the last column where a change happened. I tried a lot but could not figure it out. In the first example (row 2) this would be column V (=column with the heading 18) where the value is -1.60 and no further values are available. In the second example (row 3) this would be column P (heading 12) and so on. In order to find this last-change column I have three steps (columns A, B, C) and a check (column D) to see if there are indeed no more non-zero values afterwards. This works well in many cases but it does not work if there is a zero value and then non-zero values before it is all zero to the end (rows 6 and 7). As there are in my case many columns (up to 270) it should be a general formula.
I would love to attach the sample Excel but I think this is not possible here. I did however paste the data part (E1:W7) as a csv here example data as csv, hope that is ok. Here the four formulas for row 7 and columns A to D:
=COUNT(E7:W7)
=INDEX($E7:$W7,0,$A7)
=MATCH($B7,$E7:$W7,0)
=SUM(INDEX($E7:$W7,0,$C7+1):W7)

If you need the position (or corresponding column header) of the last value that has changed, you can use Aggregate:
=AGGREGATE(14,6,D$1:U$1/((D2:U2<>"")*(C2:T2<>D2:U2)),1)
or if you have Excel 365, you can use Filter:
=MAX(FILTER(D$1:U$1,(D2:U2<>"")*(D2:U2<>C2:T2)))
Then you can use Index to find the value as before:
=INDEX(C2:T2,A2)
or use #Ron Rosenfield's formula.

Try:
=LOOKUP(2,1/((B4:Z4<>C4:AA4)*NOT(ISBLANK(C4:AA4))),C4:AA4)
This formula should return the last change, if I've understood the logic of what you are doing.
If not giving expected results, post back with an example of the failed row and what you expect.
Note that two horizontal references are used, with one being offset from the other by one column.

Related

Climate: Calculate Consecutive Days of Rain

I have a data column rainfall, and a column is on that date;
I intend to extract Consecutive Days of Rain from it in a separate column.
With the formula below, I extracted Consecutive :
columnF=IF(B2>0,SUM(F1)+1,0)
But the result is in a column and I can not separate them like in attached picture (see desired result).
If we want to extract the continuity of rainfall in a separate column
How to do?
My result:
Desired result:
Following from Ted D's comment... a possible formula for the second helper column would be (in cell G2):
=IF(F2=0,0,MATCH(0,F3:F999,0)-1+F2)
This looks for the first 0 in the counter column (F) beneath our row (looking around 1000 rows down - that 999 can be changed as appropriate). We use that plus our current counter to give us the size of the block.
From there, we can make the columns with the dx1day etc. titles and populate them with the formula:
=IF($G2=VALUE(SUBSTITUTE(MID(H$1,3,999),"day","")),$F2,"")
This is parsing the number from dx1day at the top of the column (in this case H) (if you did it with just numbers and a custom format you could save that bit*). We then check if the current "block size" value (in column G) is the same as that in the header. If it is we copy the counter value (from column F), otherwise blank.
You can get away from this second helper column by replacing the $G2 with (IF($F2=0,0,MATCH(0,$F3:$F999,0)-1+$F2)) - but if makes it a bit more of a headache to understand (and debug :-)
*the way for this is to do custom format with the "Type" being "dx"0"day"... then the formula becomes =IF($G2=H$1,$F2,"") or =IF((IF($F2=0,0,MATCH(0,$F3:$F999,0)-1+$F2))=H$1,$F2,"") for the no-second-helper-column version

Why am I obtaining this strange result adding all the values in 2 Excel columns?

I am not into Excel and I have this problem trying to sum the values of 2 different column and put this result value into a cell.
So basically I have the D column containing 2 values (at the moment only 2 but will grows without a specific limit, I have to sum all the values in this column). These value are decimal values (in my example are: 0,3136322400 and 0,1000000000).
Then I have an I column containing the same type of value (at the moment only one but also the values in this column can grow without a specific limit...in my example at this time I have this value −0,335305)
Then I have the K3 cell where I have to put the sum of all the valus into the D column and all the values into the I column (following my example it will contain the result of this sum: 0,3136322400 + 0,1000000000 −0,335305.
Following a tutorial I tried to set this simple forumla in the K3 cell:
=SUM(A:I)
The problem is that in this cell now I am not obtaining the expected result (that is 0.07832724) but I am obtaining this value: 129236,1636322400.
It is very strange...I think that maybe it can depend by the fact that the D and the I column doesn't contain only number but both have a textual "heder" (that is the string "QUANTITY" for both the cells). So I think that maybe it is adding also the number conversion of this string (but I am absolutly not sure about this assertion).
So how can I handle this type of situation?
Can I do one of these 2 things:
1) Adding the column values starting from a specific starting cell in the column (for example: sum all the values under a cell without specify a down limit).
2) Exclude in some way the "header" cells from my sum so the textual values are not considered in my sum.
What could be a smart solution for my problem? How can I fix this issue?
The sum function can take several arguments.
=sum(d2:d10000, i2:I10,000, more columns )
This should remove the header from the calculation.
If you turn your data into an Excel Table (Insert > Table), you can use structured referencing to address a table column, excluding the header.
=SUM(Table1[This Header],Table1[That Header])
Then you don't need to reference whole columns. If you add new data to the table, the formula will take that into account.

Excel formula to lookup the last value in a column and return the value of the adjacent cell

I have the following formula to return the value of the last value in a column:
=LOOKUP(2,1/(D:D<>""),D:D)
What I need now is to return the value of the cell adjacent to it as well. (It will not necessarily be the last value in that column and the info in Column D could have duplicates.
If your data looks like this:
A 1
A 2
A 3
B 4
B 5
B 6
C 7
To get last value this will do the trick:
=INDIRECT("B"&COUNTA(A:A))
And to get last where value is A:
=INDIRECT("B"&MATCH("A",A1:A7,0)+COUNTIF(A1:A7,"A")-1)
Just use next column:
=LOOKUP(2,1/(D:D<>""),E:E)
Ok, So I have found an answer by playing around with array formulas.
The problem was that this is a stock control sheet where there are changes made at multiple times, each recorded in the next available row. There is always a date (Column E) but not necessarily a Supplier, as it might be stock moving out. When a Supplier delivers, the Supplier name is recorded in Column D. In D1 the last supplier is then shown with the following formula.
=LOOKUP(2,1/(D:D<>""),D:D)
I want to then see what date it was last received. The formula I found that works is as follows (Array Formula):
=INDEX(E:E,MAX(IF(D:D=D1,ROW(D:D)-ROW(INDEX(D:D,1,1))+1)))
This is generally how I do it:
=XMATCH(FALSE,ISBLANK(A:A),0,-1)
This is what each part does:
Parameter
Explanation
FALSE
Instructs Excel to find the first instance of FALSE that it finds
ISBLANK(A:A)
Takes in the column A:A and notionally assigns a value to every item in the column
0
Means we want an exact match. Probably not necessary to put in, but I think it's good practice anyway
-1
Instructs Excel to start the search at the bottom/right of the range and work up/left. If you change this to 1 (the default), Excel will begin the search at the top/left and work down/right
So, taken together, this will search from the bottom of the column A:A, until Excel finds the first cell that is not blank, and return that cell.
Also, yes, this equation can be changed to a row format (e.g. 1:1), and can take a smaller range (e.g. A1:A20), but it cannot take a 2-dimensional range (e.g. A1:B20).
As a practical matter, this approach is much faster than other approaches (and much faster than you'd think, given it's evaluating against every row/column in the range), and won't get fooled by columns that have empty spaces in them (like with a COUNTA style approach).

How to do a union of two columns and then sort them into a third column in Excel?

I'm having the following sorting problem in excel - I have two columns which are sorted and could potentially have some identical values. I want to take the union of the two columns (e.g. A = {x,y,z}, B = {a,b,c}; C = A U B = {a,b,c,x,y,z}) and then sort them (as seen in the prior example).
Here's how it should look like in excel -
I haven't seen anything on Google or Stack Overflow that has a solution for this problem. What formula can I use to get the above result?
In the first row put
=SMALL($A$1:$B$10,ROW(1:1))
then copy down.
Non duplicate solution to be added
With this solution, the numbers need to start in the second row. If that is an issue, then slightly modify the first row. Also, this only assumes at max two duplicates of a number (one in each column).
=IFERROR(IF(SMALL($A$2:$B$11,ROW(2:2))=D1,SMALL($A$2:$B$11,ROW(2:2)+1),SMALL($A$2:$B$11,ROW(2:2))),"End of Numbers")
What this does is it says "if the smallest number for the nth row is equal to the row above, then find the nth + 1 smallest, otherwise, just return the nth smallest." The iferror is just to help identify the end of the numbers.
Let me know if this helped.

Sort Order formula to alphabetise in Excel

I am currently drawing up a spreadsheet that will automatically remove duplicates and alphabetize a list:
I am using the COUNTIF() function in column G to create a sort order and then VLOOKUP() to find the sort in column J.
The problem I am having is that I can't seem to get my SortOrder column to function properly. At the moment it creates an index for two number 1's meaning the cell highlighted in yellow is missed out and the last entry in the sorted list is null:
If anyone can find and rectify this mistake for me I'll be very grateful as it has been driving me insane all day! Many thanks.
I'll provide my usual method for doing an automatic pulling-in of raw data into a sorted, duplicate-removed list:
Assume raw data is in column A. In column B, use this formula to increase the counter each time the row shows a non-duplicate item in column A. Hardcord B2 to be "1", and use this formula in B3 and drag down.
=if(iserror(match(A3,$A$2:A2,0)),B2+1,B2)
This takes advantage of the fact that when we refer to this row counter in our revised list, we will use the match function, which only checks for the first matching number. Then say you want your new list of data on column D (usually I do this for display purposes, so either 'group-out' [hide] columns that form the formulas, or do this on another tab). You can avoid this step, but if you are already using helper columns I usually do each step in a different column - easier to document. In column C, starting in C3 [C2 hardcoded to 1] and drag down, just have a simple counter, which error-checks to the stop at the end of your list:
=if(C2<max(B:B),C2+1," ")
Then in column D, starting at D2 and dragged down:
=iferror(index(A:A,match(C2,B:B,0)),"")
The index function is like half of the vlookup function - it pulls the result out of a given array, when you provide it with a row number. The match function is like the other half of the vlookup function - it provides you with the row number where an item appears in a given array.
Hope this helps you in the future as well.
The actual reason that this is going wrong as implied by Jeeped's comment is that you can't meaningfully compare a string to a number unless you do a conversion because they are stored differently. So COUNTIF counts numbers and text separately.
20212 will give a count of 1 because it is the only (or lowest) number.
CS10Z002 will give a count of 1 because it is the first text string in alphabetical order.
Another approach is to add the count of numbers to the count if the current cell contains text:-
=COUNTIF(INDIRECT("$D$2:$D$"&$F$3),"<="&D2)+ISTEXT(D2)*COUNT(INDIRECT("$D$2:$D$"&$F$3))
It's easier to show the result of three different conversions with some test data:-
(0) No conversion - just use COUNTIF
=COUNTIF(D$2:D$7,"<="&D2)
"999"<"abc"<"def", 999<1000
(1) Count everything as text
=SUMPRODUCT(--(D$2:D$7&""<=D2&""))
"1000"<"999"
(2) Count numbers before text
=COUNTIF(D$2:D$7,"<="&D2)+ISTEXT(D2)*COUNT(D$2:D$7)
999<1000<"999"
(3) Count everything as text but convert numbers with leading zeroes
=SUMPRODUCT(--(TEXT(D$2:D$7,"000000")<=TEXT(D2,"000000")))
"000999" = "000999", "000999"<"001000"

Resources