Count If with multiple criteria and unique values - excel

I have a sheet of data and want to run a count on the values in Raw data with the following criteria:
If the value in raw data is repeated FOR EXAMPLE London No1 and London No1 AUTO only to be counted once, but if the value in raw data is London No1, London No2 and London No1 Auto then only to count 2 (to count London No1 and London No2 only).
Raw data
London No1
London No2
London No1 Auto
London No2 Auto
Bristol No1 Auto
Cardiff No1
Cardiff No2
In this example Result should be: London - 2, Bristol - 1, Cardiff - 2.

This solution uses only one "working column" and assumes the following:
The list of Raw Data is located at A1:A16 (adjust as required)
The Raw Data records follow this syntax City NoX string all separated by a blank space. X is one digit number (do let me know if there will be larger numbers in order to adjust the formula)
(All ranges include header)
With the list of results located in the same worksheet at B1:D16
(adjust as required) and including the following fields:
Site: List of Cities to count in Raw Data
Unique Record: Used to list unique "clean" city records before counting. Enter this FormulaArray in D2 and copy till last record
(Formulas Array are entered by pressing [Ctrl] + [Shift] + [Enter]
simultaneously)
=IFERROR( INDEX(
MID( $A$2:$A$16, 1, 2 + FIND( "No", $A$2:$A$16 )),
MATCH( 0, COUNTIF( D$1:D1,
MID( $A$2:$A$16, 1, 2 + FIND( "No", $A$2:$A$16 ))), 0 ) * 1 ), "" )
Result Count: Enter this FormulaArray in C1 and copy till last record
=IF( EXACT( $B2, "" ), "",
IFERROR( COUNTIF( $D$2:$D$16, $B2&"*" ), "" ))

Assumptions
So if I understand your clarifications, you have a list of cities, and each city might have a different "No" on it. ie: some will be "No1", "No2", etc. Also, some might have "Auto" on it. There are sometimes duplicates, ie: "London No1" might appear twice. Also, whether or not the word "Auto" appears, it will be a duplicate if that "No" already appears. ie: "London No1" is identical to "London No1 Auto".
Helper Columns
To count how many unique instances of each city there are, I propose you use a helper column which first removes the word auto, and then only displays itself if it is not a duplicate of a prior entry.
With your data in column A, starting at A2, enter this formula in B2 and copy down:
=IF(ISERROR(MATCH(TRIM(SUBSTITUTE(A2,"Auto","")),$B$1:$B1,0)),TRIM(SUBSTITUTE(A2,"Auto","")),"")
This formula works as follows:
-First it takes the text in A2, and it replaces the word "Auto" with "". Then it TRIMS the resulting text, which means it earases any trailing spaces at the end. It takes that trimmed text, and attempts to MATCH it with the previous rows in column B. If it doesn't find a match, it results in an error. IF it ISERROR, then it shows the text (because it's the first time it has appeared). Otherwise, it shows "".
Array Formula
Now that you have this in column B, we will use Array Formulas to count how many unique instances of each city appeared. An Array Formula is a specific formula which calculates many time over an entire range, instead of just one cell. It then returns an Array of results, which we need to collapse into a single answer. For example:
=SUM(IF(A1:A5>5,B1:B5,""))
[Confirmed with CTRL + SHIFT + ENTER instead of just ENTER]
In the above example, the Array Formula will check if each individual cell from A1:A5 is a number larger than 5. If it is, it will return the cell from column B in that row. It then takes all resulting answers from column B (where A was > 5), and SUMS them to a single number.
In this case, the Array Formula will be as follows [Starting in D5 in your image and moved one column to the right, immediately to the right of the first city name, and copied down for each city; I am assuming that your raw data goes from A2:A100]:
=SUM(--NOT(ISERROR(SEARCH(C5,$B$2:$B$100))))
Confirm this formula with CTRL + SHIFT + ENTER instead of just ENTER. Then copy down for each city name in column C.
It works by attempting to search each unique value in column B for the city name shown in that row of column C. Wherever it the city name is not present, ISERROR returns TRUE for that element of column B. Then NOT turns all TRUEs to FALSEs and vice versa. Then the -- tells excel to convert TRUE and FALSE to their numerical equivalents [1 and 0, respectively]. So we will have a TRUE [AKA 1] for each match, and FALSE [AKA 0] for everything else. The sum of all 1's is the number of matches of that city name in your list of unique entries.

Related

Auto Increment the value of a cell based on an adjacent sell value plus search last number increment by 1

Ok I have 2 excel columns
1st column A "Workstream", is a data list with three numbers as a dropdown. 1,2,3
2nd column B "ID", would like to auto-populate based on the selection made from the left adjacent cell + perform a lookup to get the MAX number in the current column and ADD by 1.
For Example:
Workstream
ID
1
W1-001
1
W1-002
1
W1-003
1
W1-004
2
W1-001
1
W1-005
2
W1-002
So when a user selects from the drop-down in column A then Column B auto-populates with something like this
="W"&A:1&"-"
However, in order to complete the value, it needs to do the following:
="W"&A:1&"-" Search for the Max Record in Column B that starts with 1 or whatever value was entered into Column A, then include the next number based on the MAX value selected in Column A
So in the above example, let's say I Enter "2" in column A, then the value that auto-populates in column B would be
| 2 | W2-003
or if I selected 1 from column A given where we left off then the value that would auto-populate in column B would be:
| 1 | W1-006
If I am understanding correctly and you want the format to be "W" followed by number of the workstream (as inferred from the text of your question) try:
="W"&A2&"-"&TEXT(COUNTIF(A$2:A2, B2), "000")
If instead you want the output exactly as shown in the picture you provided, it's even easier:
="W1-"&TEXT(COUNTIF(A$2:A2, B2), "000")
EDIT: You might consider pre-dragging the formula to all the rows that you think have the possibility of being impacted so that you don't have to drag the formula each time you add a row. In that case, try:
=IF(A2="","", "W"&A2&"-"&TEXT(COUNTIF(A$2:A2, B2), "000"))

Excel: Find the Position/Location of Each Occurrence of a Specific Word in a String in a Column

Using an MS Excel formula, I would like to find the Position(s)/Location of specific words found within a String of text located in a Range/Column of cells.
I'm using a formula that only identifies and finds the position(s) of a keyword by a single cell versus a column. I'm not able to repeat this action by looking throughout the column cells, using my numeric helper column (Cell D2:D12 "Occurrence") which provides the occurrence of the next position to be found.
Helper columns are welcomed if necessary to achieve the desired results.
The cells highlighted in "Red" is what I'm looking for as the final output results.
See below for formulas used for Column C and D. Text string is located in Column A2:A12.
COLUMN A
DATA TEXT
Dolly would not count her eggs but Count her apples
Tony drove a pickup truck to work
Over many nights he could not sleep
Only this and nothing more
She went by to pickup her son
To many times he would count over
They went shopping for Christmas
You can count him to pickup someone
They count so much they would sleep in his pickup
Nobody would play with Timmy
Trying to find the position location of each word
COLUMN B
Keyword List
Toe
Shoe
Count
Pumpkin
Pickup
Randy
Sally
Sleep
Jonathan
C2: =SUMPRODUCT((LEN($A$2:$A$12)-LEN(SUBSTITUTE((UPPER($A$2:$A$12)),UPPER(B2),"")))/LEN(B2))
D2&E2: =FILTER(B2:C10,C2:C10>0)
F2: =IF(E2="","",REPT(D2&"^",E2))
G2: =TEXTJOIN("",TRUE,F2:F4)
H2: =TRIM(MID(SUBSTITUTE($G$2,"^",REPT(" ",LEN($G$2))),(COUNTIF($H$1:H1,"<>&""")-1)*LEN($G$2)+1,LEN($G$2)))
I2: =IF(H2="","",IF(COUNTIF($H$2:H2,H2)>1,SUM(I1+1),1))
Try the below picture set up and formulas solution as in.
1] C2 "Total occurrence", formula copied down :
=SUMPRODUCT(LEN($A$2:$A$12)-LEN(SUBSTITUTE(LOWER($A$2:$A$12),LOWER(B2),"")))/LEN(B2)
2] D2 "Count", formula copied across to F2"Sleep" and all copied down :
=SUMPRODUCT(LEN($A2)-LEN(SUBSTITUTE(LOWER($A2),LOWER(D$1),"")))/LEN(D$1)
3] G2 "Keywords" , formula copied down:
=LOOKUP(ROW(A1),SUMIF(OFFSET(C$1,,,ROW($1:$12),),"<>")+1,B$2:B$4)&""
4] H2 "Occurrence", formula copied down:
=IF(G2="","",COUNTIF(G$2:G2,G2))
5] I2 "Content Keyword Data Text", formula copied down:
=IF(G2="","",LOOKUP(H2,SUMIF(OFFSET(INDEX($1:$1,MATCH(G2,$1:$1,0)),,,ROW($1:$12),),"<>")+1,$A$2:$A$12))
6] J2 "Position", formula copied down:
=IF(G2="","",FIND("~",SUBSTITUTE(LOWER(I2),LOWER(G2),"~",COUNTIFS(G$2:G2,G2,I$2:I2,I2))))

Vlookup when lookup cell has string

Trying to figure this out
Say cell A1 has unique values "Car | Bike | Truck ", I would like to use Vlookup to match this to a table that has values for one of the unique values
Category Value
Shoes 1
Bike 2
Hat 3
Coat 4
Example above would return 2
There could be instances where the values dont exist in the table, which is OK. Also, I can change " | " to commas if needed, it's just how the output file currently is.
Thanks!
Consider the Array Formula:
=INDEX(D2:D5,MATCH(TRUE,ISNUMBER(SEARCH(C2:C5,A1)),0))
With data like:
Array formulas must be entered with Ctrl + Shift + Enter rather than just the Enter key.

Extracting all rows based on a value of cell without VBA

I'm really struggling to find an answer to this as online I've really only found VBA solutions to this problem which isn't what I wish to learn how to do.
THE PROBLEM
BLOOD NAME AGE GENDER
A David 18 Male
B Sarah 22 Female
O Lucy 32 Female
AB Steven 23 Male
O John 11 Male
B Mike 25 Male
AB Paul 24 Male
O Amy 23 Female
B Drake 22 Female
O Linda 11 Female
Very simply from the above dataset I wish to recreate this range but filter for only select BLOOD TYPE O.
MY ATTEMPTS
Started with a VLookup table however that stops at the first occurrence of O. Then tried incorporating IF/THEN/ELSE logic into a MATCH operand trying to locate the row numbers outputting to an array. (not gonna post my failed attempts) I did find a similarish problem online however they solved it via referencing the range manually using ROW(A1), ROW(A2) etc etc wasn't what I after.
Really want to learn how to do this type of iterative selections using Excel formulae only. Even if not solving the problem any direction towards resources where I can learn more about this type problem, would be still appreciated.
This does not use array formulas, but does use a helper column. Assuming data in cols A through D, in E2 enter:
=IF(A2="O",1+MAX($E$1:E1),"")
and copy down:
Each of the O rows is marked with a simple sequential value. This makes it easy for the usual MATCH() / INDEX() methods.
Pick some other cell and enter:
=IFERROR(INDEX(A:A,MATCH(ROWS($1:1),$E:$E,0)),"")
and copy this cell both across and down:
Here is a solution with array formulas. It will calculate extremely slowly, and honestly VBA is a much better solution. You will need to tell excel these are array formulas by hitting "Ctrl + Shift + Enter" after inputting the formulas, this will add the {} around the equation. Finally, drag down the array formulas to see the first "X" results with blood type "O":
First cell formula for "Blood" --> assumes blood is in column A of sheet1
{=IFERROR(INDEX(Sheet1!$A:$D,SMALL(IF(Sheet1!$A:$A="O",ROW(Sheet1!$A:$A)),ROW(1:1)),1,1),"")}
First cell formula for "Name" --> assumes name is in column B of sheet1
{=IFERROR(INDEX(Sheet1!$A:$D,SMALL(IF(Sheet1!$A:$A="O",ROW(Sheet1!$A:$A)),ROW(1:1)),2,1),"")}
First cell formula for "Age" --> assumes age is in column c of sheet1
{=IFERROR(INDEX(Sheet1!$A:$D,SMALL(IF(Sheet1!$A:$A="O",ROW(Sheet1!$A:$A)),ROW(1:1)),3,1),"")}
First cell formula for "Gender" --> assumes gender is in column d of sheet1
{=IFERROR(INDEX(Sheet1!$A:$D,SMALL(IF(Sheet1!$A:$A="O",ROW(Sheet1!$A:$A)),ROW(1:1)),4,1),"")}
Results:
BLOOD NAME AGE GENDER
O Lucy 32 Female
O John 11 Male
O Amy 23 Female
O Linda 11 Female
The following array formula can be put in row 2 (anywhere from column E onward) and copied across 3 columns and down as far as is necessary:
=IFERROR(INDEX(A:A,SMALL(IF(ISNUMBER(SEARCH("O",$A$2:$A$11)),ROW($A$2:$A$11),""),ROW()-1)),"")
This is entered using Ctrl + Shift + Enter and uses a fixed array (A2:A11). If your array is going to change size, you can make the reference to it dynamic by using INDIRECT and COUNTA so that it always encompasses the used range, like so:
=IFERROR(INDEX(A:A,SMALL(IF(ISNUMBER(SEARCH("O",INDIRECT("$A2:$A"&COUNTA(A:A)))),ROW(INDIRECT("$A2:$A"&COUNTA(A:A))),""),ROW()-1)),"")
What is happening:
The SEARCH function is looking for "O"s, then the IF returns the row number if an "O" was found and nothing if no "O" was found.
The SMALL function is looking for the nth instance of the results returned by the SEARCH function, where n = ROW()-1.
The INDEX function returns the nth value from the array A:A, B:B, etc, where n = the row number returned by the SMALL function.
The IFERROR function is not necessary but it makes for a cleaner dataset, all it does is replace the formulas that didn't return anything useful with a blank instead.
Try this standard formula:
= IFERROR( INDEX( A$2:A$11, AGGREGATE( 15, 6,
ROW($A:$A) / ( $A$2:$A$11 = "O" ), ROWS( $A$2:$A2 ) ) ), "" )
or if you want to limit the size of size of column A
= IFERROR( INDEX( A$2:A$11, AGGREGATE( 15, 6,
ROW($A$1:$A$11) / ( $A$2:$A$11 = "O" ), ROWS( $A$2:$A2 ) ) ), "" )
Enter the formula in G2 then copy it to G2:J10

Excel Find Nth Instance of Multiple Criteria

I have 3 columns of data. Col A contains Names, Col B contains a client ID, Col C contains a date.
I'm trying to figure out how to write a formula that will find the top 2 and top 3 instances of a specific Name in Col A and client ID in Col B and return the value in Col C.
Trying to avoid using VBA, but not sure if this is doable.
So for example data looks like this and I would want to return that Sam dealt with Client ABC the 2nd time around on 12/16.
Sam ABC 12/3
Adam XYZ 12/5
John DEF 12/9
Sam ABC 12/16
Adam HIJ 12/18
Assuming
your headers are in A1:C1
your data starts from A3 (yes, not A2)
You enter the name in G2 & Client ID in G3 & you want the list of
dates starting from G5
Enter these formula/values:
A2: =G2
B2: =G3
C2: =0
G5:
=IFERROR(INDEX(($A$2:$A$500=$G$2)*($B$2:$B$500=$G$3)*($C$2:C$500),MATCH(0,COUNTIF($G$4:G4,($A$2:$A$500=$G$2)*($B$2:$B$500=$G$3)*($C$2:C$500)),0)),"End")
(Formula in G5 is an array formula; confirm this with Ctrl+Shift+Enter)
Drag the formula in G5 down until you see 'End'
Value in cell G5 will always be 0 or '1/0' based on your formatting.
The list of dates corresponding to the name & client ID combination will start from G6.
Let me see if I understood your need. Correct me if I'm wrong.
You want to be able to inform a Name and a Client ID and have Excel tell you the last 3 occurrences of that combination?
By "top 2 and top 3 instances of a specific name" I'm assuming you mean the top 2 and 3 dates found for that specific name and ID.
If so, try this:
Supposing you have your example data table starting at Cell A1 and ending at Cell C6 (including column headers) and that you'll enter the name in F1 and Client ID on F2
A B C
1 Name Client ID Date
2 Sam ABC 12/3
3 Adam XYZ 12/5
4 John DEF 12/9
5 Sam ABC 12/16
6 Adam HIJ 12/18
Type this formula where you want to return the date of the last occurrence:
=IFERROR(LARGE(IF($A$2:$A$6=$F$1,IF($B$2:$B$6=$F$2,$C$2:$C$6)),1),"-")
This should be entered as an Array Formula, so don't forget to press CTRL + SHIFT + ENTER or it'll not work.
To bring the 2nd last occurrence on another cell, just copy and paste the formula and change the number 1 to 2 (as indicated below):
=IFERROR(LARGE(IF($A$2:$A$6=$F$1,IF($B$2:$B$6=$F$2,$C$2:$C$6)),2),"-")
If you typed 'Sam' on F1 and 'ABC' on F2, this formula would return '12/16' as the last occurrence, '12/3' as the 2nd last occurrence and a dash (-) as the 3rd last occurrence, since there isn't one.
Of course, you'll have to adjust the ranges and other cell references accordingly in your real data set.
Hope this helps.

Resources