index return data value between two dates and additional criteria - indexing

index-match-multiple-criteria
I want to extract the transactions from column H that match the specific product in column J, but at the same time, it should match location 65 location data is in column A the attached picture show the full data details.
currently I'm using =INDEX($H:$H,MATCH(1,(($B:$B=J2)($D:$D>=$K$3)($D:$D<=$L$3)),0))
how can I expand the above index formula to include the specification of location 65
the formula should return the transactions that were made between two dates k3 start date and L3 end date for each product line in the J column and also those transactions should be made by 65 location from the data given at column A
I really need your help on this I have been looking for a resolution to my request.

Related

Looping if statement with index and match functions

I'm trying to write a VBA code and I'm at a loss. I have a certain survey depth and a range of depths. I need to find the depth interval the survey falls within and then subtract the survey depth from the starting depth. At the next survey, I need to do the same but also add the remainder of the previous survey.
The data looks like this:
Basically what I want to do is:
if S1 > d1 and S1 < d2 then s1-d1
if s2 > d3 and s2 < d4 then (s2-d3) + (d2-s1)
' and so on.
However, the survey depths will not always line up with the depths so I need some type of match function that will correlate the survey with the correct depth range.

How to return the latest date with the same report number

I have some entries as shown in the image below and I am looking for a formula or VBA code (best to be formula) to do a calculation in column J.
The logic I want is:
Under the condition that values in column D are the same, return the latest day in column I. For example, D2 to D14 are with the same value, so return the latest day from I2 to I14 which is 3/16/17.
Column D is the report number, one report can include several transactions, e.g. if I have 10 transactions in one report, I have 10 lines with the same report number. I am trying to identify the date of the last transaction of this report.
With right formula, J2 to J14 should all be 3/16/2017.
Can anyone tell me how to do it?
You can use this array formula at J2 then fill down:
=MAX(I2:I1000*(D$2:D$1000=D2)) Ctrl+Shift+Enter
p.s.: it's an array formula, type it then press Ctrl+Shift+Enter
Assuming that column I will always be ascending within the groups of column D (e.g., Sort By Report Key, then by Transaction Date), add this formula to J2 and fill down:
=IF(D2=D3,J3,I2)

Trailing Average Using AverageIf in Excel

I am trying to find the average for the last 3 instances only. I am using the AVERAGEIF statement and it will calculate the average for the entire range but I need it to only calculate for that last 3 instances it finds (or less if there is less than 3 available). I need the entire column for G and H to have the average for the last 3 games that the Team played.
This is what I have:
=AVERAGEIF(B3:C17,B17,D3:E17)
You can do this with array formulas (They have to be entered using the keys Ctrl+Shift+Enter)...
Basic steps are:
Find the row (including and above current) that is the third highest row number containing the team name (or use row 1 otherwise)
Use the INDIRECT ranges in your AVERAGEIF from B-that_row to C-current_row and D_that_row to E-current_row
So in cell F17 you would have the formula
{=AVERAGEIF(INDIRECT("B"&LARGE(IF(--($B$3:B17=B17)+($C$3:C17=B17),ROW($B$3:B17),1),3)&":"&CELL("address",C17)),B17,INDIRECT("D"&LARGE(IF(--($B$3:B17=B17)+($C$3:C17=B17),ROW($B$3:B17),1),3)&":"&CELL("address",E17)))}
We repeat some of the logic, because we have two ranges (criteria range and average range).
IF(--($B$3:B17=B17)+($C$3:C17=B17),ROW($B$3:B17),1) means that if column B or (using +) column C has the value of in B17, give me the row number, otherwise 1 (our <3 case... we could make this 3, the first row of team names)
LARGE(...,3) will give us the third highest of this array --> the third highest row number having our team name
INDIRECT("B"&...&":"&CELL("address",C17)) is going to give us the range using our third highest row number to the current row, columns B and C
then we do exactly the same thing as you were doing in AVERAGEIF but using this INDIRECT range and the equivalent for columns D and E
Fun question! Good luck. And remember to use Ctrl+Shift+Enter to enter it!
EDIT The above was giving an #NUM! error for the first two rows - that was because the LARGE function was trying to get the third largest in an array of 2! Also noticed that there were some cases where the column letter needed to be absolute (i.e. $) for copying to the Away column. So the updated formula:
{=AVERAGEIF(INDIRECT("B"&LARGE(IF(--($B$3:$B17=B17)+($C$3:$C17=B17),ROW($B$3:$B17),1),MIN(3,ROW()-2))&":"&CELL("address",$C17)),B17,INDIRECT("D"&LARGE(IF(--($B$3:$B17=B17)+($C$3:$C17=B17),ROW($B$3:$B17),1),MIN(3,ROW()-2))&":"&CELL("address",$E17)))}
Replaced the 3 with MIN(3,ROW()-2) so that we get 3 if there are, but 1 or 2 if we are in one of the first two data rows
OK I posted this prematurely and attempted to delete it when I realised it wouldn't work. It should work now.... providing you add another condition which is the game dates in column A. Remember that this is an array formula so hit ctrl+shift+enter. Dates in column A; teams in column B; stats in column D. This formula can reside somewhere permanent on the sheet so you can enter the team name (shown as F13 here) to get the three most recent stats.
=AVERAGE(VLOOKUP(LARGE(IF(B3:B24=F13,A3:A24),1),A3:D24,4),VLOOKUP(LARGE(IF(B3:B24=F13,A3:A24),2),A3:D24,4),VLOOKUP(LARGE(IF(B3:B24=F13,A3:A24),3),A3:D24,4))

Divide rows and then transfer divided values in new worksheet (values: time periods & amounts)

Hope you can help with this:
In worksheet "TOTALS" and Range "M11:N251" there are "from:to" time periods inserted by user ("From" is in "M11:M" and "To" is in "N11:N"). Number of rows (and inserted time periods) may vary. Time periods are quadrimester (4months) but not always 120 days (could be 119, 122 or sth like that - if less than 100 then user must not be allowed to divide). Range "W11:W251" hosts amounts of money. So, for example, "M11:N11" can be 1/1/13-1/5/13 (dd/mm/yy) and W11 just a number (i.e. 98,45).
I want to be able to divide each time period almost by 2 (first part can be 60 days, second part rest of days) and amounts accordingly (depending on the number of days of its divided period) and transfer the divided periods and amounts to a new worksheet (TOTALS2) to -let's say- range "A11:B251" & "G11:G251").
So, in the above example, in "TOTALS2" we'll have 1/1/13-1/3/13 in "A11:B11" and 1/3/13-1/5/13 in "A12:B12", and the divided amounts accordingly to the counted days of "A11:B11" & "A12:B12" in "G11" and "G12".
And so on until there are no more time periods & amounts in TOTALS to divide and transfer to TOTALS2.
How can this be done? Ideas?
Thanks in advance!
If you want to check out the file, then download it:
http://www.sendspace.com/file/5kudcy
Based on the file you give in your link where the values in TOTALS start at row 10 (rather than 11 as stated), the prices are in column S (rather than W as stated) and the values in TOTALS2 are to appear in row 10 onwards (rather than row 11 onwards as stated) the following approach works:
Add 5 columns to worksheet TOTALS. These 5 columns have a header in row 9 and formulae in rows 10, 11, 12, etc. Using columns X, Y, Z and AA the header values in X9:AB9 are
Period, Int.To, Int.From, Proportion1, Proportion2
(where Int. is an abbrevation for intermediate.)
The formulae to use in cells X10 to AB10 are:
X10: =N10-M10+1 Calculates number of days from date M10 to date N10, inclusive.
Y10: =M10+59 Calculates end date of the 60 day period starting on date M10 (i.e calculates last date of first "half period")
Z10: =1+Y10 Calculates day after date in Y10 (start date of second "half period")
AA10: =60/X10 Proportion of whole period that is accounted for by first half-period
AB10: =1-AA10 Proportion of whole period that is accounted for by second half period
The formulae in X10:AB10 can be copied down for as many rows as contain data in worksheet TOTALS, to get something like...
The additional columns in TOTALS now provide the information that you need to split each quadrimester into two "halves". Cols M, N, Y and Z provide the date information and S, AA and AB the values for splitting each quadrimester's costs. To get it to display as desired in TOTALS2 you will also need to add a couple of columns to TOTALS2. I've used columns L and M with the following headers in L9 and M9:
Source, Part
In both L10 and L11 insert the value 1, whilst in M10 and M11 insert the values 1 and 2, respectively. Now add the following formulae to L12 and M12
L12: =1+L10
M12: =M10
Copy the formulae in L12 and M12 down so that columns L and M contain values in at least twice the number of rows (in TOTALS) that you wish to split into first half and second half periods. You should end up with the sequence 1,1,2,2,3,3,4,4 etc in column L and 1,2,1,2,1,2,1,2 etc in M.
The Source column (L) indicates which data row in TOTALS (1=first, 2=second, 3=third, etc) acts as the source of the values to be split in "half" and the Part column (M) indicates which "half" it is (1=first, 2=second).
All that remains is to put it all together using appropriate formulae in columns A, B and G of TOTALS2. The formulae to insert into A10, B10 and M10 are:
A10: =OFFSET(IF(M10=1,TOTALS!M$9,TOTALS!Z$9),L10,0)
B10: =OFFSET(IF(M10=1,TOTALS!Y$9,TOTALS!N$9),L10,0)
G10: =OFFSET(TOTALS!S$9,L10,0)*OFFSET(TOTALS!Z$9,TOTALS2!L10,M10)
Copy these formulae down the rows in TOTALS2 and it should be looking like...
The formulae in col A pick the start dates of the "half" periods using either col M or col Z from the correct Source row in TOTALS, according to whether the Part value is 1 or 2. Similarly, the formulae in col B pick the end dates using cols Y and N of TOTALS. The formulae in G multiply the cost value (col S of TOTALS) by the correct proportion from either col AA or col AB in TOTALS, again according to whether Part is 1 or 2.
I haven't included refinements such as:
preventing periods which are too short (or long) in TOTALS from being split (hint: you can detect this using the Length column in worksheet TOTALS) or
controlling the number of displayed rows in TOTALS2 so that it is exactly twice the number of data rows entered in TOTALS (not too difficult and several ways it can be approached) or
rounding the calculated costs for the two "halves" to 2 decimal places AND making sure that they add back to the original quadrimester cost (it is formatting in the worksheet that is causing only 2 decimals to be displayed and it is not guaranteed that the displayed values will sum precisely to their original source cost - the examples chosen just happen to have this property. Again not too difficult to solve.)
However, the above is a basic solution on which you can build.

Counting multiple criteria

I'm trying to count multiple criteria and got the code to work somehow with SUMPRODUCT.
Range("i2") = [SUMPRODUCT(($CZ$2:$CZ$61410="condition 1")*($DD$2:$DD$61410="condition 2")*($CU$2:$CU$61410=$A$2 <-/date/)*($CY$2:$CY$61410="condition 3"))]
There's around 40 colums with different conditions. So I have to code for each condition. But that's not really working for me, because I have to do the same thing for the whole month.
Explanation
Cells A2 to A32 hold dates, let's say from 1st of August 'til 31 of August.
Raw data table (on the same sheet) has dates on the CU column (might be 60000+ lines).
Column CZ, DD, CY, etc. have conditions that have to be met to be counted.
Question
What I want to do is if the date in column CU = the date in A2 then it will be counted to row 2, if the date in column CU = the date in A3 then it will be counted to row 3.
That way I can write one code for the whole month, not each day separately.
I offer first a solution of a formula (WorksheetFunction if preferred) of this type:
=COUNTIFS(CZ:CZ,"=1",DD:DD,"=2",CY:CY,"=3", ... CU:CU,"="&A2)
and an image showing what this results in (the yellow cell) depending upon the date chosen:
This counts the number of instances, on the date chosen in A2, that match the hard-coded criteria displayed in Row1 for illustrative purposes.
Without the hard-coding of criteria but with possibly a slightly better 'match' to the OP and with more examples:
Apart from the highlighting, the contents of C6 were entered via the following macro:
Sub Macro1()
Range("C6").Select
ActiveCell.FormulaR1C1 = "=COUNTIFS(C[101],R1C[101],C[105],R1C[105],C[100],R1C[100],C[96],""=""&RC[-2])"
End Sub