Excel array formula with wildcard - excel

I have an array formula that refers to a drop down cell ($AG$7) to determine which cells to evaluate. This works well, however, I need to include an additional item in the drop down which is "All".
When this is selected, I want the array formula to use "*" to return all instances from the array, but i can't get it to work.
This is the formula I'm currently using;
={SUM(IF((tblSkillsMatrix[Role]=[#Role])*(INDIRECT("tblSkillsMatrix["&V$2&"]")=$AG$7),1,0))}
I've tried using
={SUM(IF((tblSkillsMatrix[Role]=[#Role])*(INDIRECT("tblSkillsMatrix["&V$2&"]")="*"&$AG$7),1,0))}
and
={SUM(IF((tblSkillsMatrix[Role]=[#Role])*(INDIRECT("tblSkillsMatrix["&V$2&"]")="*"&$AG$7&"*"),1,0))}
But these don't work.
Does anybody have any ideas?
Thanks

A explicit = comparison cannot use wildcards. COUTIFS and SUMIFS can. As far as I see, you want to count only (conditional sum 1 and 0).
Problem is, COUTIFS and SUMIFS will not deal with INDIRECT ranges. But INDIRECT can and should (because of its volatile bahavior) often replaced by INDEX- MATCH.
So:
=COUNTIFS(tblSkillsMatrix[role],[#role],INDEX(tblSkillsMatrix,,MATCH($V$2,tblSkillsMatrix[#Headers],0)),"*"&$AG$7)
If $AG$7 is empty then it counts independent of the column named in $V$2.
Btw.: Within a table (ListObject) this needs not be entered as a array formula.
This is not 100% replacement of your formula since it not works if $V$2 is empty and so no table column title is given. Your formula will then look at all columns but this is not possible using COUNTIFS where each additional range must have the same number of columns as the criteria_range1 argument. So if $V$2 shall also can be empty, then this will not work.
If so then you could use
{=SUM((tblSkillsMatrix[role]=[#role])*(LEFT(INDIRECT("tblSkillsMatrix["&$V$2&"]"),LEN($AG$7))=$AG$7))}
Advantage: both $V$2 and $AG$7can be empty.
Disadvantage: Volatile behavior of INDIRECT and this formula then must be a array formula even in a ListObject-table. It must be confirmed using Ctrl+Shift+Enter.

Related

Excel CountIfs with an or condition on Dates

Looking to try and find the following in Excel but am having issues getting this to work.
I have Dates in Column A which need to be checked against Dates in the Query Column to compare. I want to count all records where the comparison column is greater than the date in A5 or is an empty cell. There are other conditions that I will also want to check but cannot seem to get this to work.
=COUNTIFS(Query1[Purchased Date],OR(Query1[Purchased Date]="",Query1[Purchased Date]>A5))
use:
=SUMPRODUCT(--((Query1[Purchased Date]="")+(Query1[Purchased Date]>A5)>0))
You can use an array function instead of countif.
Suppose your dates are in the range A2:A25 and your reference date is in cell B2.
If you type in any other cell
=SUM((A2:A25>B2)+(A2:A25=""))
and hit Ctrl+Shift+Enter it will give you the count you want.
This happens because Excel will resolve the first inner parentheses (A2:A25>B2) as an array of TRUE/FALSE, and does the same to the second (A2:A25=""). Next it will sum them, which is equivalent to the OR operation, and yields as a result an array of zeros (FALSE) and ones (TRUE). It wraps up by summing this array (with the function sum), all in one cell.
Perhaps this is more of a comment than an answer, but in this particular case you can just add the two separate totals:
=COUNTIF(Query1[Purchased Date],"")+COUNTIF(Query1[Purchased Date],">"&A5)
because the conditions are mutually exclusive.
Whether this helps or not depends on what additional criteria you want to add.
BTW there are two issues with your original formula:
(1) If you are combining lists of values with OR logic, you have to use addition instead (as in the two answers which use Sum or Sumproduct).
(2) The syntax of a Countif or Countifs where the range has to be kept separate from the criteria won't let you do what you want to do, so this again leads you to Sum or Sumproduct.

COUNTIFS with unique values 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))

How do I use an array formula over a whole column or varying range?

I have a spreadsheet that I'm importing data into. I need to find the value within a column that is closest to zero. The column contains both positive and negative values, and the value closest to zero will be used in another formula. I've found an answer using an array formula, but it will only work for a fixed range (e.g. K2:K10), and the number of records imported into my sheet will vary each time I use it.
Here's what I have so far:
=INDEX(K:K,MATCH(MIN(ABS(K:K)),ABS(K:K),0))
Is there a way to apply an array formula over an entire column and just include non-zero cells other than the column title? Or possibly just cells with numerical values? Or is it possible to control the range that it applies to?
We can dynamically find the last cell in the range by using another INDEX/MATCH formula that is not an array:
=INDEX(K:K,MATCH(1E+99,K:K))
This will find the last cell that has a number in column K.
So we now use this as the last cell in the range:
=INDEX($K$2:INDEX(K:K,MATCH(1E+99,K:K)),MATCH(MIN(ABS($K$2:INDEX(K:K,MATCH(1E+99,K:K)))),ABS($K$2:INDEX(K:K,MATCH(1E+99,K:K))),0))
And now the formula is dynamic.
This formula is still an array formula and must be confirmed with Ctrl-Shift-Enter when exiting edit mode. If done correctly then Excel will put{} around the formula.
If as you pointed out there is a chance of deleting row 2 then all the K2 references will also be deleted.
In place of K2 we can use INDEX(K:K,2) It will now always look at the second row and will not error when row 2 is erased. So use this instead:
=INDEX(INDEX(K:K,2):INDEX(K:K,MATCH(1E+99,K:K)),MATCH(MIN(ABS(INDEX(K:K,2):INDE‌​X(K:K,MATCH(1E+99,K:K)))),ABS(INDEX(K:K,2):INDEX(K:K,MATCH(1E+99,K:K))),0))
There is nothing wrong with the Offset() function in small amounts, but it is a volatile function. Which means that it will calculate EVERY TIME excel calculate whether the data to which it is dependent has changed or not.
For the benefit of anyone reading this post, I ran into another issue and found a way around it. Scott Craner's answer above worked well until I ran a macro that I had for that sheet, which would delete certain rows. If row 2 got deleted, the formula would give a #REF error, because it was trying to call $K$2.
My solution was to replace $K$2 with
OFFSET(K1,1,0)
Therefore, the complete formula would be:
=INDEX(OFFSET(K1,1,0):INDEX(K:K,MATCH(1E+99,K:K)),MATCH(MIN(ABS(OFFSET(K1,1,0):INDEX(K:K,MATCH(1E+99,K:K)))),ABS(OFFSET(K1,1,0):INDEX(K:K,MATCH(1E+99,K:K))),0))
And as Scott mentioned, remember to hit Ctrl-Shift-Enter to execute the array formula.

SUMIF with several OR criteria

The blue cell summates to 1, but that is not correct and it should summate to more because of the row text match with B47 row. Any ideas what's wrong?
SUMIF is not supposed to work with more than one criteria (you can't check multiple criteria within one SUMIF as you tried in your formula).
This formula will calculate the right result for you: =SUM(B3:BI3*(IFERROR(MATCH(B2:BI3,B47:AL47,0)>0,0))), this is an array formula, so you need to press CTRL+SHIFT+ENTER after it.
MATCH(...): look for all students whether they are in the list with requirments (this is the part which works only as array formula)
IFERROR(...): converts #N/A errors to 0
If I am not wrong, you are trying to calculate the number of students for a particular project with skill1 and skill2.
You may also try using COUNTIFS() function. This works for multiple criteria.

Multiple values for an AND/OR statement

Is it possible to combine AND (or OR) statement criteria to avoid repeating a cell name?
For example, I want to see if cell C2 contains the any of the numbers 2,3,5,7, or 10. I want to avoid writing
IF(AND(C2=2,C2=3... etc.
and simplify it to an array of the numbers like
IF(AND(C2=[2,3,5,7,10]...
Unfortunately, I have a lot more than just 5 numbers to add so it's getting be very laborious. Anyone have an easier way than repeating the cell name=__ over and over?
You can use an "array constant" like this
=IF(OR(C2={2,3,5,7,10}),"Yes","No")
.....or for a large set of numbers you could put all the numbers in a cell range, e.g. Z2:Z100 and do the same
=IF(OR(C2=$Z$2:$Z$100),"Yes","No")
although when you use a range rather than an array constant the formula becomes an "array formula" so needs to be confirmed with CTRL+SHIFT+ENTER
perhaps better to use COUNTIF and avoid "array entering"...
=IF(COUNTIF($Z$2:$Z$100,C2)>0,"Yes","No")
=IF(ISERROR(MATCH(C2,{2,3,5,7,11},0)),"no","yes")
No need to enter this as an array formula. How it works: MATCH returns a #N/A! error if it can't find the lookup value in the lookup array. ISERROR catches this.
But as barry houdini suggests, you may want to put your numbers in some range e.g. Z1:Z5 instead of hard-coding them into your formula. So you would have this formula instead:
=IF(ISERROR(MATCH(C2,Z1:Z5,0)),"no","yes")

Resources