Climate: Calculate Consecutive Days of Rain - excel

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

Related

How to find the 3 highest values and respective category for a cell

Here is an example of the data I'm trying to organize:
I'm looking for a way to automatically see the top 3 categories (column) for each Name# (row). The size of the category is determined by the number below the category.
Ideally, I'd also like to see a percentage breakdown (from the total) for each category. For example, in row "Name3" 2 categories make up a significantly larger portion of the total values. However, without this percentage breakdown, the 3 top values would seem to be comparable, when they are in fact, not.
Interested to see how this would all work with duplicate numbers, too.
I've tried Excel's rank function, but this doesn't tell me the categories that have the 3 largest sizes, just the 3 highest values.
With Office 365:
=FILTER(SORTBY($B$1:$H$1,B2:H2,-1),SORT(B2:H2,1,-1,TRUE)>=LARGE(B2:H2,3))
And copy down.
If there are ties it will expand the results to include it. It finds the third highest value and returns everything that is equal to or greater than it.
This approach spills all the results at once (array version). In cell J2, you can put the following formula:
=LET(D, A1:H5, A, TAKE(D,,1), DROP(REDUCE("", DROP(A,1), LAMBDA(ac,aa,
VSTACK(ac, TAKE(SORT(DROP(FILTER(D, (A=aa) + (A="")),,1),2,-1,1),1,3)))),1))
It assumes as per input data the cell A1 is empty (if not it can be adjusted accordingly). Here is the output:
An alternative that doesn't require previous assumption (but it is not really a hard one) is the following:
=LET(names, A2:A5, Data, B2:H5, colors, B1:H1, DROP(REDUCE("", names,
LAMBDA(ac,n, VSTACK(ac, TAKE(SORT(VSTACK(colors, INDEX(Data, XMATCH(n,names),0))
,2,-1,TRUE),1,3)))),1))
The non-array version can be obtained from previous approach, and expand it down:
=TAKE(SORT(VSTACK($B$1:$H$1,INDEX($B$2:$H$5, XMATCH(A2,$A$2:$A$5),0)),2,-1,TRUE),1,3)
Explanation
To spill the entire solution it uses DROP/REDUCE/VSTACK pattern. Check my answer to the following question: how to transform a table in Excel from vertical to horizontal but with different length.
For the first formula we filter for a given element of A name (aa) via FILTER the input data (D) to select rows where the name is empty (to consider the header) OR (plus (+) condition) the name is equal to aa. We remove via DROP the first column of the filter result (names column). Next we SORT by the second row (the first rows are the colors) in descending order (-1) by column (last input parameter of SORT we can use TRUE or 1). Finally, we use TAKE to take the first three columns and the first row.
For the second approach, we select the values for a given row (names equals n) and use INDEX to select the entire row (column index 0), then we form an array via VSTACK to add as first row the colors and use the similar logic as in previous approach for sorting and select the corresponding rows and column (colors).
Notes:
If you don't have VSTACK function available, then you can replace it as follow: CHOOSE({1;2}, arr1,arr2) and substitute arr1, arr2, wit the corresponding arrays.
In the second formula instead of INDEX/XMATCH you can use: DROP(FILTER(Data, names=n),,1), it is a matter of personal preference.

Function to search for specific number and then to further search for the prefix

I have a huge amount of data to process in which 4 points with a related prefix needs to be subtracted from each other.
Data consists of ID and x value
Example
ID = 290.12, 290.03, 290.06, 290.09, 300.12, 300.03, 300.06, 300.09, 301.12, 301.03, 301.06, 301.09
(let's call prefix a "ring number" and suffix time on the clock)
X value = any numerical value for each ID assigned
What I'm hoping to do is to search for the first number before the dot i.e. 300 and then subtract the value of 300.06-300.12 in one cell and in another cell 300.03-300.09.
(The subtraction is just an example, how I need to manipulate with the numbers is slightly more complicated, but I got this one under control)
This is my actual Data and what I need to produce is to the right of the raw data. At the moment, I'm doing it manually for each set of "rings"
Anyone knows how to approach this? I'm thinking vlookup, but I'm not very proficient in excel.
New Excel
I tried vlookup, but I don't know how to construct the formula and I run out of ideas.
Edit:
I found out that REDUCE is no requirement in this case, so it can be shortened to:
=SQRT(SUM(((INDEX(B:D,XMATCH(I3+0.09,A:A),SEQUENCE(1,3))-INDEX(B:D,XMATCH(I3+0.03,A:A),SEQUENCE(1,3)))^2)))
You could change +0.09 and +0.03 to your needs and may reference them using LET() for easy maintaining:
=LET(id,I3,
_id1,0.09,
_id2,0.03,
SQRT(SUM(((INDEX(B:D,XMATCH(id+_id1,A:A),SEQUENCE(1,3))-INDEX(B:D,XMATCH(id+_id2,A:A),SEQUENCE(1,3)))^2))))
Previous answer:
=LET(
id,I3,
_id1,0.09,
_id2,0.03,
SQRT(
REDUCE(0, SEQUENCE(1,3),
LAMBDA(x, y,
x+((INDEX(B:D,XMATCH(id+_id1,A:A),y)
-INDEX(B:D,XMATCH(id+_id2,A:A),y))
^2)))))
This formula looks for the matching value of the id value I3 + _id1 minus the matching value of id value + _id2 for columns B to D and adds the ^2 results per column. Then it calculates it's square root.
You can change _id1 and _id2 to your needs.
To calculate the Delta (as shown) at once you could use:
=LET(id,I3,
_id1,0.09,
_id2,0.03,
_id3,0.12,
_id4,0.06,
x,SQRT(SUM((INDEX(B:D,XMATCH(id+_id1,A:A),SEQUENCE(1,3))-INDEX(B:D,XMATCH(id+_id2,A:A),SEQUENCE(1,3)))^2)),
y,SQRT(SUM((INDEX(B:D,XMATCH(id+_id3,A:A),SEQUENCE(1,3))-INDEX(B:D,XMATCH(id+_id4,A:A),SEQUENCE(1,3)))^2)),
(x-y)*1000)
You can have a column of unique values of the integers and a new column where you reference these values as id and drag down the formula to get your row by row result
In another column you can refer to these columns and sort per the second column using SORTBY()

Calculating a running sum with reset in Excel

I have a need to calculate a running sum of column D's count data in column E. However, I only want to calculate the running sum for the appropriate categories in columns B and C. In other words, there are four combinations of categories and I need a running sum for each. The easiest way is to do what I currently have in column F (cell F3=SUM($D$2:D3)) and drag it down through F11 and manually restart it at F12. I can't do this in my full dataset though because there are about 20k rows of data. So, I'm trying to make column E dynamically calculate what's in column F. I started with =SUMIFS() and can return the final sum for each combination of the two categories, but it's not a running sum, created dynamically, that resets with the new day count in column A.
Any suggestions would be appreciated. TIA
Initial Response
If I understand the problem correctly, the solution is actually very simple.
This formula goes to E2 (and then copy down):
=IF(B1&C1<>B2&C2,D2,SUM(E1,D2))
In each case (including the first) of a change of either Cat A or Cat B, it takes the count value at that row (i.e. the new starting balance). Thereafter, it does a running balance addition (balance from row above + count at this row), until the next change of Cat A or Cat B is encountered.
Catering for a wider range of Categories:
The above assumes: Cat A and Cat B are only ever 0 or 1 (per the example in OP).
If this isn't true (and the Cats could be any range of values), change the formula per chris neilsen's suggestion (per comments):
=IF(OR(B1<>B2,&C1<>&C2),D2,SUM(E1,D2))
Catering for Previous Same-State Categories:
Both the original formula and the alternate above assume:
Should Cat A & Cat B states change, but subsequently return a 'previous same-state', the running total should still start afresh (i.e. don't 'carry forward' from 'previous same-states').
If one wanted the running balance to include the balance of previous same-states for Cat A & Cat B, use solution suggested by Apostolos55 (below)
=SUMIFS($D$1:D2,$B$1:B2,B2,$C$1:C2,C2)
all you need is a minor adjustment to the cumulative count:
=Sumifs($D$2:D2,$B$2:B2,B2,$C$2:C2,C2)
Put in Current Count and it is done. Drag/drop OR Autocomplete down as needed
Now it takes into account only above/previous than the current...
It's a bit hacky, but you can add a few columns:
consecutive numbers from 2 to end of your data (say this is in Column G).
references to every 10th cell (e.g. G2, G12) for every cell in that cluster. (say this is in Column H)
you can fill in the first few rows for these two columns and then drag it down.
a reference to the count column ('D') in a single cell (say I1).
You can then use CONCATENATE and INDIRECT inside SUM:
Cumulative Count:
SUM(INDIRECT(CONCATENATE($I$2,H2)):D2)
and drag this down.

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).

Count If match multiple fields: First field by initials, second field if date has been filled

I'm trying to create a simple tracker. In columns B and C, tasks are tagged with an author, and then the author fills in a completion date.
Above is a dashboard that counts items for easy viewing. I countif for the total, but have not been able write a formula for the complete column. I've tried countifs, but haven't been able to get the formula to work.
Thoughts?
Try below formula as per attached
OUTPUT
Using DSUM will probably send you in the right direction.
http://office.microsoft.com/en-gb/excel-help/dsum-HP005209069.aspx
From what I see in your image, you should do a Pivot table for your dashboard.
If you absoluteley have to (???) get along without a Pivot table, do the following:
start the dashboard in column B, so that column A just contains column header "Tasks" and data (no blank rows allowed!)
count the number of tasks as =COUNTA(A:A)-1 (-1 for the header)
count all other columns as =COUNTA(OFFSET(B5;0;0;$E$2;1)) (in this example you count column B, header is in B4, first data therefore in in B5, number in tasks from above formula in $E$2)
The Offset function basically gives back a range starting relative to a reference and being of chooseable size. So we start it at the first data cell and make it 1 column wide, and #oftasks rows deep
With COUNTIFS, assuming VH is in A8, Tasks/Author/Completed are in Row12 and you have or might have data up to Row1000:
=COUNTIFS(B$12:B$1000,A8,C$12:C$1000,">1")
copied down as required.
You can use SUMPRODUCT to find the amount completed per author.
=SUMPRODUCT(--($B$8:$B$16=A3),--($C$8:$C$16>1))
In this case, the "--" tells SUMPRODUCT to treat each cell in the array (I used the example by User2063626) individually so it starts at the beginning and checks if B8=VH and records the answer as a boolean (1 for TRUE or 0 for FALSE), in this case it does equal VH so it uses a 1. Then it checks to see if C8>1 (it will be if there is a date) and stores a 1. It then multiplies the two values together 1*1=1 to give you a value for the first row. It then moves on and does the check for B9 (gets a 1 because it is equal to VH) and C9 (gets a 0 because there is no date), multiplies the row result 1*0=0 and adds it to the previous result. It repeats for each row in the array and returns the count of rows where the author was VH AND there is a completed date (2).

Resources