Identify first occurrence of an entry in a list, if certain criteria is met (EXCEL) - 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.
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.
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)
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:
We merged Product Name & Company to identify unique records.
Then we created a condition to check for last 3 characters to match our column name: =IF(TRIM(RIGHT($D2,3))=E$1
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.
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 i.e. 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)
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

Related

Counting the max number of times a value occurs consecutively

I have a list of users with the number of times they have been file checked in a 12 month period. I want to identify (in column H) how many consecutive months there has been NO file check for that user. Eg :
A B C D E F G
User |Oct |Nov | Dec | Jan | Feb | Mar
A | 0 | 1 | 1 | 0 | 0 | 0
B | 1 | 1 | 0 | 0 | 1 | 0
C | 0 | 0 | 1 | 0 | 0 | 0
D | 2 | 0 | 0 | 0 | 1 | 1
Cell H2 should contain 3 as the there were no file checks on 3 consecutive months (Jan, Feb and March) for User A
Cell H3 should contain 2 as there were no file checks on 2 consecutive months (Dec, Jan) for User B
Cell H4 should contain 3 as the largest consecutive run of 0's is 3 (Jan, Feb, Mar)
Cell H5 should contain 3 as the there were no file checks on 3 consecutive months (Nov, Dec and Jan) for User D
I know a simple COUNTIF would give me the total number of 0's for each user, but I want to calculate how many consecutive months and, where there has been more than one 'block' of consecutive 0's, what the longest period is.
Inelegant, but it appears to work:
=MAX(IF(IFERROR(FIND(REPT("0",ROW($1:$6)),CONCAT($B2:$G2)),0),ROW($1:$6),0)) [Ctrl+Shift+Enter]
It iterates through the numbers yielded by ROW($1:$6) to find the maximum number of zeroes in the concatenation of your per-month values in each row. Enter it as an array formula into H2 and fill down.

I have data stored in excel where I need to sort that data

In excel, I have data divided into
Year Code Class Count
2001 RAI01 LNS 9
2001 RAI01 APRP 4
2001 RAI01 3
2002 RAI01 BPR 3
2002 RAI01 BRK 3
2003 RAI01 URE 3
2003 CFCOLLTXFT APRP 2
2003 CFCOLLTXFT BPR 2
2004 CFCOLLTXFT GRL 2
2004 CFCOLLTXFT HDS 2
2005 RAI HDS 2
where I need to find the top 3 products for that particular customer for that particular year.
The real trick here is to rank each row based on a group.
Your rank is determined by your Count column (Column D).
Your group is determined by your Year and Code (I think) columns (Column A and B respectively).
You can use this gnarly sumproduct() formula to get a rank (Starting at 1) based on the Count for each Group.
So to get a ranking for each Year and Code from 1 to whatever, in a new column next to this data:
=SUMPRODUCT(($A$2:$A$50=A2)*(B2=$B$2:$B$50)*(D2<$D$2:$D$50))+1
And copy that down. Now you can AutoFilter on this to show all rows that have a rank less than 4. You can sort this on Customer, then Year and you should have a nice list of top 3 within each year/code.
Explanation of sumproduct.
Sumproduct goes row by row and applies the math that is defined for each row. When it is done it sums the results.
As an example, take the following worksheet:
+---+---+---+
| | A | B |
+---+---+---+
| 1 | 1 | 1 |
| 2 | 1 | 4 |
| 3 | 2 | 2 |
| 4 | 4 | 1 |
| 5 | 1 | 2 |
+---+---+---+
`=SUMPRODUCT((A1:A5)*(B1:B5))`
This sumproduct will take A1*B1, A2*B2, A3*B3, A4*B4, A5*B5 and then add those five results up to give you a number. That is 1 + 4 + 4 + 4 + 1 = 15
It will also work on conditional/boolean statements returning, for each row/condition a 1 or a 0 (for True and False, which is a "Boolean" value).
As an example, take the following worksheet that holds the type of publication in a library and a count:
+---+----------+---+
| | A | B |
+---+----------+---+
| 1 | Book | 1 |
| 2 | Magazine | 4 |
| 3 | Book | 2 |
| 4 | Comic | 1 |
| 5 | Pamphlet | 2 |
+---+----------+---+
=SUMPRODUCT((A1:A5="Book")*(B1:B5))
This will test to see if A1 is "Book" and return a 1 or 0 then multiple that result by whatever is B1. Then continue for each row in the range up to row 5. The result will 1+0+2+0+0 = 3. There are 3 books in the library (it's not a very big library).
For this answer's sumproduct:
So ($A$2:$A$50=A2) says to return a 1 if A2=A2 or a 0 if A2<>A2. It does that for A2 through A50 comparing it to A2, returning a 1 or a 0.
(B2=$B$2:$B$50) will test each cell B2 through B50 to see if it is equal to B2 and return a 1 or 0 for each test.
The same is true for (D2<$D$2:$D$50) but it's testing to see if the count is less than the current cells count.
So... essentially this is saying "For all the rows 1 through 50, test to find all the other rows that have the same value in Column A and B AND have a count less than this rows count. Count all of those rows up that meet that criteria, and add 1 to it. This is the rank of this row within its group."
Copying this formula has it redetermine that rank for each row allowing you to rank and filter.

Excel - Finding Cells with Top Values

| Company 1 | Company 2
Item 1 | 7 | 0
Item 2 | 1 | 0
Item 3 | 2 | 8
Item 4 | 0 | 3
Item 5 | 0 | 1
So I need to grab the cell/row number for the top 3 ordered values in a given column so for Company 1 you'd get B2, B4, B3 and for Company 2 you'd get C4, C5, C6.
To return the row number use this formula:
=MATCH(LARGE(B$2:B$6,ROW(1:1)),B:B,0)

Complete Truth Tables Based On Binary

I am trying to figure out names for every combination with truth tables.
In the first table, I have each truth table for a two input and one output system. The inputs are read by row. The outputs are in a binary counted format. Each Output is read by column and is labeled with a hex number 0 to F. The input by row is related to the outputs within the specified output column.
In the second table, I have listed by row how each output column on the first chart works. In each row I have listed the binary logic gate name, if statement in javascript, and a description for how each would work. I have a hyphen for spaces that are not complete.
Are there names for the blank spaces in the gate names in the second table?
Complete Truth Tables
Inputs | Outputs
1 2 | 0 1 2 3 4 5 6 7 8 9 A B C D E F
-----------------------------------------
0 0 | 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
0 1 | 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1
1 0 | 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1
1 1 | 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
Num | Gate | Javascript | Return True If
--- | ----- | ---------- | --------------
0 | - | 0 | FALSE
1 | AND | I1&&I2 | I1 AND I2
2 | - | I1&&!I2 | I1 AND NOT I2
3 | - | I1 | I1
4 | - | !I1&&I2 | I2 AND NOT I1
5 | - | I2 | I2
6 | XOR | I1!==I2 | I1 NOT EQUALS I2
7 | OR | I1||I2 | I1 OR I2
8 | NOR | !I1||!I2 | NOT I1 OR NOT I2
9 | XNOR | I1==I2 | I1 EQUALS I2
A | - | !I2 | NOT I2
B | - | !(!I1&&I2) | NOT ( I2 AND NOT I1 )
C | - | !I1 | NOT I1
D | - | !(I1&&!I2) | NOT ( I1 AND NOT I2 )
E | NAND | !I1&&!I2 | NOT I1 AND NOT I2
F | - | 1 | TRUE
Some of the other combinations have gate names, but not all do.
The A and C cases are each an example of a NOT gate, and the 3 and 5 cases are each an example of a BUFFER.
The D case is known as an IMPLY gate, but this is not as commonly known as the others.
For the rest, there are no commonly used gate names because to implement their boolean function would require either no gates (as in TRUE and FALSE), or they would require a combination of two or more of the conventional gates that you have already identified. There may be specific implementations of tools or systems that have created names for these "quasi-gates", but they are not in common use.
See Also
Logic Gate (Wikipedia)
Imply Gate (Wikipedia)

Why doesn't this formula work?

Consider the following (partial) Excel sheet:
| A | B C D
-+---+------------------
1|id | comp1 comp2 comp3
-+---+------------------
2| 1 | 1 0 0
3| 1 | 1 0 0
4| 2 | 0 1 0
5| 3 | 0 0 1
6| 1 | 1 0 0
7| 3 | 0 0 1
As an example, if filled in all zero's and one's in columns B, C and D by hand. I need to automate this because my dataset is to large to do it all manually. I need, for instance for cell B2, a formula which checks whether value of A2 is similar to company id in cell B1. If so, set a 1, else a 0. Another example: take row 4. It has company id 2 in cell A4 so B4 (comp1) should be 0, C4 1 and D4 0.
I have used the following formula in cell B2, ready to be dragged to all other cells:
=IF($A2=RIGHT(B$1;1);1;0)
However, it puts a 0 in B2. I don't understand, because A2 has value 1, and so does RIGHT(B1;1), so IF(1=1;1;0), so B2 should get value 1. But it doesn't where am I going wrong?
Because right() returns a string and you compare a string to a number. You need to transform that string to a number, e.g. with value(right(...)).

Resources