Excel: Match 2 Items, Including if Date is Between a Date Span on Multiple Worksheets - excel

I have 2 worksheets:
Worksheet 1: Member ID, Engagement Date and other data.
Worksheet 2: Member ID, Policy Begin Date, Policy End Date and other data.
On Worksheet 1, I want to return a policy type name (from Worksheet 2), if Worksheet 1's Member ID matches Worksheet 2's Member ID AND if Worksheet 1 Engagement Date falls between Worksheet 2's Policy Begin and End Date...
The following is the formula I tried and also have attempted extensive research, to no avail:
=INDEX('Program Data'!M2:M25671,MATCH(A2:A489&F2>='Program Data'K2&F2<='Program Data'L2,'Program Data'!E2:E25671,0))

Replace your MATCH with an AGGREGATE that Calculates the SMALLest row where the Policy ID and Dates all match up.
So, we want AGGREGATE(15, 6, <SOME CODE>, 1) to get the Smallest non-error value in a list created by <SOME CODE>
The first thing that <SOME CODE> will want is the ROW we are looking at (minus one, because I see that you are skipping the header row...) which is ROW(Sheet2!$A$2:$A$25671)-1
If the Row does not match, we want to either make it massive or make it error (even better, because then it gets completely skipped). How to do this? Well, I have the POWER function for that. If you try POWER(10,999) you get a #NUM! error, because 10^999 is too large a number for Excel. If you try POWER(0,999) you get 0, because 0^anything is 0. So, we'll just add some POWER to our ROWs to make it error-out when we don't want them.
But, now we need to decide between 10 and 0. Fortunately, Logical statements can be treated as Bitwise Multiplication (True = 1 and False = 0`)
So, --(Sheet2!$A$2:$A$25671=$A2) will give us 1 when the First Columns (Member ID) match, --(Sheet2!$B$2:$B$25671<=$B2) will make sure that the Policy Start Date is before-or-on the record date, and --(Sheet2!$C$2:$C$25671>=$B2) will check the End Date. Multiply it all together, and we get 1 when the row matches, and 0 when it doesn't.
Now, if we take that away from 1, we get the opposite: 0 when we want 0, and 1 when we want 10. So multiply that by 10, and shove it in the POWER function, and add that to the ROW to get <SOME CODE>. Dump it all in the AGGREGATE from the start, and voila
=AGGREGATE(15,6,ROW(Sheet2!$A$2:$A$25671)-1+(POWER(10*(1-(Sheet2!$A$2:$A$25671=$A2)*--(Sheet2!$B$2:$B$25671<=$B2)*--(Sheet2!$C$2:$C$25671>=$B2)),999)),1)
Then, just use that in place of your MATCH (it will generate a #NUM! error when no policy is found)
=INDEX(Sheet2!M2:M25671,AGGREGATE(15,6,ROW(Sheet2!$A$2:$A$25671)-1+(POWER(10*(1-(Sheet2!$A$2:$A$25671=$A2)*--(Sheet2!$B$2:$B$25671<=$B2)*--(Sheet2!$C$2:$C$25671>=$B2)),999)),1),1)

Related

How can I extract a total count or sum of agents who made their first sale in a specified month?

I am trying to extract some data out of a large table of data in Excel. The table consists of the month, the agent's name, and either a 1 if they made a sale or a 0 if they did not.
What I would like to do is plug in a Month value into one cell, then have it spit out a count of how many agents made their first sale that month.
Sample Data and Input Output area
I found success by creating a secondary table for processing a minif and matching to agent name, then countif in that table's data how many sales months matched the input month. However I would like to not have a secondary table and do this all in one go.
=IF(MINIFS(E2ERawData[Date Group],E2ERawData[Agent],'Processed Data'!B4,E2ERawData[E2E Participation],1)=0,"No Sales",MINIFS(E2ERawData[Date Group],E2ERawData[Agent],'Processed Data'!B4,E2ERawData[E2E Participation],1))
=COUNTIFS(ProcessedData[Month of First E2E Sale],H4)
Formula in column F is:
=MAX(0;COUNTIFS($A$2:$A$8;E3;$C$2:$C$8;1)-SUM(COUNTIFS($A$2:$A$8;"<"&E3;$C$2:$C$8;1;$B$2:$B$8;IF($A$2:$A$8=E3;$B$2:$B$8))))
This is how it works (we'll use 01/03/2022 as example)
COUNTIFS($A$2:$A$8;E3;$C$2:$C$8;1) This counts how many 1 there are for the proper month (in our example this part will return 2)
COUNTIFS($A$2:$A$8;"<"&E3;$C$2:$C$8;1;$B$2:$B$8;SI($A$2:$A$8=E3;$B$2:$B$8)) will count how many 1 you got in previous months of the same agents (in our example, it will return 1)
Result from step 2, because it's an array formula, we sum up using SUM() (in our example, this return 1)
We do result from step 1 minus result from step 3 (so we get 1)
Finally, everything is inside a MAX function to avoid negative results (February would return -1 because there were no sales at all and agent B did a sale on January, so it would return -1. To avoid this, we force Excel to show biggest value between 0 and our calculation)
NOTE: Because it's an array formula, depending on your Excel version maybe you must introduce pressing CTRL+ENTER+SHIFT
If one has got access to the newest functions:
=LET(X,UNIQUE(C3:C9),VSTACK({"Month","Total of First time sales"},HSTACK(X,BYROW(X,LAMBDA(a,SUM((C3:C9=a)*(MINIFS(C3:C9,D3:D9,D3:D9,E3:E9,1)=C3:C9)))))))

Excel return true if number is within range

My problem
I have a list of values (List 1) that have the following pattern...
1234-COD-125
I have another list (List 2), which follow the pattern...
12345(1234-COD-100 - 1234-COD-150)
I need to search List 2 and return True if the value in List 1 is within range. So for example..
List 1 Result List 2
1234-COD-125 TRUE 12345(1234-COD-100 - 1234-COD-150)
1234-COD-126 TRUE 12345(4567-BAH-100 - 4567-BAH-150)
1234-COD-155 FALSE
4567-BAH-125 TRUE
4567-BAH-126 TRUE
4567-BAH-155 FALSE
Background
The first part (1234-COD) is the vendor ID and code. The last part (125) is the order ID. Different vendors can have the same order ID, so my lookup needs to account for the vendor.
What I've done so far
I have written a series of formulas which extract parts of the string. For instance, for
12345(1234-COD-100 - 1234-COD-150)
I have extracted the following in individual cells
100
150
COD
1234-COD-100
1234-COD-150
I know I can create a series to manually populate each order ID within range, then perform a lookup but I have hundreds of values in List 2 so this isn't an option.
Another option is to take the value in List 2 (12345(1234-COD-100 - 1234-COD-150) and check if every number in the range 100-150 is found in List 1.
I would like to be able to use a formula (or even VBA) to achieve this, but haven't been able to get any further. Any help would be greatly appreciated!
Parsing text is always problematic. The following will return what you want:
=SUMPRODUCT((A2>=TRIM(LEFT(MID($C$2:$C$3,FIND("(",$C$2:$C$3)+1,LEN($C$2:$C$3)),FIND(" - ",MID($C$2:$C$3,FIND("(",$C$2:$C$3)+1,LEN($C$2:$C$3)))-1)))*(A2<=SUBSTITUTE(TRIM(MID($C$2:$C$3,FIND(" - ",$C$2:$C$3)+3,LEN($C$2:$C$3))),")","")))>0
But it depends exclusively on the pattern you are showing. Specifically that the range is always in () and the two ranges are separated by -

Using tbl.Lookup to match just part of a column value

This question relates to the Schematiq add-in for Microsoft Excel.
Using =tbl.Lookup(table, columnsToSearch, valuesToFind, resultColumn, [defaultValue]) the values in the valuesToFind column have a consistent 3 characters to the left and then varying characters after (e.g. 908-123456 or 908-321654 - i.e. 908 is always consistent)
How can I tell the function to lookup the value based on the first 3 characters only? The expected answer should be the sum of the results of the above, i.e. 500 + 300 = 800
tbl.Lookup() works by looking for an exact match - this helps ensure it's fast but in this case it means you need an extra step to calculate a column of lookup values, something like this:
A2: =tbl.CalculateColumn(A1, "code", "x => LEFT(x, 3)", "startOfCode")
This will give you a new column that you can use for the columnsToSearch argument, however tbl.Lookup() also looks for just one match - it doesn't know how to combine values together if there is more than one matching row in the table, so I think you also need one more step to group your table by the first 3 chars of the code, like this:
A3: =tbl.Group(A2, "startOfCode", "amount")
Because tbl.Group() adds values together by default, this will give you a table with a row for each distinct value of startOfCode and the subtotal of amount for each of those values. Finally, you can do the lookup exactly as you requested, which for your input table will return 800:
A4: =tbl.Lookup(A3, "startOfCode", "908", "amount")

Using VLookUp for a partial search

I have two tables in excel.
In table 1, one column contains a list of order numbers. This is done the format of XXXX-YYYY where X is an integer and Y is a letter. For example 3485-XTIP
Table 2 also has an order number column but this time it's in the format XXXX-YYYY (ZZ) where Z is the initials of the customer who made the order. Example: 3485-XTIP (KN)
How can I use a VLookUp to search for the order number in Table 2 but only using the XXXX-YYYY part? I tried using TRUE for an approximate search but it still failed for some reason.
This is what I have
=VLOOKUP("I3",'Table2 '!A:B,2,FALSE)
I am open to any alternatives other than VLookup for this situation.
Note that there are hundreds of order numbers and entering the strings manually will take forever.
You can use * as wildcard and add it at the end of the order number so that your VLOOKUP will match any order plus any other characters that come after it:
=VLOOKUP(I3&"*", 'Table2 '!A:B, 2, 0)
* will match anything after the order number.
Note: 0 and False have the same behaviour here.

Finding the next result from a MATCH

I am trying to produce a sorted table in excel, which depend on the selected year and category.
My methodology has been to sequentially find largest values in order, within the selected year and category parameters, doing the following:
Column E
{=LARGE(IF(('Master Data'!A$1:A$500 = $B$1) * ('Master Data'!B$1:B$500 = $B$2),'Master Data'!C$1:C$500), $B10)}
This works fine, $B$1$ is where I store the year, $B$2 is where I store the category, $B10 references a hard coded 1-25 in column B.
Column F
{=MATCH(E10,IF(('Master Data'!A$1:A$500 = $B$1) * ('Master Data'!B$1:B$500 = $B$2),'Master Data'!C$1:C$500),FALSE)}
This returns the row number of the result I need, which I then use in conjunction with INDEX to find related data.
The problem with this is that Match only returns the first row number, and if you have two results with the same value this clearly becomes an issue.
Column G
To resolve this I used an example from dailydoseofexcel which looks like this:
=IF(F10<>F11, F11, G10+MATCH(E11,INDIRECT("'Master Data'!C"&(G10+1)&":C500"),0))
This works to a limited extent, for my purposes, as it is unable to take into account the year and category filter I need to apply, so I tried:
{=IF(F10<>F11, F11, G10+MATCH(E11,IF((INDIRECT("'Master Data'!A"&(G10+1)&":A500") = $C$2) * (INDIRECT("'Master Data'!B"&(G10+1)&":B500") = $C$3), INDIRECT("'Master Data'!C"&(G10+1)&":C500")),0))}
But I am just getting #N/A as a result.
I think SUMPRODUCT may be what you are looking for:
Charley Kyd XL Legend: Use SUMPRODUCT to get the Last item in a list

Resources