Match criteria between two tables to find maximum date within a list - excel

I have 2 tables of data, one is only 5 columns and the second is 100s of columns, but in simplistic terms they have the formatting below. Both are hundreds of rows long.
Table 1 (is sorted ascending, all columns)
A B C D E
1 Date TeamF Trans Name TeamT
2 date1 teamF1 trans1 name1 teamT1
3 date2 teamF2 trans2 name2 teamT2
4 date3 teamF3 trans3 name3 teamT3
5 date4 teamF4 trans4 name4 teamT4
6 date5 teamF5 trans5 name5 teamT5
Table 2 (not sorted in any manner)
A B C D E F G ,etc.
1 Team Game Date Opp NameA NameB NameC ,etc...
2 team1 game1 date1 opp1 statsA1 statsB1 statsC1 ,etc...
3 team2 game2 date2 opp2 statsA2 statsB2 statsC2 ,etc...
4 team3 game3 date3 opp3 statsA3 statsB3 statsC3 ,etc...
5 team4 game4 date4 opp4 statsA4 statsB4 statsC4 ,etc...
6 team5 game5 date5 opp5 statsA5 statsB5 statsC5 ,etc...
I've been trying to get the INDEX/MATCH formula I typically use (below) a little more sophisticated to avoid some errors I've been having in data processing. I'm trying to pull the TeamT name from Table 1 and populate the stats rows in Table 2, columns E and beyond. I do this based on a
match of Names between Table1 column D and Table 2 row 1
Dates in Table 1 being <= Dates in Table 2 column C
with the following formula
my current formula =INDEX(Table1!$A:$E,MATCH(1,(Table1!$D:$D=Table2!E$1)*(Table1!$A:$A<=Table2!$C2),0),5)
As you can probably see, if there are multiple dates in Table 1 that are less than C2, I only get the first one. What I'd really like is the maximum (newest one). I've been playing with MAX/IF statements and INDEX/MATCH/IF statements but can't get the proper result. Some examples of those failed tries are below
example 1
=MAX(IF(Table1!$D:$D=Table2!E$1,IF(Table1!$A:$A<=Table2!$C2,Table1!$E:$E)))
example 2
=INDEX(Table1!$E:$E,MATCH(MAX(IF(Table1!$D:$D=Table2!E$1,Table1!$A:$A<=Table2!$C2)),IF(Table1!$D:$D=Table2!E$1,Table1!$A:$A<=Table2!$C2),0))
Any help would be appreciated. I've tried many solutions I've found, but I'm still pulling my hair out on this one.
I also have a second variation of formula where I match dates based on Dates in Table 1 being >= Dates in Table 2 column C. In that case I want the minimum match (the oldest date). I'm assuming I'd simply use MIN instead of MAX for this?

If I understand you correctly, here I have the answer for your first question.
To make the sample easier, I put everything in one sheet. You can copy to the new sheet once you are happy with the result. That way you will have the working formula.
The array formula (click Ctrl + Shift + Enter together) I used from cell K2 is:
=IFERROR(INDEX($E$2:$E$11,MATCH(MAX(IF(--($D$2:$D$11=K$1)*--($A$2:$A$11<$I2)*$A$2:$A$11,$A$2:$A$11)),IF(--($D$2:$D$11=K$1)*--($A$2:$A$11<$I2)*$A$2:$A$11,$A$2:$A$11),0)),"")
You can drag this to the fields you need. Please note this to find the most current date. Because I am not 100% sure this is what you are looking for. Therefore, in my example from cell O2, I managed to find the latest row and the formula (regular formula) is:
=IFERROR(INDEX($E$2:$E$11,LOOKUP(2,1/(--($D$2:$D$11=O$1)*--($A$2:$A$11<$I2)),(ROW($E$2:$E$11)-ROW(O$1)))),"")
For the 2nd question, I will leave it to you to find out from these two and that should do the trick.

I worked this out myself with MAXIFS to find the max date and a multi-criteria VLOOKUP function
this finds the max date less than a current row based on a name match
=VLOOKUP(MAXIFS(Table1!$A:$A,Table1!$D:$D,E$1,Table1!$A:$A,"<="&$C2)&E$1,CHOOSE({1,2},Table1!$A:$A&Table1!$D:$D,Table1!$E:$E),2,0)
the solution to a min date greater than the current row based on a name match is
=VLOOKUP(MINIFS(Table1!$A:$A,Table1!$D:$D,E$1,Table1!$A:$A,">="&$C2)&E$1,CHOOSE({1,2},Table1!$A:$A&Table1!$D:$D,Table1!$B:$B),2,0)

Related

Highlight cells with different values based on ID

I have two columns, ID and Date. I need to highlight the rows for an ID if that ID does not have the same dates in all of the rows for that ID. e.g.:
ID Date
1 1/1/2020
1 1/1/2020
1 1/1/2020
2 2/2/2020
2 2/16/2020
2 2/2/2020
In the case of ID = 1, it shouldn't highlight anything because all of the rows for the ID = 1 have the same dates, but in the ID = 2, there's different dates (There's 2 '2/2/2020' and 1 "2/16/2020') so it should highlight all of the rows of the ID = 2.
Please note that there might be cases in which different IDs use the same set of dates. e.g.:
ID Date
1 1/1/2020
1 1/1/2020
1 1/1/2020
2 1/1/2020
2 1/16/2020
2 1/1/2020
In the case above, it should highlight only the rows that have ID = 2 since there's different dates for that ID (2 '1/1/2020' and 1 '1/16/2020').
Edit: This was achieved, without creating a macro, by using Conditional Formatting.
In the ID case of 1, it shouldn't highlight anything but in the ID 2, there's different dates so it should highlight all of the rows for that ID.
No VBA required. Use Home Tab - Conditional Formatting
Select your range and use Use a formula to determine which cells to format and use the below formula. I am taking $A$2:$B$7 for demonstration purpose. Change as applicable.
=COUNTIF($A$2:$A$7,$A2)<>COUNTIF($B$2:$B$7,$B2)
If you still want VBA then simply record a macro and perform the above steps. You will get the code which you can edit to suit your needs.
#SiddharthRout Thank you for your help! This sounds like it would work but what if 2 IDs use the same date? Lets say that by coincidence 2 IDs must have the same date, then the conditional format would highlight all of the cells regardless of the ID. ID Date 1 2/2/2020 1 2/2/2020 1 2/2/2020 2 2/2/2020 2 2/3/2020 2 2/2/2020 Do you think this can be avoided using conditional format as well? – Michael Murillo 14 hours ago
In such a case, the above formula will not work. Try the conditional formatting with this formula
=COUNTIF($A$2:$A$7,$A2)<>SUMPRODUCT(IF(CONCATENATE($A$2:$A$7,"--",$B$2:$B$7)=$A2&"--"&$B2,1,0))
What this formula does is checks for the count of ID in Column A and matches it with count of text created by concatenation of Column A and Column B.

Excel, finding the highest date value from a list with duplicate values

I have a deep column A with IDs and each ID has in next column B date value.
Problem is that A contains up to 4 duplicate values at different places, thus e.g. 25430 could be there 4 times with different dates, e.g. 1.1.2014, 3.2.2015 etc.
I`d need to get into column C sorted column A - only 1 unique ID and if there are duplicate IDs in A then pick the highes date value.
Of course I could do it manually by sorting A by duplicate IDs and then manually delete other IDs with lower date but it`s not realistic to do it this way..
Thanks ;)
To identify the highest date value in ColumnB associated with each of the unique values in a labelled list in ColumnA:
=MAX(IF(A:A=A2,B:B))=B2
enter with Ctrl+Shift+Enter and copy down to suit.
So I was able to do this with the following formula if you have this data:
A B
Date Rank
01/01/2015
01/01/2015
01/01/2015
02/02/2015
01/01/2014
03/03/2015
04/04/2015
04/04/2015
03/03/2015
04/04/4015
So in B2, you should put the following code:
=SUM(1*(A2>$A$2:$A$11))+1+IF(ROW(A2)-ROW($A$2)=0,0,SUM(1*(A2=OFFSET($A$2,0,0,INDEX(ROW(A2)-ROW($A$2)+1,1)-1,1))))
then CTRL+SHIFT+ENTER which will turn it into an array and drag it down, this is what you will end up with:
Date Rank
01/01/2015 2
01/01/2015 3
01/01/2015 4
02/02/2015 5
01/01/2014 1
03/03/2015 6
04/04/2015 8
04/04/2015 9
03/03/2015 7
04/04/4015 10
1 Being the earliest date and 10 being the latest date.
I hope this helps :)

SUMIF a cell on the same row, in a certain column, contains a specific text

I'm trying to sum all transactions on a spreadsheet of my bank statement. Let's say I have the following under columns B-F:
B C D E F G
Date Type Transaction Value Balance ???
row 4 23/05/14 BAC Bank transfer A 1103.55 637.03
row 5 23/05/14 POS Pizza purchase 10.00 627.03
row 6 23/05/14 POS Coffee purchase 10.00 627.03
I'd like to make a new cell on column G which sums all values in column E that are on rows containing "POS" in column C. Is this possible?
Yes and I believe the simplest way is by using SUMIF:
=SUMIF(C:C, "POS", E:E)
In other words, sum E:E if C:C equals "POS".
=SUM(IF(C4:C6="POS",1,0)*E4:E6) is one way. Expand the ranges to taste.
Note this is an array formula: you need to press Ctrl + Shift + Return once you're done editing.

Find value in 2D array and return value in adjacent cell

**Sheet 1**
ColumnA B C D E F G H
------------------------------------------------------------
EURUSD 1.2765 1 ACCOUNT624 2 account125 1 account834
EURCAD 1.01 2 Account49 3 account45 2 account67
EURGBP 0.78 2 Account777 1 account45 2 account678
**Sheet 2**
ColumnA B C D
---------------------------------------
EURUSD 1.2765 Account 624 ?
EURUSD 1.2765 Account 125
EURUSD 1.2765 Account 834
EURCAD 1.01 Account49
EURCAD 1.01 Account45
In Sheet 1 above each row shows a currency trade and what quantity goes to each account.In Sheet 2 each row shows 1 account only. I would like to populate columnd D in sheet 2 with the quantites from sheet 1.
Breaking it up into steps, i would like to:
Find the price in Sheet2!B1 in sheet1
On the same row in sheet1, find the cell containing the same account as Sheet2!C1
Return value in cell to the left of cell with matching account
I have used index/match before but I can't get it to work for 2 dimensional arrays. Can anyone help with a formula? Thanks in advance!
It's not pretty, but using what you requested - to find the match based upon price in column B (I would say your safer bet would be to use the Currency conversion "EURUSD", for example, since what if 2 currencies have the same ocnversion rate??), paste this formula in cell D1 on your second sheet:
=OFFSET(Sheet1!$B$1,MATCH(B1,Sheet1!$B$1:$B$3,0)-1,MATCH(C1,OFFSET(Sheet1!$B$1,MATCH(B1,Sheet1!$B$1:$B$3,0)-1,0,1,10),0))
You can then drag it down / change ranges as needed.
(PS - I'm also assuming you made a mistake on sheet2 and that the account numbers will be typed the exact same in both sheets)

Sum values in a column based on date

I have written this function that will give me a monthly sum for two columns: one has the date of each order, one has the cost of each order.
=SUMIF($C$1:$C$1000,">="&DATE(2010,6,1),$D$1:$D$1000)-SUMIF($C$1:$C$1000,">="&DATE(2010,7,1),$D$1:$D$1000)
Using data like this:
8/16/10 17:00 7.99
8/16/10 14:25 7.99
8/15/10 22:42 7.99
I end up with a table like this:
May 998
June 968.28
July 1239.76
August 514.96
However, now I would like to do daily sums and using my way I have to hand edit each row.
How can I do this better in Excel?
Use a column to let each date be shown as month number; another column for day number:
A B C D
----- ----- ----------- --------
1 8 6 8/6/2010 12.70
2 8 7 8/7/2010 10.50
3 8 7 8/7/2010 7.10
4 8 9 8/9/2010 10.50
5 8 10 8/10/2010 15.00
The formula for A1 is =Month(C1)
The formula for B1 is =Day(C1)
For Month sums, put the month number next to each month:
E F G
----- ----- -------------
1 7 July $1,000,010
2 8 Aug $1,200,300
The formula for G1 is =SumIf($A$1:$A$100, E1, $D$1:$D$100). This is a portable formula; just copy it down.
Total for the day will be be a bit more complicated, but you can probably see how to do it.
Use pivot tables, it will definitely save you time. If you are using excel 2007+ use tables (structured references) to keep your table dynamic. However if you insist on using functions, go with Smandoli's suggestion. Again, if you are on 2007+ use SUMIFS, it's faster compared to SUMIF.
Following up on Niketya's answer, there's a good explanation of Pivot Tables here:
http://peltiertech.com/WordPress/grouping-by-date-in-a-pivot-table/
For Excel 2007 you'd create the Pivot Table, make your Date column a Row Label, your Amount column a value. You'd then right click on one of the row labels (ie a date), right click and select Group. You'd then get the option to group by day, month, etc.
Personally that's the way I'd go.
If you prefer formulae, Smandoli's answer would get you most of the way there. To be able to use Sumif by day, you'd add a column with a formula like:
=DATE(YEAR(C1), MONTH(C1), DAY(C1))
where column C contains your datetimes.
You can then use this in your sumif.
Add a column to your existing data to get rid of the hour:minute:second time stamp on each row:
=DATE(YEAR(A1), MONTH(A1), DAY(A1))
Extend this down the length of your data. Even easier: quit collecting the hh:mm:ss data if you don't need it. Assuming your date/time was in column A, and your value was in column B, you'd put the above formula in column C, and auto-extend it for all your data.
Now, in another column (let's say E), create a series of dates corresponding to each day of the specific month you're interested in. Just type the first date, (for example, 10/7/2016 in E1), and auto-extend. Then, in the cell next to the first date, F1, enter:
=SUMIF(C:C, E1, B:B )
autoextend the formula to cover every date in the month, and you're done. Begin at 1/1/2016, and auto-extend for the whole year if you like.
If the second row has the same pattern as the first row, you just need edit first row manually, then you position your mouse pointer to the bottom-right corner, in the mean time, press ctrl key to drag the cell down. the pattern should be copied automatically.

Resources