COUNTIFS with unique values Excel - excel

I am trying to produce a count of the number of times different strings come up in an Excel table. An example table, currently in SHEET1, would be this:
I have another table in another spreadsheet where I want to indicate, for each letter on the left in Table 1, how many entries for "za", "zc" or "zd" come up on the right. However, I would only like to only consider one entry of each.
The end result, on row B of SHEET2, would have to be something like this:
At the moment I am using a combination of SUM and COUNTIFS to do the job.
More specifically, applied to the example, I am using the following formula:
=SUM(COUNTIFS(Sheet1!A1:A18,Sheet2!$A1,Sheet1!B1:B18,{"za","zc","zd"}))
The formula is doing some of what is intended. However, it is not counting each entry just one time. Instead, its is counting, for each letter on the left, every entry of "za","zc" or "zd". The table that the formula is returning is as follows:
How can I change the formula so that it does what I intend?
Thank you.

My initial thought would be:
=SUM(MIN (1,COUNTIFS(Sheet1!A1:A18,Sheet2!$A1,Sheet1!B1:B18,{"za","zc","zd"}))
but I’m not where I can test if the MIN will apply properly to the COUNTIFS array of results. ;-)
EDITED: The MIN function is taking minimum of 1 or all of the items in the COUNTIFS array, rather than minimum of 1 and each item in the COUNTIFS array, which is what I was afraid of. Using
=MIN(COUNTIFS(Sheet1!A$1:A$18,Sheet2!$A1,Sheet1!B$1:B$18,"za"),1)+MIN(COUNTIFS(Sheet1!A$1:A$18,Sheet2!$A1,Sheet1!B$1:B$18,"zc"),1)+MIN(COUNTIFS(Sheet1!A$1:A$18,Sheet2!$A1,Sheet1!B$1:B$18,"zd"),1)
will gain the desired results. It is a little clunky, but simpler than an array formula. If you want an array formula, you can use:
=SUM(FREQUENCY(IFERROR(MATCH({"za","zc","zd"},(IF(Sheet1!$A$1:$A$18=$A5,Sheet1!$B$1:$B$18)),0),""),IFERROR(MATCH({"za","zc","zd"},(IF(Sheet1!$A$1:$A$18=$A5,Sheet1!$B$1:$B$18)),0),"")))
This uses the FREQUENCY function to take a set of values and see how many items in another set of values fall within each of the data ranges. Since you need text instead of numbers, we use the MATCH function to find out the first time the value occurs in your list, returning "" with the IFERROR function if it doesn't. (We only need the first occurrence since you don't want to know how many occurrences there are). Since it is text, we use the same input for both arguments for FREQUENCY.
Therefore, if you need to change the values you are looking for or the ranges in which you are searching, make sure to change both! Alternately, you could list the values out somewhere, say in F1:F3, and make a named range for this, another one for A1:A18, and another for B1:B18. Your formula would then look something like this:
=SUM(FREQUENCY(IFERROR(MATCH(SearchValues,(IF(colA=$A2,colB)),0),""),IFERROR(MATCH(SearchValues,(IF(colA=$A2,colB)),0),"")))
Then you need only change your named range definitions and your formulas would update. :-)
NOTE: Since this is an array formula, you must close out of the cell by pressing CTRL+SHIFT+ENTER rather than only ENTER. When you look at the formula bar, you should see
{=SUM(FREQUENCY(IFERROR(MATCH(SearchValues,(IF(colA=$A2,colB)),0),""),IFERROR(MATCH(SearchValues,(IF(colA=$A2,colB)),0),"")))}
It does NOT work to enter the curly braces yourself. ;-)

You can use this formula at B1 and fill down:
B1:
=SUMPRODUCT(((Sheet1!$A$1:$A$18=A1)*(Sheet1!$B$1:$B$18= {"za","zc","zd"}))/
COUNTIFS(Sheet1!$A$1:$A$18,Sheet1!$A$1:$A$18,Sheet1!$B$1:$B$18,Sheet1!$B$1:$B$18))

Related

How to simplify multiple COUNTIFS in excel with non-consecutive ranges

I have a spreadsheet...
A you can see, the cell F2 has a formula with multiple COUNTSIF, basicly checks the cells F14, F33, F62 y there is a Pass there, and if there is one will give you a % completion. My question is that I have to add around 20-30 COUNTIFS to that formula, is there a way to simplify it.
=(COUNTIF(F14,"Pass")+COUNTIF(F33,"Pass")+COUNTIF(F62,"Pass")+COUNTIF(F75,"Pass")+COUNTIF(F88,"Pass")+COUNTIF(F105,"Pass"))/(COUNTIFS(F14,"<>na")+COUNTIFS(F33,"<>na")+COUNTIFS(F62,"<>na")++COUNTIFS(F75,"<>na")++COUNTIFS(F88,"<>na")+COUNTIFS(F105,"<>na"))
This is not the final formula, still missing around 20 entries. If you're wondering why not do a simple F15:FXX, because i just need the cells that have a test case name, like F14, F33, etc.
For the first part of your formula, you can use the INDEX function to return a non-contiguous set of values, which you can test.
For example, the equivalent for the first part would be:
=SUM(N(INDEX($F:$F,IF(1,N({14,33,62,75,88,105})))="Pass"))
The IF(1,N({…})) part is how you specify which cells (rows) in Column F to return.
Without knowing more about your data, not sure about handling the percentage issue.
Your posted formula would not calculate a percentage, as it is only dividing the SUM by whether or not F14<>"na" is true, and then adding one for the <>"na" factor for the rest
In earlier versions of Excel, you may need to confirm this array formula, hold down ctrl + shift while hitting enter. If you do this correctly, Excel will place braces {...} around the formula seen in the formula bar.
If you want to return the percent "pass" in your list of cells, merely divide the SUM by the number of cells. You can either hard-code that number, or compute it with something like:
COLUMNS({14,33,62,75,88,105})
Or all together:
=SUM(N(INDEX($F:$F,IF(1,N({14,33,62,75,88,105})))="Pass"))/6
or
=SUM(N(INDEX($F:$F,IF(1,N({14,33,62,75,88,105})))="Pass"))/COLUMNS({14,33,62,75,88,105})

How have I misunderstood structural references?

I have a table with travel expenses and I want to simply sum an entire column.
What I came up with is the following:
=SUM(REPLACE(Table1[[#Data], [Main Currency]], 1, 4, ""))
But it seems to only work with the data that is on the same row as the formula. I thought it would sum the entire column. What am I misunderstanding?
The Replace function works on a single string, not an array of strings. You may want to use a helper column in your data that turns the text into a number row by row, and then sum the helper column.
Or, you need to use an array formula, which is confirmed with Ctrl+Shift+Enter. In this case, it is not sufficient, though, since the array that is served to the Sum() function will consist of a list of text strings. Sum ignores text. Therefore, you will need to coerce the string to a number, like this:
=SUM(REPLACE(Table1[Main Currency],1,4,"")+0)
Remember to use Ctrl+Shift+Enter to confirm the formula. Even this will give you an error if there is a blank cell in the column.
So, it might be easier to clean up the data before trying to sum it.
You may want to use Power Query to clean up the data before you let any formulas loose on it.

Excel formula - Get first occurrence of partial string in rows above cell

I have a table of fruits in Excel 2013.
I'd like to fill the "Category" column by searching from the current row to the top until the first occurrence of "::", which is the keyword for a category in the table.
If there was some way to reverse a range, I could do something like "=Match("::*"; $A6:$A$2)" to find the row. However, this is not possible.
Does anyone know how this might be accomplished using formulas?
Using your provided sample data, and assuming your data is already organized as shown in your sample, you can take advantage of that organization and use this formula in cell C2 and copy down:
=IF(LEFT(A2,2)="::","",IF(LEFT(A1,2)="::",MID(A1,4,LEN(A1)),C1))
Assuming your table is in A1, put this in C3:
=INDEX(A:A, AGGREGATE (14,6,ROW($A$1:A2)/(LEFT($A$1:A2,2)="::"),1))
And copy down.
Here's a kinda different approach. I'm just basically responding to this part of your post to prove this is possible:
If there was some way to reverse a range, I could do something like "=Match("::*"; $A6:$A$2)" to find the row. However, this is not possible.
Reversing a range is possible, it's just tricky.
As you pointed out: $A6:$A$2 won't work since this is equivalent to $A$2:$A6.
However, without getting into the nitty-gritty details, this array formula will reverse this range:
= INDEX($A$2:$A6,N(IF({1},MAX(ROW($A$2:$A6))-ROW($A$2:$A6)+1)))
Note this is an array formula, so you must press Ctrl+Shift+Enter instead of just Enter after typing this formula into a cell.
You could use this in combination with your MATCH formula to get the desired result (which tells you how many rows up the :: row is):
= MATCH("::*",INDEX($A$2:$A6,N(IF({1},MAX(ROW($A$2:$A6))-ROW($A$2:$A6)+1))),0)
(Also haha this is kinda cool: Usually you see MATCH used within INDEX to effectively get a VLOOKUP type of functionality. This is the first time I have ever seen it the opposite way of having INDEX within MATCH.)
Note that I'm not saying this is necessarily the best approach for this specific problem, just proving a point that arrays can be reversed.

How to Use Cell Text From Cell Being Checked by COUNTIF in Excel

What I'm wanting to do is have a formula in one cell that counts the values in a range that conform to a lookup of that range cell's value compared to another cell.
OMG, now that I look at it, that is totally confusing. Let me try to clarify a lot here.
Say we have Cell1, which will hold the counting formula. I have a list of values in a two-column table, Table1. The range, Range1 that Cell1 will be counting from is a range of cells that have List Validation in them. Table1 holds references to all values that can result from those Lists, in column 1. I have another cell, Cell2, which holds a number value. Column 2 of Table1 holds values that reference Cell2. I need to count the number of values from Range1 whose row matches in Table12 match the value in Cell2. Is there a way I can do this with COUNTIF without referencing each cell individually? Is there some shorthand (like Range.currentValue) that I can use to get the value of the cell currently being checked? The range is 11 rows long, and I need to do a second range that has 12 rows counted.
Man, I really don't know how to clarify that any more... I'll post this for now, in case anyone can understand what I'm saying and knows the answer, while I work on a sample spreadsheet I can upload.
I did my best to visually represent what I'm trying to accomplish:
http://gyazo.com/b83295baf3b156683a5c39b40c806504
Extended explanation: http://gyazo.com/4048802050e3dcfca7aee238acc2f7dd
Use a helper column, say, between the brown and the first blue or at the right of the setup. Use a vlookup like
=vlookup(brownvalue,BluetableRange,2,false)
Then do a countif on the helper column
=countif(HelperColumn,"<="&GreenCellAddress)
You can hide the column with the helper if it upsets your spreadsheet design.
You can (and probably should) use a helper column as Teylyn suggests. But, for when that may be inconvenient, you can also use an array formula:
=SUM(COUNTIFS(listlookupcolumn,rangeoflists,numbervaluecolumn,"<="&numbertomatch))
To enter it as an array formula, type "ctrl-shift-enter" after editing the formula, rather than just "enter"
Rough explanation: since rangeoflists is in a place where a single value is expected, the countifs is calculated once for each value, and the array of results is passed to sum. Use the "evaluate formula" feature to see the intermediate result array.
Afterthought: It occurs to me now that this does rely on listlookupcolumn containing unique values. (Almost certainly true in this example.) You can modify the formula a bit to get around this:
=SUM(SIGN(COUNTIFS(listlookupcolumn,rangeoflists,numbervaluecolumn,"<="&numbertomatch)))
The SIGN function will keep you from double counting.
Again, you must use "ctrl-shift-enter" for this to work. (Yes, as I'm sure others are ready to point out, you can also use the sumproduct hack in this instance.)

Can't get sumifs to work with a nested function

I have a set of data with one column having product IDs, and another column having the values.
I'm looking to take a sum of the column values, IF the IDs have a 7 or 9 as their 3rd character. I have a formula that I believe is very close, but not there yet:
=SUMIFS(E3:E821,A3:A821,MID(this,3,1)="7",A3:A821,MID(this,3,1)="9")
My problem is that I'm using the mid function in the criterias, but I don't know how to give it a variable to reference the current cell being evaluated. If I were programming in JavaScript, I'd use "this" to reference the current object, but that doesn't work in Excel. All of the examples I've been able to find have very simple criteria where you don't reference the cell itself in the comparison, which leaves me at a loss.
Create a helper column C:
=OR(MID(A2,3,1)="7",MID(A2,3,1)="9")
SumIfs function is then:
=SUMIFS(B:B,C:C,TRUE)
Another alternative, without helper column, that I've not optimized. It is an array function (so press CTRL SHIFT ENTER after typing formula:
=SUMPRODUCT(IF(MID(A:A,3,1)="7",1,0),B:B)+SUMPRODUCT(IF(MID(A:A,3,1)="9",1,0),B:B)
Even more simple:
=SUMPRODUCT((MID(A:A,3,1)="7")+(MID(A:A,3,1)="9"),B:B)
No CTRL+SHIFT+ENTER and no helper column. Just one formula for what you want.
More information for this available here.

Resources