COUNTIF running into problems with DataTypes - excel

Basically I want to search 2 columns. IF any number in any of the columns is 1, I want to total it up.
here is my formula:
COUNTIF(('Background data'!$A:$A,'Background data'!B:B), 1).
However, when I apply it, I get an error saying 'Value used in the formula is the wrong data type'.
To fix this issue, I went to the Background Data tab and
Set all Columns to Numerical data type
Set all columns to Numerical data type in the tab that I am currently working on
Individually tested the countif formula with either column A and then column B and then I don't receive the error. This concludes that my columns are of the correct data type.
I am guessing I'm getting the error because there is an error in my formula and not the actual data?

Try breaking it up into 2 countif statements:
=COUNTIF('Background data'!$A:$A,1)+COUNTIF('Background data'!B:B, 1)

I want to search 2 columns. IF any number in any of the columns is 1, I want to total it up.
A COUNTIF function can count multiple columns at once.
=COUNTIF('Background data'!A:B, 1)
Since summing a bunch of ones is the same as counting them, a SUMIF function could do this as well.
=SUMIF('Background data'!A:B, 1)

How do you plan on using Countif()? Your formula, as above, has an extra number. Just use =Countif('Background data'!$A:$A,'Background data'!B:B).
What's the extra , 1) doing?

Related

Getting number and off setting numbers from array in Excel

I have array of numbers in a single column like this:
I want only that numbers for which corresponding negative numbers exist. If number exist 2 times, but negative number exist only one time, then I wanted to retain one positive and one negative number. Similarly, if number exists 3 times, and negative number appears only two times, then I want 2 set of numbers including positive and negative. In this case, I wanted to get output:
5 2 -2 -5
Orders of numbers are not relevant for me. Please do not use VBA. You can create multiple column and apply filter at the end.
Thank you for the response, but I wanted to get the data in column next to the values. Like:
5
2
-2
-5
Please help.
Here's another Office 365 solution:
Name the data range DATA
Put this formula anywhere: =CONCAT(REPT(-ROW(A1:A100)&" "&ROW(A1:A100)&" ",COUNTIF(DATA,"="&ROW(A1:A100)*IF(COUNTIF(DATA,"="&-ROW(A1:A100))<COUNTIF(DATA,"="&ROW(A1:A100)),-1,1))))
That will output the pairs into one cell.
Here's a slightly modified Step 2, which excludes duplicates: =CONCAT(IF((COUNTIF(DATA,"="&-ROW(A1:A100))>0)*(COUNTIF(DATA,"="&ROW(A1:A100))>0),-ROW(A1:A100)&" "&ROW(A1:A100)&" ",""))
Looks like this:
The data doesn't need to be sorted. Both methods work up to 100, but you can easily expand that by changing A100 to A1000 or whatever you need.
Use the vlookup formula to identify the rows, and you can use the Filter & Unique formula to get the list, or a pivot table.
First, immediately next to your data use the formula:
=vlookup(A1*-1,$A$1:$A$1,1,0)
For non-365:
This will produce an error for each instance that doesn't have a match. You can filter at this point to get your list from the existing table. You can also create a pivot table under the Data tab of your ribbon and inserting a pivot table. Filter the #N/A from there to get an exclusive list without hidden rows.
For 365:
You can use the following combination of formulas to get the exclusive list as well.
=UNIQUE(FILTER(B1:B8,ISNUMBER(B1:B8)),0,0) or =UNIQUE(FILTER($B$1:$B$8,ISNUMBER($B$1:$B$8)),0,0) should yield the same results
As ScottCraner mentioned, you can circumvent the helper column in 365 by modifying the formula a bit more:
=UNIQUE(FILTER(A1:A8,ISNUMBER(MATCH(-A1:A8,A1:A8,0)),"")
The Match here is doing something similar to the Vlookup, but housing that logic within the formula, so it's a cleaner solution in my opinion.
Using your data the result was { -5,-2,2,5 }
These are spill formulas so you only need to put it in one spot and it will expand the formula over the adjacent cells below where it's entered for however many cells needed to list all the unique numbers that occur. It takes into account the negatives and so on. This may be a 365 formula, so if you're on another version of excel it may not work.
Edit: Adjusted the instructions to fully address the question.

Excel Sum Index Match Across Multiple columns

I am having significant issues trying to resolve my problem. Essentially I need an excel formula that replicates a SUMIFS function, as it appears that sumifs doesn't work in my scenario. Effectively I need to SUM across a horizontal axis, based on the date & header parameter. I have tried summing index-matches, sumifs, aggregates, summing sumif, summing vlookups & hlookups, and I either get errant values or I get the first value (for example, store A would return 0 for 7/8 & Store G would return -3,291)
=SUMIF($1:$1,B22,INDEX($C$2:$AQ$1977,1,MATCH($A982,$A$2:$A$9977,0)))
=SUMIFS(B2:N2,1:1,B22,A:A,A23)
=SUM(SUMIF($B$1:$N$1,$B23,INDEX($B$2:$N$12,1,MATCH($A23,$A$2:$A$12,0))),SUMIF($B$1:$N$1,$B23,INDEX($B$2:$N$12,2,MATCH($A23,$A$2:$A$12,0))),SUMIF($B$1:$N$1,$B23,INDEX($B$2:$N$23,3,MATCH($A23,$A$2:$A12,0)))).
I'm sure the sumrange is what is killing me, but I would ideally like the code to be dynamic enough to locate and sum the cells via references, in case the input data changes at some point. I am working with thousands of rows so the sum range is B2:AQ10000.
The formula is on a different sheet than this but i input it as an example.
What am I missing? Is there a way to do this with Excel?
Use:
=SIMIFS(INDEX($B$2:$N$12,MATCH($A23,$A$2:$A$12,0),0),$B$1:$N$1,B$22)

Excel: Get values in non-adjacent cells based on multiple criteria

I have an excel sheet sort of like this:
I'm trying to figure out how to get the totals in cells B1 through B4.
I tried INDEX-MATCH, where I tried to match the words in A1:A4 with the words in row 7, get the numbers relative to them, and then sum them, but it was a lot of Google searching and stabbing in the dark -- every attempt returned an error.
I also tried to INDEX-MATCH the words in A1:A4 with row 7, and then nest a VLOOKUP in there where it'd get the number relative to "visits:" but that didn't work at all either.
Is INDEX-MATCH even the correct function? Any help would be much appreciated, I'm not even sure what to Google anymore.
EDIT: I need to use a search function of some kind, like the INDEX-MATCH method, rather that static formulas because the sheet will change periodically and I don't want to have to update the formula every time I add an animal.
Your data table is unusual in structure.
However, if you are gong to keep a fixed rule such that the number of visits is always offset 2 rows and 1 column from the animal type(and that itself is always in row 7), you could do:
In B1:
=SUM(IF($A$7:$AAA$7=$A1, $B$9:$AAB$9, 0))
Confirm with Ctrl-Shift-Enter, and then copy down..
DOes this work?
=SUM(IF($B$7=A1,$C$9,0),IF($D$7=A1,$E$9,0),IF($F$7=A1,$G$9,0),IF($H$7=A1,$I$9,0))
I'm not sureto have fully grasped your challenge. Yet it seems the following solution would work:
Add the following formula in each box where the number of visits is added as
=+SUMIF($A$1:$A$end;animal;$B$1:$B$end)
Where end is a number of the last cell in the first and second columns data contain the data.
And animal is the cell that contains the name of the animal.
Therefore in your simple example, the formulas on cells C9;E9;G9 and I9 would be respectively:
=+SUMIF($A$1:$A$4;B7;$B$1:$B$4) ; =+SUMIF($A$1:$A$4;D7;$B$1:$B$4); =+SUMIF($A$1:$A$4;F7;$B$1:$B$4) and =+SUMIF($A$1:$A$4;H7;$B$1:$B$4).

Taking average of certain values in one Excel column based on values in another

I have a (large) array of data in Excel of which I need to compute the average value of certain values in one column, based on the values of another column. For example, here's a snippet of my data:
So specifically, I want to take the average of the F635 mean values corresponding with Row values of 1. To take it a step further, I want this to continue to Row values of 2, Row values of 3 etc.
I'm not familiar with how to run code in Excel but have attempted to solve this by using the following:
=IF($C = "1", AVERAGE($D:$D), "")
which (to my understanding) can be interpreted as "if the values (anywhere) in column C are equal to 1, then take the average of the corresponding values in column D."
Of course, as I try this I get a formula error from Excel.
Any guidance would be incredibly appreciated. Thanks in advance.
For more complicated cases, I would use an array-formula. This one is simple enough for the AVERAGEIF formula. For instance =AVERAGEIF(A1:A23;1;B1:B23)
Array-formula allows for more elaborate ifs. To replicate the above, you could do =SUM(IF($A$1:$A$23=1;$B$1:$B$23;0))/COUNT(IF($A$1:$A$23=1;$B$1:$B$23;0)).
Looks like more work but you can create extremely elaborate if-statements. Instead of hitting ENTER, do CTRL-ENTER when entering the formula. Use * between criteria to replicate AND or + for OR. Example: SUM(IF(($A$1:$A$23="apple")*($B$1:$B$23="green");$C$1:$C$23;0)) tallies values for green apples in c1:c23.
Your sample data includes three columns with potential ifs so my guess is that you're going to need array formulas at some point.
Excel already has a builtin function for exactly this use; AVERAGEIF().
=AVERAGEIF(C:C,1,D:D)

Excel Pivot - Count of distinct values in a given range

I've the following data set from which I need the count of distinct values in a pivot. I've tried few function like FREQUENCY, COUNTIFS etc. but I could not make it.
Input
Input Data
Output
Expected Output
=SUM(IF((B2:D4=C10),1,0))
To get result after using formula hit ctrl+shift+enter
I think it's an awkward case because the data values are in more than one column and because they are text not numbers.
The only way I could come up with would be to repeat a standard method of getting the distinct values and then use COUNTIF to get the counts.
So starting in F2 I have:-
=IFERROR(INDEX($B$2:$B$4,MATCH(0,COUNTIFS($F$1:$F1,$B$2:$B$4),0)),
IFERROR(INDEX($C$2:$C$4,MATCH(0,COUNTIFS($F$1:$F1,$C$2:$C$4),0)),
IFERROR(INDEX($D$2:$D$4,MATCH(0,COUNTIFS($F$1:$F1,$D$2:$D$4),0)),"")))
(It's an array formula and must be entered with CtrlShiftEnter)
And starting in G2:-
=COUNTIF($B$2:$D$4,F2)
To avoid having to specify an exact range (e.g. $B2:$B4), you could use the following in F2 and adjust it to the maximum number of rows you are likely to use:-
=IFERROR(INDEX($B$2:$B$10,MATCH(0,IF(ISTEXT($B$2:$B$10),COUNTIFS($F$1:$F1,$B$2:$B$10),1),0)),
IFERROR(INDEX($C$2:$C$10,MATCH(0,IF(ISTEXT($C$2:$C$10),COUNTIFS($F$1:$F1,$C$2:$C$10),1),0)),
IFERROR(INDEX($D$2:$D$10,MATCH(0,IF(ISTEXT($D$2:$D$10),COUNTIFS($F$1:$F1,$D$2:$D$10),1),0)),"")))
and this in G2:-
=IF(F2="","",COUNTIF($B$2:$D$10,F2))
but of course it's restricted to three columns and anything beyond this I think may point to a VBA solution.
There is a also a general formula for distinct values from a 2d array here but the output includes a zero when blank rows and columns are included so would need some modification.
So here is the modified formula from the reference above with error handling starting in I2:-
=IFERROR(INDEX(tbl_text, MIN(IF( IF(ISTEXT(tbl_text),COUNTIF($I$1:$I1, tbl_text),1)=0, ROW(tbl_text)-MIN(ROW(tbl_text))+1)),
MATCH(0, COUNTIF($I$1:$I1, INDEX(tbl_text, MIN(IF(IF(ISTEXT(tbl_text),COUNTIF($I$1:$I1, tbl_text),1)=0, ROW(tbl_text)-MIN(ROW(tbl_text))+1)), , 1)), 0), 1),"")
With the counts starting in J2:-
=IF(J2="","",COUNTIF(tbl_text,J2))
where tbl_text is a named range defined (when I tested it) as $B$2:$E$10
This I think should meet your additional criterion of it being more generalized because you can set tbl_text to include the maximum number of rows and columns that you are likely to use.
Will need a slight further modification to ignore blanks within the table.

Resources