CountifS + multiple criteria + distinct count - excel

I'm looking for a formula calculating : distinct Count + multiple criteria
Countifs() does it but do not includes distinct count...
Here is an example.
I have a table on which I want to count the number of distinct items (column item) satisfying multiple conditions one column A and B : A>2 and B<5.
Image description here
Line Item ColA ColB
1 QQQ 3 4
2 QQQ 3 3
3 QQQ 5 4
4 TTT 4 4
5 TTT 2 3
6 TTT 0 1
7 XXX 1 2
8 XXX 5 3
9 zzz 1 9
Countifs works this way : COUNTIFS([ColumnA], criteria A, [ColumnB], criteria B)
COUNTIFS([ColumnA], > 2 , [ColumnB], < 5)
Returns : lines 1,2,4,5,8 => Count = 5
How can I add a distinct count function based on the Item Column ? :
lines 1,2 are on a unique item QQQ
lines 4,5 are on a unique item TTT
Line 8 is on a unique item XXX
Returns Count = 3
How can I count 3 ?!
Thanks
You can download the excel file # Excel file

Newer versions of Excel allow for this problem to be solved in a (relatively) more simple way. It certainly is easier to follow and understand, conceptually.
First, filter the table based on multiple criteria (join multiple with the *):
=FILTER(Table,(Table[Column A]>2)*(Table[Column B]<5))
Then, grab the "Item" column with INDEX:
=INDEX(FILTER(Table,(Table[Column A]>2)*(Table[Column B]<5)),,2)
Next, filter for unique entries:
=UNIQUE(INDEX(FILTER(Table,(Table[Column A]>2)*(Table[Column B]<5)),,2))
Finally, perform a count:
=COUNTA(UNIQUE(INDEX(FILTER(Table,(Table[Column A]>2)*(Table[Column B]<5)),,2)))

Ugly formula, but it works.
=SUM(((FREQUENCY(IF(C2:C10>2,1,0)*IF(D2:D10<5,1,0)*(COUNTIF(B2:B10,">"&B2:B10)+1),ROW(B2:B10)-ROW(B2)))*(ROW(B2:B11)-ROW(B2))>0)*1)
I'll start with the criteria IFS:
IF(C2:C10>2,1,0)*IF(D2:D10<5,1,0)
Gives an array of 1s and 0s for the rows that satisfy both criteria. ARRAY = {1;1;1;1;0;0;0;1;0} for your example.
Where B2:B10 is the Item column, the countif formula:
COUNTIF(B2:B10,">"&B2:B10)
returns {6;6;6;3;3;3;1;1;0} where the number equals the number of item values in B2:B10 alphabetically less than the tested item value.
QQQ goes to 6 [3"TTT", 2"XXX", 1"zzz"]
TTT goes to 3 [2"XXX", 1"zzz"]
XXX goes to 1 [1"zzz"]
zzz goes to 0 [0 less than "zzz"]
Need to add 1 to this array to make sure there are no 0 values:
{7;7;7;4;4;4;2;2;1}.
So when multiplying the criteria, and the countif statement:
(IF(C2:C10>2,1,0)*IF(D2:D10<5,1,0)*(COUNTIF(B2:B10,">"&B2:B10)+1)
You get ARRAY = {7;7;7;4;0;0;0;2;0}.
FREQUENCY(ARRAY,ROW(B2:B10)-ROW(B2))
ROW(B2:B10)-ROW(B2) sets the frequency bins to {0;1;2;3;4;5;6;7;8}. So the output of the frequency formula is {4;0;1;0;1;0;0;3;0;0} where the last 0 is for all values greater than 8.
((ROW(B2:B11)-ROW(B2)>0)*1) equals {0;1;1;1;1;1;1;1;1;1}. Multiplying ARRAY by this removes the 0 count at the start: ARRAY = {0;0;1;0;1;0;0;3;0;0}. [NOTE: B11 is lowest item column cell+1 because of the added array value from the frequency formula for values over 8]
(ARRAY)>0)*1 = {0;0;1;0;1;0;0;1;0;0}
SUM this = 3.
ctrl + shift + enter, because it's an array formula.
cmd + shift + enter for mac.

You could try this:
=SUMPRODUCT(1/COUNTIF(B2:B10,B2:B10))
Credit where credit due, however ... I found it over here:
https://exceljet.net/formula/count-unique-values-in-a-range-with-countif

Related

Presenting a value based on number or text in cell

I have a list of 4 values in Sheet1 and 4 values in Sheet2.
In Sheet3 I will combine a random selection of these numbers and return the value in a column. (edit: no random selection from Excel, its a part picked from a bucket)
(A fifth column in Sheet3 will be used to do calculations with ValueS1 and ValueS2)
Sheet1
NumberS1
ValueS1
1
17.10
2
17.20
3
17.12
4
17.15
Sheet2
NumberS2
ValueS2
1
16.10
2
16.20
3
16.12
4
16.15
Sheet3
NumberS1
NumberS2
ValueS1
ValueS2
1
3
17.10
16.12
2
2
17.20
16.20
4
1
17.15
16.10
3
4
17.12
16.15
What kind of function can give the desired return?
I have looked into examples using "Indirect" but cannot see how they will solve my problem.
for the randomization: =ROUNDUP(RAND()*4,0)
rand() gives you a number between 0 and 1, so rand()*4 gives you a number between 0 and 4.
roundup(x,y) round up the number x with y digits you want to round the number up to (in our case 0).
for import the right number from sheet 1 or 2: =VLOOKUP(A1,Sheet1!A1:B2,2,0)
A1 - The value you look for in sheet 1 or 2.
Sheet1!A1:B4 - The array he look for your value on the firs column, always on the first column.
2 - The column you want to import the value from. (because we write an array of tow columns. we can write here only 1 or 2)
0 - it's an Optionally index (0 or 1). o is if you want an exact match of the return value.
Regular Lookup could do:
=LOOKUP(A2:A5,Sheet1!A2:A5,Sheet1!B2:B5) in Sheet3!C2
And
=LOOKUP(B2:B5,Sheet2!A2:A5,Sheet2!B2:B5) in Sheet3!D2
Note that LOOKUP will give the result to the closest match smaller than the search value.
Or VLOOKUP:
=VLOOKUP(A2:A5,Sheet1!A2:B5,2,0) / =VLOOKUP(B2:B5,Sheet2!A2:B5,2,0)
VLOOKUP will error if the value is not found (the way used above). It uses arguments like this:
=VLOOKUP(What you want to look up, where you want to look for it, the column number in the range containing the value to return, return an Approximate or Exact match – indicated as 1/TRUE, or 0/FALSE)
Office 365 has XLOOKUP which combines the logic of the two above and some more:
=XLOOKUP(A2:A5,Sheet1!A2:A5,Sheet1!B2:B5,"value not found",0)
XLOOKUP uses the following arguments:
=XLOOKUP(lookup_value, lookup_array, return_array, [if_not_found], [match_mode], [search_mode])

Excel - assign values based on the first unique item

I have got an excel question that I can not answer. Here is my table:
ID Key Count Unique Available Text Results
1 0 Text-1 Dupe-Y
2 1 Y Text-1 Y
3 0 Text-1 Dupe-Y
4 0 Text-1 Dupe-Y
5 1 N Text-2 N
6 1 Y Text-3 Y
7 0 Text-2 Dupe-N
8 0 Duplicate Text-2 Dupe-N
9 0 Duplicate Text-2 Dupe-N
10 0 Y Text-2 Dupe-N
Id Key is just unique key.
Count unique picks up the first time each value in column Text appears. Available can have Y, N, Duplicate and Text is the main column I need to analyze my table. The Results are for the first time each value in Text appears (Count unique = 1), if there is a value in Available then that is the value I need, if Count Unique is 0 then is either Dupe-Y or Dupe-N depending on the value in Available.
I tried with a formula like this one but got stuck after initial progress. =IF(B2=0,"",IFERROR(IF(COUNTIF(D:D,D2)>1,IF(COUNTIF($D:$D,D2)=1,"",C2),1),1))
Note that the column Results is the one I need to populate with a formula that is not affected by sorting or lack of it.
I guess you got all those values and you just need a formula for column Results.
My formul will work only if the data is sorted like in your example. If sorting changes, formula will fail:
My formula is:
=IF(B2=1;D2;"Dupe-"&RIGHT(G1;1))

Sum values of one column depending on the ID of another

I have a table with many values as such (this is an oversimplified example):
IDx
Namex
Pricex
1
a
5
2
b
2
1
a2
5
3
c
3
2
b2
9
and another table with only the ID, in which I'd like to add a column that shows the addition of all the values that match that ID, in this example:
IDy
Totaly
1
10
2
11
3
3
I'm guessing this is a combination of vlookup with sum or sumif, I've tried so far:
=SUM(VLOOKUP(IDy1,$IDx$1:$IDx$5,$Pricex$1:$Pricex$5),// don't know how to proceed here
Try this:
B5:B9 = IDx
B16,D5 = Price
=SUMIF(B$5:B$9,B16,D$5:D$9)

Retrieve column 4 from Column 2 and 3 which contains minimum and maximum conditions along with Column 1 which is a separate value?

Hello I have a table shown below where I have letters in column 1, and min and max ranges for column 2 and 3. I am trying to retrieve the final number in column 4.
I know I can use a VLOOKUP and set the range as TRUE to get the last column. However, how would I factor in multiple columns/criteria to find match the correct range with the correct letter.
For example, I can would like to get value 4 from the last column. I would have to match with "B" and it would be between 0 and $50,000.
A 0 $50,000 1
A $50,001 $100,000 2
A $100,001 $250,000 3
B 0 $50,000 4
B $50,001 $100,000 5
B $100,001 $250,000 6
C 0 $50,000 7
C $50,001 $100,000 8
C $100,001 $250,000 9
Thank you!
Two ways:
If the pattern is the same as to the breaks of the dollar amounts then use this:
=INDEX(D:D,MATCH(G1,A:A,0)+MATCH(H1,$B$1:$B$3)-1)
Where MATCH(G1,A:A,0) returns the first row where the ID is located and MATCH(H1,$B$1:$B$3) finds the relative location of the price in the first pattern. Change $B$1:$B$3 to encompass the whole pattern.
If the patterns are different then you can use this:
=SUMIFS(D:D,A:A,G1,B:B,"<=" & H1,C:C,">=" & H1)
One more for the future when Microsoft releases FILTER():
=FILTER(D:D,(A:A=G1)*(B:B<=H1)*(C:C>=H1))
This is entered normally and does not matter the pattern.

Spreadsheets: how do I SUM values in a column, only if the text column has a 1?

Let's say I have this data
4 1
4 0
4 1
3 0
5 1
How do I write a function (using SUM or something like that) to add all the values on the left, if the values on the right are 1, or true
The total should be 13
Assuming columns A and B are used...
Check just for 1:
=SUMIF(B1:B5,"=1",A1:A5)
If you want to check for TRUE also:
=SUMIF(B1:B5,"=1",A1:A5) + SUMIF(B1:B5,"=TRUE",A1:A5)
sort by column b, then auto-sum the values you want in column a. cheap & lazy solution.

Resources