Sum of last 4 results in a column with multiple conditions - excel

I have a list of data (top to bottom with gaps) in range AM4:AM1000 which is always added to and to which i want to find and sum the last 4 results. But I only want to find those results which correspond to a separate column, range AL4:AL1000 that is equal to cell E3, and where a third column (AS4:AS1000) meets the criteria of "p".
Im using the code below which extracts the last 4 results but I can't make it meet the other two conditions. any help would be gratefully appreciated
=SUM(INDIRECT("Am" & LARGE(IF(NOT(AM4:AM1000=""),ROW(AM4:AM1000),0),1) & ":Am" & LARGE(IF(NOT(AM4:AM1000=""),ROW(AM4:AM1000),0),4)))
Iv also tried the code below but this only returns the value 0
=SUM(IFERROR(INDEX($AM$4:$AM$1000,LARGE(IF(ISNUMBER(MATCH($AL$4:$AL$1000, $E$3, 0)),IF(AS$4:AS$1000="p",MATCH(ROW(AL$4:AL$1000), ROW(AL$4:AL$1000)), "")), ROWS($I$6:$I7))),""))

Here's an array formula for you to try - make sure to enter it by using Ctrl+Shift+Enter:
=SUMPRODUCT(IF(ROW($AM$4:$AM$1000)=TRANSPOSE(LARGE(IF(--($AL$4:$AL$1000=$E$3)*(--($AS$4:$AS$1000="p")),ROW($AM$4:$AM$1000),0),ROW($A$1:$A$4))),$AM$4:$AM$1000,0))
The result of 26 matches your criteria (highlighted cells):

Related

How to make Excel follow a formula algorithm on every 3 rows

I want to make Excel follow a formula pattern on every 3 rows, such that after an increase of three in the conditional if statement, there should be an increment of one on return value "TRUE". So that every time I drag it down, the algorithm gets applied to succeeding 3 rows that it follows. I have this simple formula of:
=IF(B11="RTR",RTR!G11:Z11,IF(B11="DHG2",'DHG2'!G11:Z11,IF(B11="JQV",JQV!G11:Z11,"")))
=IF(B12="RTR",RTR!G11:Z11,IF(B12="DHG2",'DHG2'!G11:Z11,IF(B12="JQV",JQV!G11:Z11,"")))
=IF(B13="RTR",RTR!G11:Z11,IF(B13="DHG2",'DHG2'!G11:Z11,IF(B13="JQV",JQV!G11:Z11,"")))
=IF(B14="RTR",RTR!G12:Z12,IF(B14="DHG2",'DHG2'!G12:Z12,IF(B14="JQV",JQV!G12:Z12,"")))
=IF(B15="RTR",RTR!G12:Z12,IF(B15="DHG2",'DHG2'!G12:Z12,IF(B15="JQV",JQV!G12:Z12,"")))
=IF(B16="RTR",RTR!G12:Z12,IF(B16="DHG2",'DHG2'!G12:Z12,IF(B16="JQV",JQV!G12:Z12,"")))
Following the formula I wanted, it's supposed to be followed by:
=IF(B17="RTR",RTR!G13:Z13,IF(B17="DHG2",'DHG2'!G13:Z13,IF(B17="JQV",JQV!G13:Z13,"")))
I tried dragging down the formula to apply it to below rows but the pattern doesn't follow to what I wanted. Instead, the row below goes into like:
=IF(B17="RTR",RTR!G17:Z17,IF(B17="DHG2",'DHG2'!G17:Z17,IF(B17="JQV",JQV!G17:Z17,"")))
Need a little bit of help here.
Thank you.
Here is a solution that doesn't require helper cells:
This formula will increment by 1 every 3 rows:
=CEILING.MATH(ROW()/3)
If you want the sequence to start on a different row, say row 10, you can just subtract that number - 1 from the ROW(), so for starting on row 10 from 1, subtract 9:
=CEILING.MATH((ROW()-9)/3)
And if you wanted the sequence to start from a different number at a different row, you can just add that number -1 to this whole thing, this example will start at 11 on row 10:
=CEILING.MATH((ROW()-9)/3)+10
EDIT: Much better solution proposed by P.b, you don't need the IFs at all:
=INDIRECT(B11 & "!G" & (CEILING.MATH(ROW()/3)+10) & ":Z" & (CEILING.MATH(ROW()/3)+10))
So, an example which you can expand:
VLOOKUP(A1,INDIRECT("'RTR'!"&"G11:Z11",1),1,0)
Not sure where you increment of 3 occurs as B11 goes to B12 and even the last two B16 goes to B17 and the target range matches the row numbers.
As I have shown you can build the target range so adding the row numbers is trivial now. They can be taken from cells, C1 could be 3, C2 6 etc.
The following could be used (to avoid using (volatile) INDIRECT):
=INDEX(
CHOOSE( MATCH(B11, {"RTR","DHG2","JQV"}, 0),
'RTR'!G:Z, 'DHG2'!G:Z, 'JQV'!G:Z),
INT((ROW()-11)/3)+11,
)
It selects the correct tab by matching the value in B11 to an array mentioning the names of the tabs. It than indexes range G:Z from that tab.
The row number shifts 1 number every 3 rows because of INT((ROW()-11)/3)+11.

Excel multiple rows values addition

i am trying to add the formula that will sum first two rows and excel, put the result in the next column, then move down, take the next two values and put the result below the sum of the previuos one. (it is clear in the picture).
Please any advice would be helpful because my formula is taking the second value and add it on the next one which is not what i need. https://postimg.org/image/st4uie90j/ example
The following formula will work to do what you ask if the columns are adjacent and set the $A$1 to the first cell (top-left of your data set):
=SUM(OFFSET(INDIRECT("R" & (ROW($A$1) +(( ROW()-1)*2)) & "C" & (COLUMN()-1),FALSE),0,0,2,1))

I want to list all values in a range where adjacent cell value have:1) time difference is less than 24hour, and other cells 2) equal specific text

TABLE + DESIRED RESULT
This is what I got but not sure why it doesn't work:
Formula entered in Sheet2!A2=
index($A$2:$A$100, match(0,if(OR(AND(now()-Sheet1!O2<1,D2="closed"),D2="Pending",""),0))
Show me the list of cells in Sheet1! when Condition 1: Now - Time in O2 < 1 Day & D2 = "closed"
Or Condition 2: D2 = "Pending"
Thanks for the help guys
Ok I think I got it, this is an array formula so you want to copy it into cell G2 and press CTRL+SHIFT+ENTER, then drag the formula down far enough to hold all the results. I changed the condition from "Open, waiting, pending or closed within the last 24 hours" to "not closed more than 24 hours ago" to make it easier to read
formula for G2 is:
=IFERROR(INDEX(A$2:A$11, SMALL(IF(NOT((C$2:C$11="Closed")*(NOW()-D$2:D$11>1)), ROW(A$2:A$11)-ROW(A$2)+1), ROWS(A$2:A2))), "")
The SMALL function looks like this =SMALL(array, n) and returns the nth smallest element of the array. In our case n is given by ROWS(A$2:A2) which will give the row number of the current row in the result output. i.e. the first row of your results will contain the 1st smallest number, the 2nd row will contain the 2nd smallest number etc, the trick is that its not giving the smallest number in the id list but its giving the smallest number in the array defined by this line:
IF(NOT((C$2:C$11="Closed")*(NOW()-D$2:D$11>1)), ROW(A$2:A$11)-ROW(A$2)+1
This part is a little complicated as its all array formulas/logic. Please note the * in this context represents a logical AND. If you want to understand it better you can highlight just this part of the formula in the formula bar in Excel and press F9, this will show the values of the array. Clicking in cell G2 and highlighting the line above gives this:
{1;FALSE;3;4;5;FALSE;FALSE;8;9;10}
You can see that the resulting array contains the row number for rows that meet the condition and FALSE for those who don't. The INDEX and SMALL functions then display the id of the row with the 1st smallest value then the second etc with the FALSE's used to ignore the rows that don't meet the condition.
syntax error
you have:
AND(now()-Sheet1!O2<1, D2="closed")
which is fine, problem is in your OR statement:
OR(AND(now()-Sheet1!O2<1,D2="closed"),D2="Pending","")
the last parentheses should be before the last comma I believe. Currently you have an OR statement with 3 arguments the last of which is "" which isn't going to give you what you want. I think you intended the "" to be the result of the IF statement not a condition for the OR statement, try this:
if(OR(AND(now()-Sheet1!O2<1, D2="closed"), D2="Pending"),"",0)

Find a range of value in excel

I have two different sheets with 300,000 data in Excel.
First sheet contains:
S2_Symbol Start_Pos End Position
STE 254857 267891
PRI 748578 758962
ILA 852741 963369
VIS 789456 796325
Second:
S1_Location
789460
852898
748678
My output should be like this:
S1_Location Symbol
789460 VIS
852898 ILA
748678 PRI
I have to find that S1_location falls in which S2_location and its corresponding Symbol. I have used INDEX formula in Excel but for each cell, I have to change the reference cell manually. I couldn't do it 300,000 data.
How can I do in an in Excel or should I use a script?
This solution assumes the following:
Start and End Positions for each S2 Symbol are unique (i.e. there is no intersection between the ranges allocated to each symbol)
Data in first sheet is located at A1:D17 (adjust ranges in formulas as needed)
Data in second sheet is locate at A1:B300010 (adjust ranges in formulas as needed)
The solution requires:
To add a working column in worksheet one. Enter this formula in D2 and copy till last record.
=ROWS($A$1:$A2)
Fig. 1
Then in second worksheet enter this formula at B2 and copy till last record.
=INDEX( Sheet1!$A$1:$A$17,
SUMIFS( Sheet1!$D$1:$D$17,
Sheet1!$B$1:$B$17, "<=" & $A2, Sheet1!$C$1:$C$17, ">=" & $A2 ) )
Fig. 2
It took aprox. less than 14 seconds to copy downwards and calculate the formulas in sheet 2.
As it can be seen in figures 1 and 2 none of the tables need to be sorted.
Assuming both sheets start in A1, and First sheet ColumnB is sorted ascending, in Second sheet B2 please try:
=INDEX(First!A:A,MATCH(A2,First!B:B))
copied down to suit. It relies on inexact matching.
Assuming we have a Sheet1 like this:
note, the Sheet1is sorted by Start_Pos, End_Pos in ascending order.
and a Sheet2 like this:
Then the formula in Sheet2!B2 downwards could be:
=INDEX(Sheet1!A:A,IF(MATCH(A2,Sheet1!B:B)>IFERROR(MATCH(A2-(10^-10),Sheet1!C:C),0),MATCH(A2,Sheet1!B:B),NA()))
See MATCH: https://support.office.com/en-us/article/MATCH-function-e8dffd45-c762-47d6-bf89-533f4a37673a
The idea is: MATCH without exact matching (without parameter match_type) gets the row of the largest value which is smaller or equal the search value. So in the Start_Pos column it will get the row from which we can get the S2_Symbol. But from the End_Pos column it should get one row beforehand if the value is not outside the given ranges.
There is only one exception. If the value is exact the value in the End_Pos column, then it will return the same row as in the Start_Pos column. Considering this exception, we can search in the End_Pos column with a little bit smaller value. Thanks to Tom Sharpe for his comment.
The formula in Sheet2!D2 downwards is:
{=INDEX(Sheet1!A:A,MIN(IF($A2>=Sheet1!$B$2:$B$300000,IF($A2<=Sheet1!$C$2:$C$300000,ROW(Sheet1!$A$2:$A$300000),2^20+1))))}
this is an array formula which is exactly formulated respecting the requirements. But this is very bad in performance for using in much many cells. But using this, the Sheet1 is not required to be sorted.
Benchmark test:
Have the following Sheet1:
Formulas:
A2:A300002: ="S"&(ROW(A1)-1)*10&"-"&(ROW(A1)-1)*10+7
B2:B300002: =(ROW(A1)-1)*10
C2:C300002: =B2+7
and the following Sheet2:
Formulas:
A2:A300002: =RANDBETWEEN(0,3000007)
B2:B300002: =INDEX(Sheet1!A:A,IF(MATCH(A2,Sheet1!B:B)>IFERROR(MATCH(A2-10^-9,Sheet1!C:C),0),MATCH(A2,Sheet1!B:B),NA()))
Note the -10^-9 instead of -10^-10 in previous version. This is because we have only 16 digits precision. In previous version this was maximum 6 digits integer part and then 10 digits decimal part. Now it is maximum 7 digits integer part and then 9 digits decimal part.
Calculation after pressing F9 in Sheet2 takes ca. 2 s. (Excel 2007, Windows 7, 4 core processor).
I would have gone for something like this which gives you the first match if there is one:-
=INDEX(First!A:A,MATCH(1,(First!B:B<=A2)*(First!C:C>=A2),0))
assuming keys and start and end values are in a sheet called First and lookup values start in A2.
Array formula which must be entered with CtrlShiftEnter
In response to the question from #pnuts about how long it will take, I have set up a similar benchmark with 300,000 rows in each sheet and it has reached 1% after 90 minutes, so it should take about 150 hours to reach 100% or roughly one week. This is to be expected as the number of computations required is (rows in sheet 1) X (rows in sheet 2)
300,000 X 300,000
but in fact because the multiplication applies to complete columns, I believe it is more correctly
300,000 X 1,048,576
i.e. > 300 billion.
A practical version which gives good response for smaller ranges is as follows:-
I define three named ranges Range1, Range2 and Range3
=First!$A$1:INDEX(First!$A:$A,MATCH("ZZZ",First!$A:$A))
=First!$B$1:INDEX(First!$B:$B,MATCH(9.9E+307,First!$B:$B))
=First!$C$1:INDEX(First!$C:$C,MATCH(9.9E+307,First!$C:$C))
and the modified formula is
=INDEX(Range1,MATCH(1,(Range2<=A2)*(Range3>=A2),0))
I was thinking of deleting this answer, but would rather it stood as a counter-example.

Looking for formula to extract specific values from a row containing numbers and blanks

I have a sheet with rows of data, with many columns. I am looking for help on a formula that will extract the sum of the smallest 3 numbers in a row based on the last 5 values entered. Note that not all the rows will have values for each column, so the first value found on each row will may be found in a different column.
To determine the sum of the smallest 3 I am using the formula =SUM(SMALL(B3:R3,{1,2,3})), Unfortunately, that formula is looking at the entire range. Again, I am looking for help that with a formula that will select only the last 5 values posted.
Here is simple example. The results for each line show the totals that should be determined. Again, it needs to look for the sum of the smallest 3 based on the last 5 posted (in the example below the range would be col. 1 thru 10, with col 10 having the latest postings).
Ex.
1.....2.....3......4......5.....6.....7.....8......9.....10...... Result
31.........44....51....36..........44...34....36....38.......106 (34+36+34)
35..31...44...40.....38...52..........42....37...............115 (37+38+40)
Hope this is understandable. I am looking for a formula solution vs a VBA macro solution because of my users. Thanks for any help!!
Now that you clarified the question, I have an answer for you. This is fairly ugly but it gets the job done. You might want to hide the columns with the intermediate results - or you could get adventurous and "nest" the expressions. This makes it really hard to understand / debug though. If there's a smarter way to do this I am always open to learning.
Assuming you have the data in columns A through J, starting in row 2, put the following in cell L2:P2:
=MATCH(9999, A2:J2,1)
=MATCH(9999,OFFSET($A2,0,0, 1, L2-1)) ... copy this by dragging right to the next 2 columns
=MATCH(9999,OFFSET($A2,0,0, 1, M2-1))
=MATCH(9999,OFFSET($A2,0,0, 1, N2-1))
=MATCH(9999,OFFSET($A2,0,0, 1, O2-1))
The first line finds the last cell with data in it; the next ones find the last cell "not including the last cell", and so they work backwards. The result is a number corresponding to the columns with data. For your example, this gives
10 9 8 7 5
9 8 6 5 4
Now we want to find the sum of the smallest 3 of these: put the following equation in cell Q2:
=SUM(SMALL(INDIRECT("RC["&P2-17&"]:RC["&L2-17&"]",FALSE),{1,2,3}))
Working from the inside out:
RC["&P2-17"] results in RC[-12], which is "the cell 12 to the left of this one".
That is the first of the "last five cells with data", cell E2
RC["&L2-17"] results in RC[-7], the last cell with data in this row
FALSE use "RC" rather than "A1" indexing
INDIRECT turn string into an address (in this case a range)
SMALL find the 3 smallest values in this range
SUM and add them together.
This formula did indeed give me 106, 115 for the example you provided.
I would hide columns L through P so you only see the result (and not the intermediate stuff).

Resources