I need to list a running count in column A of each occurrence of the value "4A" in column B, but the count needs to reset to 0 if the value in column B equals "2B".
I have attached a sample of my workbook, column A shows how I need the count to operate. I am open to using VB or a formula.
Or in text format:
| A | B |
+---+----+
| 0 | 2B |
| | 3A |
| 1 | 4A |
| | 4B |
| | 4B |
| 2 | 4A |
| | 4B |
| 3 | 4A |
| | 9A |
| 0 | 2B |
| | 3A |
| 1 | 4A |
| | 4C |
| 2 | 4A |
| | 9A |
| 0 | 2B |
| | 3A |
| 1 | 4A |
| | 9A |
| 0 | 2B |
| | 3A |
| 1 | 4A |
| | 4C |
| | 9A |
| | 9B |
| | 9Z |
EDIT:
#Jeeped and #trincot proposed better non-array versions. I modified them slightly here:
#Jeeped's formula:
=IF(OR(B1={"2B","4A"}), COUNTIF(INDEX(B:B,AGGREGATE(14, 6, ROW(B$1:B1)/(B$1:B1="2B"), 1)):B1, "4A"),"")
After modification it uses LARGE function in the aggregate rather than SMALL and "" instead of TEXT(,).
#trincot's formula:
=IF(OR(B2={"2B","4A"}),(B2="4A")*(1+IFERROR(INDEX(G:G,MATCH(MAX(G$1:OFFSET(G2,-1,0))+1,G$1:OFFSET(G2,-1,0))),0)),"")
Compared to original, it's been rearranged slightly 'IF-wise', I also replaced magic 9999 with MAX([...]) + 1 and made it so that you can just paste it in 1st cell and drag down rather than have two separate formulas for first cell and the consecutive ones.
My original version:
I think it has the benefit of perhaps being the easiest to understand (? - I am obviously biased here so I might be wrong) and it's physically the shortest of all.
It has the obvious drawback of being an array-formula.
In cell "A1" you can write:
=IF(OR(B1="2B",B1="4A"),SUM((B$1:B1="4A")*(ROW(B$1:B1)>MAX(ROW(B$1:B1)*(B$1:B1="2B")))),"")
It's an array formula so you have to confirm it with ctrl + shift + enter.
Then you can drag it down.
Basically it counts number of occurrences of "4A" in a range extending from current row to the first provided that rows of counted "4A" are larger than that last row of "2B".
Here is a way with standard (non-array) formulas:
In A1 put:
=IF(B1="4A",1,0)
In A2 put:
=IF(B2="2B",IF(INDEX(A:A,MATCH(9999,A$1:A1))>0,0,""),
IF(B2="4A",1+INDEX(A:A,MATCH(9999,A$1:A1)),""))
Drag/copy A2 down to the remaining cells in the A column.
This will produce blanks where otherwise the same value would be displayed.
If repeating values don't need to be suppressed, then you can simplify the formula in A2 to:
=IF(B2="2B",0,IF(B2="4A",1+A1,A1))
... and copy it down.
Related
The spreadsheet I am working with houses multiple duplicates for CLIENT_ID and I want to be able to count each CLIENT_ID only once, and then count the amount of a specified value within AGE_CATEGORY. Basically I want to exclude any duplicates and then countif.
| Column A | Column B |
+-----------+--------------+
| CLIENT_ID | AGE_CATEGORY |
| 1514 | 65 |
| 1517 | 65 |
| 1522 | 17 |
| 1519 | 37 |
| 1514 | 65 |
| 1516 | 28 |
| 1503 | 20 |
--- I would like for the count for people >=65 to be 2, since client# 1514 appears twice.
How can I do it?
What you need is first extract unique values from column A then count those IDs. As per my below screenshot use below formulas ExcelO365 formulas.
D2 =UNIQUE(A2:A8)
E2 =COUNTIF(A2:A8,D2#)
Create a list of unique age (category?) values — say, in Column D
— with “Data” → “Sort & Filter” → “Advanced (Filter)”.
(Sort them if you want.)
Enter
=SUM(--(FREQUENCY(IF(B$2:B$8=D2,A$2:A$8),A$2:A$8)>0))
into E2, press Ctrl+Shift+Enter,
and copy/drag down.
This is the result:
| A | B | C | D | E |
---+-----------+--------------+--------------+--------------+-------+
1 | CLIENT_ID | AGE_CATEGORY | | AGE_CATEGORY | |
2 | 1514 | 65 | | 65 | 2 |
3 | 1517 | 65 | | 17 | 1 |
4 | 1522 | 17 | | 37 | 1 |
5 | 1519 | 37 | | 28 | 1 |
6 | 1514 | 65 | | 20 | 1 |
7 | 1516 | 28 | | | |
8 | 1503 | 20 | | | |
Based on this answer by SandPiper.
Assuming your data (to return values) is in A2:A8 and criteria is in B2:B8 use this ARRAY formula to check unique values >=65
=SUM(--(FREQUENCY(IF($B$2:$B$8>=65,MATCH($A$2:$A$8, $A$2:$A$8,0)),ROW($A$2:$A$8)-ROW($A$2)+1)>0))
Note for array formula you have to enter this formula with ctrl + shift + enter so that curly braces appear around the formula automatically.
Needless to say, you can change >=65 to your criteria accordingly. sample check see screenshot
So I have a case where I need to return all results in a column where 2 criteria are met into one cell.
My table looks something like this
+-----------+------+-------+
| Job | Type | Name |
+-----------+------+-------+
| Tree | AA | Bob |
| Apple | FF | John |
| Banana | FF | John |
| Grape | CC | James |
| Mango | FF | Paul |
| Chocolate | AA | Angus |
+-----------+------+-------+
And I currently use a formula like this:
=IFERROR(INDEX(Table1[Job],MATCH(1,(Table1[Name]=B12) * (Table1[Type]="FF"),0)),"")
Where B12 is an lookup name. However if I was to write John, I'd only get Apple. What I want is "Apple, Banana".
How would I go around doing that?
Use TEXTJOIN as an array formula:
=TEXTJOIN(", ",TRUE,IF((Table1[Name]=B12) * (Table1[Type]="FF"),Table1[Job],""))
Being an array formula it must be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode.
TEXTJOIN was introduced with Office 365 Excel.
and thanks for your help! Without helper columns/rows I'm trying to find the average difference in LVEDi between first and last visits of the patients.
E.g., by hand you would work it out as:
(diff for patient a + diff for pt b + diff for pt c)/3 =
((55-45) + (40-31) + (25-31))/3 =
(10 + 9 + (-6))/3 =
13/3 =
4.333
I'm sure the answer involves indexing with an array function, but I can't get started on how to tackle it!
Any help?
Thanks!
A | B | C | D | E
01 Patient ID | Date | First-Last | LVEDi | LOTS of similar cols
02 a | 3/3/03 | LAST | 55
03 a | 1/1/01 | FIRST | 45
04 a | 2/2/02 | | 65
05 b | | |
06 b | 3/2/16 | LAST | 40
07 b | | |
08 b | | |
09 b | 2/1/12 | FIRST | 31
10 c | 1/7/14 | LAST | 25
11 c | | |
12 c | 2/2/03 | FIRST | 31
13 c | 2/2/08 | | 46
You can use the * as and AND criterion in arrays, so with MATCH you search for the instance, where both conditions multiplied are TRUE (i.e. 1). The rest is just filling in your calculation:
{=((INDEX(D2:D13,MATCH(1,(A2:A13="a")*(C2:C13="LAST"),0))-INDEX(D2:D13,MATCH(1,(A2:A13="a")*(C2:C13="FIRST"),0)))+(INDEX(D2:D13,MATCH(1,(A2:A13="b")*(C2:C13="LAST"),0))-INDEX(D2:D13,MATCH(1,(A2:A13="b")*(C2:C13="FIRST"),0)))+(INDEX(D2:D13,MATCH(1,(A2:A13="c")*(C2:C13="LAST"),0))-INDEX(D2:D13,MATCH(1,(A2:A13="c")*(C2:C13="FIRST"),0))))/3}
And remember to use CTRL + SHIFT + ENTER to enter the array.
Found a slightly less verbose method:
=SUM(SUMIFS(D:D,C:C,"LAST",A:A,{"a","b","c"})-SUMIFS(D:D,C:C,"FIRST",A:A,{"a","b","c"}))/3
EDIT 1
Building on Lukas' comment:
=(SUMIF(C:C,"LAST",D:D)-SUMIF(C:C,"FIRST",D:D))/COUNTIF(C:C,"LAST")
I am working of the dataset in Excel that I obtained from an experiment. Since I needed some ratings (and I wanted the raters to be blind) I completely randomized the answers and now I can't put them back in order!
This is what I have:
1A
38R
22R
7A
41R
64A
etc...
And this is what I need in the end:
1A
2A
3A
...
99R
100R
101R
Thank you!
I have created two new columns (B and C in this case, as in the other example posted).
I have typed LEFT(A1,LEN(A1)-1) in column B to get the number; then =RIGHT(A1,1) in column C to get the letter; finally I can sort by B and C.
You will not get your desired output by sorting alphabetically, because 100R would come before 2A.
If your values will always be in the format of a number followed by a single character and will be at most 5 characters long, you can use #Scott Craner's formula =RIGHT("00000"&A1,5) to pad the left of your value with "0" so that you can alphabetize correctly. 100R will become 0100R. 2A will become 0002A. These will now alphabetize correctly.
Now you can simply sort your range by column B ascending alphabetically.
If you need more characters, just add as many zeroes as characters to the formula, and change the 5 in the formula to your new number of characters.
Here is an example excel file.
INPUT
+---+-----+-------+
| | A | B |
+---+-----+-------+
| 1 | 1A | 0001A |
+---+-----+-------+
| 2 | 38R | 0038R |
+---+-----+-------+
| 3 | 22R | 0022R |
+---+-----+-------+
| 4 | 7A | 0007A |
+---+-----+-------+
| 5 | 41R | 0041R |
+---+-----+-------+
| 6 | 64A | 0064A |
+---+-----+-------+
RESULT
+---+-----+-------+
| | A | B |
+---+-----+-------+
| 1 | 1A | 0001A |
+---+-----+-------+
| 2 | 7A | 0007A |
+---+-----+-------+
| 3 | 22R | 0022R |
+---+-----+-------+
| 4 | 38R | 0038R |
+---+-----+-------+
| 5 | 41R | 0041R |
+---+-----+-------+
| 6 | 64A | 0064A |
+---+-----+-------+
I have the following list in Excel:
+-------+----------+
| am | ipiresia |
+-------+----------+
| 50470 | 29 |
| 50470 | 43 |
| 50433 | 29 |
| 6417 | 51 |
| 6417 | 52 |
| 6417 | 53 |
| 4960 | 25 |
| 4960 | 26 |
| 5567 | 89 |
| 6716 | 88 |
+-------+----------+
I want to add a column, let's say 'num' and count the occurrences of column 'am' in a row adding one when a new occurrence happens as follows:
+-------+----------+-----+
| am | ipiresia | num |
+-------+----------+-----+
| 50470 | 29 | 1 |
| 50470 | 43 | 2 |
| 50433 | 29 | 1 |
| 6417 | 51 | 1 |
| 6417 | 52 | 2 |
| 6417 | 53 | 3 |
| 4960 | 25 | 1 |
| 4960 | 26 | 2 |
| 5567 | 89 | 1 |
| 6716 | 88 | 1 |
+-------+----------+-----+
Is it possible to get this automatically with a formula in Excel?
yes,
my example:
(assume you start your table containing 3 columns at Excels origin at A1 without header lines)
Then fill C1 with value "1"
and then start in C2 with entering a formula
simple like this:
=if($A2=$A1;$C1+1;1)
then you drag C2 down at the cells downright located autofill position as far as you want. Most times also double click works to let Excel autofill the columns down to the end of you prefilled table.
If you need assistance for AutoFill press F1 in Excel an the help with tell you in detail.
Assuming the sample table starts at A1 (with headers) the following formula will provide the expected results even if the list is not sorted.
=COUNTIF($A$1:$A2,A2)
Enter the formula at cell C2 then paste it down to the last cell of the data (or use AutoFill)