VLOOKUP with criterion of max - excel

lets say I have a Table1 as follow:
ID | Value
________________
1 | 0
2 | 0
1 | 1
3 | 1
1 | 0
2 | 0
1 | 0
2 | 0
3 | 0
4 | 1
1 | 0
5 | 0
and I have a second table that contains unique IDs from Table1.
In Table1 ID may repeat, but each ID can have at most one 1 in Value column, the rest is 0.
How can I write VLOOKUP like formula that will tell me if given ID has 1 in any occurence?
I would like to get smth like
ID | Value
________________
1 | 1
2 | 0
3 | 1
4 | 1
5 | 0
with SQL I would write smth as SELECT ID, max(Value) from Table1 group by ID, or even instead of max would use sum.
Also to mention: Table1 will be in separate file from my output table and the Value will be just one of many columns, therefore I cannot use Pivot Tables

I think the solution is easier than you might think:
=SUMIFS(B$2:B$13,A$2:A$13,1)
What are you doing? You are summing everything? I just want to know where the 1 is, no need to sum it?
Well: you seem to have two possible values: either all 0's, either all 0's and just one 1: if you search for that 1, or if you take the sum, the result is the same :-)
Ok, that's a neat trick, but what if I decide there might be more than one 1?
Well: just translate a number, larger than 1, to 1, which you can do with this formula:
=IF(E2,1,0)

There are several ways to go about it, and I'm assuming that your values are more complicated than your example, so here is one way:
=MAX(IF(A$2:A$13=E3,B$2:B$13))
Where A2:A13 is your IDs, B2:B13 is the value, and E3 is the start of your reference table. This is an array formula and needs to be confirmed with CTRL+SHIFT+ENTER
If it's as simple as 1 or 0, you should use the answer that #dominique gave.

Give a try on the following formula-
=HSTACK(UNIQUE(A2:A13),MAXIFS(B2:B13,A2:A13,UNIQUE(A2:A13)))
This will work like SQL. It will also work if you have more values than one.

Related

Excel Count Distinct where other columns match and sum of another column = 0

I need to answer the question of how many parts were successful by counting the distinct Part Labels where PartName matches, and the sum of LabelFailures = 0.
PartName | PartLabel | LabelFailure
---------+-----------+-------------
a | 1 | 1
a | 1 | 0
a | 2 | 0
a | 2 | 0
b | 1 | 0
Desired Results:
PartName | PartsLabelSucceeded
---------+--------------------
a | 1
b | 1
This question might be similar to these two, but I'm having a hard time holding the individual components in my head to apply the answers to this particular situation. I've been trying to use COUNTIFS, but haven't found a way to fit both criteria in correctly.
Excel Count Unique Values on Multiple Criteria
Excel Count Distinct Values with Multiple Criteria
Use a helper column.
In D2 put:
=(COUNTIFS($A$1:A2,A2,$B$1:B2,B2)=1)*(COUNTIFS(A:A,A2,B:B,B2,C:C,1)=0)
Then insert a pivot table with PartName as the Rows And Count in the Values.
OR List the PartName manually and use SUMIFS:
=SUMIFS(D:D,A:A,G2)
This can also be done with a formula, without the helper if one wants to list the partNames:
=SUMPRODUCT((($A$2:$A$6=F2)*(COUNTIFS(A:A,$A$2:$A$6,B:B,$B$2:$B$6,C:C,1)=0)/(COUNTIFS(A:A,$A$2:$A$6,B:B,$B$2:$B$6)+($A$2:$A$6<>F2)+(COUNTIFS(A:A,$A$2:$A$6,B:B,$B$2:$B$6,C:C,1)>0))))

Microsoft Excel- Invert a 0/1 value or leave empty if no value

I'm ran into an issue while trying to do some work in an excel doc. I have created a column to count the number of flags in other columns. One of the flags I created simply inverts the value of another flag. I'm aware there's no reason for the new column if all I'm doing is inverting the value of a column that already exists, but space isn't an issue and in this case it's much easier for to create a 2nd column to keep things easier to understand.
The new column I created is based off a "consent given" column where the value is 1 if consent has been given, 0 if it has not been given, or empty if we don't have that information. I want my flag counter to count when consent has NOT been given, but I can't figure out a formula to set the value as 0 if consent has been given, 1 if it has not been given, or empty if no information. I can easily say 1 for no consent and 0 for consent or no information, but I want the no information columns to be empty. I can't use an empty string in the case of no information because that breaks my flag counter sum column.
Here is some example data of how I would like everything to be-
A | B | C | D
-------------------------------------------------------------
flag counter | consent given | consent not given | other flag
-------------------------------------------------------------
0 | 1 | 0 | 0
1 | 1 | 0 | 1
1 | 0 | 1 | 0
2 | 0 | 1 | 1
1 | | | 1
0 | | | 0
Formulas I am currently using (all formulas only look at cells in their same row- i.e. A1 uses B1, C1, D1, while A2 uses B2, C2, D2. I'll list A1 below for simplicity):
A1: =C1+D1
B1: This is raw data. Values are 1, 0, or empty
C1: This is what I'm looking for. I want something along the lines of =IF(B1=1, 0, IF(B1=0, 1, <empty>)) where <empty> is a pure empty that doesn't break the addition formula in column A (empty string breaks the formula). I've found formulas that leave the empty string or a 0, and either of those work in this instance. I want 1 -> 0, 0 -> 1, no value -> no value.
D1: This is raw data. Values are 1, 0, or empty
use ABS:
=IF(B2="","",ABS(B2-1))
And then in Column A sue sum that will ignore the text.
=SUM(C2:D2)

Generate new table from a current table in excel

I have sample table like this:
ID | 1 | 2 | 3
-------------
1 | 0 | 1 | 0
--------------
2 | 1 | 1 | 1
Then I want to generate a new table from that table. It will take the second row (1) then compare with each column (1, 2, 3) then print value of the matrix ( 0 - 1 - 0 ). For example:
Row_ID | Column_ID | Value
--------------------------
1 | 1 | 0
--------------------------
1 | 2 | 1
--------------------------
1 | 3 | 0
--------------------------
2 | 1 | 1
--------------------------
2 | 2 | 1
--------------------------
2 | 3 | 1
I'm not sure how or where to start by using formula. Please help. Thanks,
Well. There's no single formula that's going to do the job, obviously, but we have a few options we can use. I'll assume that the new table is going to start in cell A1 of Sheet2. Adjust accordingly.
Start with manually entered headers
Row_ID | Column_ID | Value
In the first column, first row, enter a 1. In rows below, use this formula: =IF(B3<B2,A2+1,A2) This will increment the value in the first column by 1 each time the second column resets its numbering.
In the second column, first row, enter a 1. The formula we'll use for this one will need some tweaking, but the basic version is: =IF(MOD(ROW()**+1**,**3**)=0,1,B2+1)
This formula is going to essentially count up to a certain point, then reset its numbering. The point it will count to, and where it will reset, will vary depending on the amount of data you have and which row you're starting from. Replace the 3 with the number of data columns you have, and remove the **s. The +1 is needed to increase the Row() counter to the SAME NUMBER as your number of data columns. So in my example, with 3 data columns and starting on row 2, the ROW() function gives us 2, so we need to add 1 to that to get up to a total of 3. If I had 5 data columns, I would add 3 to the total. Hope that makes sense.
These two formulae should give you a set of row and column numbers. Copying the formula down will force the values to increase as needed, thus:
Row_ID | Column_ID | Value
1 | 1 |
1 | 2 |
1 | 3 |
2 | 1 |
...etc.
Finally, to bring in the values, we'll use an OFFSET formula in the Value column: =OFFSET(Sheet1!$A$1,A2,B2) That formula starts from a reference cell - A1, in this case - then moves down x number of rows and across y number of columns to return a value. X and Y are provided by the formulas we already have. Your final structure will be something like this:
Row_ID | Column_ID | Value
1 | 1 |=OFFSET(...
=IF(...|=IF(MOD(...|=OFFSET(...
I hope all that made sense. Please let me know if there's anything that doesn't, and I'll try to troubleshoot.
EDITED TO ADD:
If the Row ID is something like a key that needs to be included with each value, we can get that fairly easily. We'll include another column with a slightly modified OFFSET formula: =OFFSET(Sheet1!$A$1,A2,0)
With this version of the formula we're not changing the column as we go down, just the row when it changes. It allows the values in the first row to be repeated in every row of the table. So this is my input:
And this is my output:
Notice that the ID repeats on each line of the output for the same item.

Pivot table to return the FIRST value in a range

Glad to be joining the forum.
My question deals with attempting to return the FIRST value that occurs over several columns of data, using a pivot table that is filtered within a narrow time range. My current pivot table works by counting values in each column over the time rows. However I'm really only interested in the FIRST value that I come across for each person. So the raw looks something like this:
Person|TimeValue|Variable1|Variable2
1 | 1 | 1 | 0
1 | 2 | 1 | 0
2 | 1 | 1 | 0
2 | 2 | 0 | 1
What I currently get for a pivot using a range of time1 to time 2 is
1 | |2 | 0
2 | |1 | 1
Clearly, the time range I select includes MULTIPLE values in the same column, leading to counts of >1. What I'm thinking is that there is a way to use the same time sorting, but count only the FIRST time a value occurs in that variable, so that the pivot reports only the first time a value occurs within the range for the variables of interest.
Is there a simple way, or am I going to have to do this in VBA?
Much appreciated for any and all help. This is my first more complicated attempt with the newer pivots.
This is probably not the problem you would want to solve using a pivot table. You could just use the VLOOKUP Excel function to solve this issue in a simple way. VLOOKUP will always return the first value in the lookup range that matches the lookup value.

Counting the number of older siblings in an Excel spreadsheet

I have a longitudinal spreadsheet of adolescent growth.
ID | CollectionDate | DOB | MOTHER ID | Sex
1 | 1Aug03 | 3Apr90 | 12 | 1
1 | 4Sept04 | 3Apr90 | 12 | 1
1 | 1Sept05 | 3Apr90 | 12 | 1
2 | 1Aug03 | 21Dec91 | 12 | 0
2 | 4Sept04 | 21Dec91 | 12 | 0
2 | 1Sept05 | 21Dec91 | 12 | 0
3 | 1Aug03 | 30Jan89 | 23 | 0
3 | 4Sept04 | 30Jan89 | 23 | 0
This is a sample of how my data is formatted and some of the variables that I have. As you can see, since it is longitudinal, each individual has multiple measurements. In the actual database there are over 10 measurements per individual and over 250 individuals.
What I am wanting to do is input a value signifying the number of older brothers and older sisters each individual has. That is why I have included the Mother ID (because it represents genetic relatedness) and sex. These new variable columns would just say how many older siblings of each sex each individual has. Is there a formula that I could use to do this quickly?
=COUNTIFS($B:$B,"<>"&$B2,$H:$H,$H2,$AI:$AI,$AI2,$J:$J,"<"&$J2)
Create a column named Distinct with this formula
=1/COUNTIF([ID],[#ID])
Then you can find all the older 0-sexed siblings like this
=SUMPRODUCT(([DOB]>[#DOB])*([MOTHERID]=[#MOTHERID])*([Sex]=0)*([Distinct]))
Note that I made the data a Table and used table notation. If you're not familiar [COLUMNNAME] refers to the whole column and [#COLUMNNAME] refers to the value in that column on the current row. It's similar to saying $A:$A and A2 if you're dealing with column A.
The first formula gives you a value to count that will always result in 1 for a particular ID. So ID=1 has three lines and Distinct will result in .33333 for each line. When you add up the three lines you get 1. This is similar to a SELECT DISTINCT in Sql parlance.
The SUMPRODUCT formula sums [Distinct] for every row where the DOB is greater than the current DOB, the Mother is the same as the current Mother, and the Sex is zero.
I have a possible solution. It involves adding two columns -- One for "# older siblings" and one for "unique?". So here are all the headings I have currently:
A -- ID
B -- CollectionDate
C -- DOB
D -- MOTHER ID
E -- Sex
F -- # older siblings
G -- unique?
In G2, I added the following formula:
=IF(A2=A1,0,1)
And dragged down. As long as the data is sorted by ID, this will only display "1" once for each unique person.
In F2, I added the following formula:
=COUNTIFS(G:G,"=1",D:D,"="&D2,C:C,"<"&C2)
And dragged down. It seemed to work correctly for the sample data you provided.
The stipulations are:
You would need the two columns.
The data would need to be sorted by ID
I hope this helps.
You need a formula like this (for example, for row 2):
=COUNTIFS($A:$A,"<>"&$A2,$E:$E,$E2,$D:$D,$D2,$C:$C,"<"&$C2)
Assuming E:E is column for sex, D:D is column for mother ID and C:C is column for DOB.
Write this formula in H2 cell for example and drag it down.

Resources