简体   繁体   中英

Identify first occurrence of an entry in a list, if certain criteria is met (EXCEL)

I have spent too much time trying to figure our the question imposed, in the title of this question, and am reaching out for some help.

I would like to output an entry of 1 for the first occurrence when criteria is equal to AR and output an entry of 1 for the first occurrence on a column when criteria is equal to SFR on a seperate column.

For example the columns I'd want in the following table are the columns titled AR and SFR

|Property Name  | product | company | AR | SFR |
|---------------|---------|---------|----|-----|
|orange grove   |    2    |   SFR   | 0  |  1  |
|orange grove   |    1    |   AR    | 1  |  0  |
|orange grove   |    6    |   AR    | 0  |  0  |
|garden court   |    2    |   SFR   | 0  |  1  |
|garden court   |    1    |   AR    | 1  |  0  |
|chimney sweeps |    6    |   AR    | 1  |  0  |
|chimney sweeps |    2    |   SFR   | 0  |  1  |
|chimney sweeps |    1    |   AR    | 0  |  0  |
|chimney sweeps |    4    |   SFR   | 0  |  0  |
|downing apts   |    2    |   SFR   | 0  |  1  |
|downing apts   |    1    |   SFR   | 0  |  0  |
|downing apts   |    6    |   AR    | 1  |  0  |

I tried implementing countif within my formula and only taking into consideration when my output is a result of 1, then nesting the formula for criteria "AR" or "SFR" with the result of my countif, if the criteria is satisfied, but the table isn't organized so that AR comes first in the table row and vice versa. Here is an example of my formula:

=IF(E2="AR", IF(COUNTIF($C$2:C2,C2)=1,1,""), "")

Sometimes I'll have instances where the company name occurs but the count isn't 1. Take for example orange grove . Orange grove's count would be 1, 2, 3 consecutively, for each +1 instance. If my count is 1 and SFR , my formula would output a 1 but if my criteria is AR and the count is not 1 (it's actually 2 or 3), I don't get a first occurrence output per my criteria of AR .

I saw there may be a way to utilize sumproduct but I am not familiar with it.

Could anyone please help. Anything would be appreciated!

It would guanrantee you a result if you could CONCATENATE the Property Name and Company and then input the below mentioned formula into AR and SFR cells.

  1. Insert a column and use =CONCATENATE(A2," ",C2) to merge Product Name and Company. It should give you result in space delimited format. For eg. orange grove SFR, chimney sweeps AR, etc.
  2. Enter this formula in Cell E2 which belongs under AR column: =IF(TRIM(RIGHT($D2,3))=E$1,IF(OR(COUNTIF($D:$D,$D2)=1,MATCH($D2,$D:$D,0)>=ROW($D2)),1,0),0)
  3. Drag the formula left onto Cell F2 to apply to SFR column and then to the bottom of each columns to apply for all records/rows.

In the end you will have the following result as expected:

Table:

R/C   A               B         C        D                    E     F

01    Property Name   product   company  Merged PN + C        AR    SFR
-------------------------------------------------------------------------
02    orange grove    2         SFR      orange grove SFR     0     1
03    orange grove    1         AR       orange grove AR      1     0
04    orange grove    6         AR       orange grove AR      0     0
05    garden court    2         SFR      garden court SFR     0     1
06    garden court    1         AR       garden court AR      1     0
07    chimney sweeps  6         AR       chimney sweeps AR    1     0
08    chimney sweeps  2         SFR      chimney sweeps SFR   0     1
09    chimney sweeps  1         AR       chimney sweeps AR    0     0
10    chimney sweeps  4         SFR      chimney sweeps SFR   0     0
11    downing apts    2         SFR      downing apts SFR     0     1
12    downing apts    1         SFR      downing apts SFR     0     0
13    downing apts    6         AR       downing apts AR      1     0

Explanation:

  1. We merged Product Name & Company to identify unique records.
  2. Then we created a condition to check for last 3 characters to match our column name: =IF(TRIM(RIGHT($D2,3))=E$1
  3. If the above condition is true which means that we are in the correct Company column, we can now proceed to check if the Product Name instance is first or not. If not, then we are in the wrong column so just print 0.
  4. As you mentioned there can be either a unique combination or multiple combinations of Product Name & Company. Therefore there are 2 conditions: a. When combination is unique ie count is equal to 1: COUNTIF($D:$D,$D2)=1 b. When combination is duplicate but row number of first instance is less than equal to current row number: MATCH($D4,$D:$D,0)>=ROW($D4)
  5. If either of the above 2 conditions are TRUE then the record is first instance and hence print 1, else it is not and hence print 0.

Hope it helped.

Assuming Column D and E are where your AR and SFR columns are respectively, try:

Table Structure:

    AR  SFR
AR  1   0
AR  0   0
AR  0   0
AR  0   0
AR  0   0
SFR 0   1

Cell D2 Would be (assuming AR is your column Header for column D)

=IF(C2=D1,1,0)

E2 Would be:

=IF(C2=E1,1,0)

For D3, then drag down for each cell in column D

=IF(SUM(D$2:D2)=0,IF($C3=D$1,1,0),0)

For E3, then drag down for each cell in column E

IF(SUM(E$2:E2)=0,IF($C3=E$1,1,0),0)

Thanks!

Ryan

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM