Count unique records with conditional statement in EXCEL - excel

I have a table in Excel.
Id|Gender|Age|Test|Result|Center
1 |MALE |11 | HB | 11 |Center1
1 |MALE |11 | UA | 8 |Center1
1 |MALE |11 | BG | 100 |Center1
2 |FEMALE|12 | HB | 12 |Center1
2 |FEMALE|12 | UA | 10 |Center1
3 |MALE |14 | HB | 14 |Center2
4 |FEMALE|13 | HB | 15 |Center2
I am able to find unique records but not getting how to put a conditional statement over it. Below is the queries I need.
Total males: 2
=SUMPRODUCT((report!D2:D232="MALE")/COUNTIF(report!B2:B232,report!B2:B232&""))
Total Females:2
=SUMPRODUCT((report!D2:D232="FEMALE")/COUNTIF(report!B2:B232,report!B2:B232&""))
I need the following queries.
Total Females with HB result above 14. ??
Total Females with HB result between 10 and 14 in Center1??
EDITS:- Every count should be from the unique counts from males and females.
Thanks in advance.

Total Females with HB result above 14
You can use COUNTIFS to specify multiple criteria in this case:
=COUNTIFS(B2:B232,"FEMALE", D2:D232,"HB", E2:E232, ">14")
This contains three checks:
Does B2:B232 = FEMALE?
Does D2:D232 = HB?
Is E2:E232 > 14?
In the case of your sample data, this outputs 1.
Total Females with HB result between 10 and 14 in Center1
Again, use COUNTIFS with multiple parameters:
=COUNTIFS(B2:B232,"FEMALE", D2:D232,"HB", E2:E232, ">=10", E2:E232, "<=14", F2:F232, "Center1")
In this case, these are the checks:
Does B2:B232 = FEMALE?
Does D2:D232 = HB?
Is E2:E232 >= 10?
Is E2:E232 <= 14?
Does F2:F232 = Center1?
The output in the example data is again 1.

You could also use Format-as-table on the table and set a total row and set it to Count.
This way, as you filter on your conditions in each column, you will get a list with the subjects that fit the query as well as the total number of occurrence within the queried. This will however not find the unique Id's. See bootom of answer on how to do that with a matrix-formula
Result for query: Female with HB above 14:
Result for query: Female with HB between 10 and 14 in Center1:
To count unique ID's in the filtered table use this matrix-formula (apply with CTR+SHIFT+ENTER):
=SUM(--(FREQUENCY(IF(SUBTOTAL(3; OFFSET(A2:A8; MATCH(ROW(A2:A8); ROW(A2:A8))-1; 0; 1));COUNTIF(A2:A8;"<"&A2:A8);"");COUNTIF(A2:A8;"<"&A2:A8))>0))
More info on this formula can be found here: https://www.get-digital-help.com/count-unique-distinct-values-in-a-filtered-table/
This would look like this:

You could modify the below:
Formula:
=SUMPRODUCT(($A$2:$A$8=H3)*($B$2:$B$8=I3)*($C$2:$C$8=J3)*($D$2:$D$8=K3)*($E$2:$E$8=L3)*($F$2:$F$8=M3))
Results:

For Office 365 Excel:
If you want a
Count of unique individuals meeting your criteria
A method of easily changing the criteria
And assuming that
ID identifies the individuals
You have Excel 2016 with the new UNIQUE and FILTER functions
I suggest the following which will use the Advanced Filter
*You could also use a simple filter, but it may be a bit more difficult to set up the varying filter criteria, and they would not be visible in the results display**
Turn your data range into a Table and name it myTbl
Add a Helper column named Visible
Optionally you may HIDE this column
Formula for helper column: =SUBTOTAL(3,[#ID])
*This will return 0 if row is hidden, else 1
Use this formula for a Unique Count: =COUNTA(UNIQUE(INDEX(FILTER(myTbl[ID],myTbl[Visible]=1),0,1)))
Set up a Criteria Range where you can type in the Criteria
See HELP for Advanced Filter for more information
Examples: (I changed your data a bit to test for uniqueness)
Base Data and empty Filter
Total Females with HB result between 10 and 14 in Center1
If you prefer not to use a filter, or a helper column, here is a formula to return this same last set of criteria:
=COUNT(UNIQUE(INDEX(FILTER(myTbl,(myTbl[Gender]="FEMALE")*(myTbl[Test]="HB")*(myTbl[Result]>=10)*(myTbl[Center]="Center1")),0,1)))
You can use this as an example as to how to create formulas to return a unique count of any other sets of criteria.
EDIT:
If you choose to use the Advanced Filter method with the Visible helper column, and if your ID's are all numeric, you can use this formula to obtain the Unique Count:
=SUM(IF(FREQUENCY(IF(myTbl[Visible],myTbl[Id]),IF(myTbl[Visible],myTbl[Id]))>0,1))
In versions of Excel that do not have dynamic array formulas, you must confirm the formula by holding down ctrl + shift while hitting enter. If you do this correctly, Excel will place braces {...} around the formula seen in the formula bar.

Related

Excel countif and sumif together

I am trying to write a formula in Excel which will count how many times we have sold less than 50 of a particular product. For example, here is a day's sales:
Order | Product | Qty
1 | A | 5
2 | A | 5
3 | A | 5
4 | B | 30
5 | C | 75
I want a formula in a cell which says how many times we have a requirement for less than 50 of a certain product. So in the example above, there is a total of 15 As, 30 Bs and 75 Cs, so 2 of those are less than 50.
I think it will need to be an array function of COUNTIF and SUM, but can't figure it out.
You could use this formula:
=SUMPRODUCT(--(IF(ROW($B$2:$B$10)=MATCH($B$2:$B$10,$B$1:$B$10,0),SUMIF($B$2:$B$10,$B$2:$B$10,$C$2:$C$10),"")<50))
Note: It's an array formula and must be entered through Ctrl+Shift+Enter
Product order placement can be randomized and does not have to be in order.
Another way
=SUMPRODUCT((SUMIF(B2:B10,B2:B10,C2:C10)<50)/COUNTIF(B2:B10,B2:B10))
Maybe something like that will help:
=SUMPRODUCT(--IF($B$2:$B$11<>$B$1:$B$10,SUMIF($B$2:$B$11,$B$2:$B$11,$C$2:$C$11)<50,0))
Note that this is an array formula so needs to be entered with Ctrl+Shift+Enter. Data needs to be sorted by Product (i.e. product A cannot appear in random rows, like row 2, 20 and 100; it needs to be grouped together).
Result:

How to sort a column based on exact matches with another column

I have an inventory table that looks like this (subset):
part number | price | quantity
10115 | 14.95 | 10
1050 | 5.95 | 12
1074 | 7.49 | 8
110-1353 | 13.99 | 22
and i also have another table in sheet 2 that looks like this (subset):
part number | quantity
10023 | 1
110-1353 | 3
10115 | 2
20112 | 1
I want to basically subtract the quantities in the second table from the ones in the first table. What is the best way of doing this? I have looked in to VLOOKUP and INDEX MATCH but they are not quite right for this. Would this perhaps actually be better in say an Access DB ?
I have add another two columns next to sheet 1 last column. Let us assume that the second table range is A1:B5.
Image:
Formulas:
Column D:
=IFNA(VLOOKUP(A2,Sheet2!$A$2:$B$5,2,FALSE),0)
Column E:
=C2-D2
If you wanted to tackle this using MS Access, the SQL code might look like this:
select
t1.[part number],
t1.price,
t1.quantity - nz(t2.quantity, 0) as qty
from
inventory t1 left join table2 t2 on t1.[part number] = t2.[part number]
Here, I assume that you have a table called inventory and a table called table2 (change these to suit your database).
A left join is used to ensure that all records from inventory are returned, regardless of whether a match is found in table2, and the Nz function is used to return 0 for records for which there is no part number match in table2.

Search a Column for Specified Text, Then Return the Sum of the adjacent column which contains values to sum

I am creating a World Cup spreadsheet.
Example:
Team | Pts |
Brazil | XXX |
Switz. | XXX |
C. Rica| XXX |
Serbia | XXX |
I have another table which displays the Country, Score & whether it was a W (win), L (loss) or T (tie). A WIN will add 3 points, a TIE will add 1 point & a LOSS will add 0 points. Where I put the XXX, I want a formula that will search the table column for the Country Name and ADD up the W's, L's & T's and display the sum of every game there team played with the point system I mentioned.
Any help would be greatly appreciated! Thanks.
Try,
=SUM(COUNTIFS(D:D, A2, E:E, {"T","W","W","W"}))
You can use COUNTIFS. I set up a mock table to demonstrate:
The first COUNTIFS will count a given teams wins, and then multiple by a factor of 3.
The second COUNTIFS will count a given teams ties (no need to multiply here).
Since a Loss equates to 0 points, there is no need to sum or count anything.
Adjust ranges to fit your setup and then auto-fill the equation down your table to calculate for each team.
You can use a formula in H2, as below, and drag down as many rows as required
=(3*SUMPRODUCT(--($A$2:$A$13=$G2),--($C$2:$C$13="W"))+SUMPRODUCT(--($A$2:$A$13=$G2),--($C$2:$C$13="T")))
Data:
You only care about Wins or Ties. So count the wins per country and * 3 and add to that the count of ties, which you don't need to multiply as is 1.
Sumproduct is used to handle the arrays nicely.

How to calculate values dynamically from Excel table

I have a programming issue in Excel that I don't know how to solve it. I want to create an automatic Delivery Cost Program on Excel that will help me calculating the cost more easily.
The input variables are:
Quantity (Values for 1, 2-9,10-49,50+ and more)
Shipping method
Depending on the Quantity Value and Shipping method, Excel should lookup on the table and return the total shipping cost according the following Table:
------------------------------------------
Delivery | Per shipment fee
------------------------------------------
| 1 2-9 10-49 50+
------------------------------------------
Standard | 2,99 1,89 1,5 1,1
Expedited | 5,99 2 1,75 1,25
Priority | 10,99 3,39 2,25 1,35
------------------------------------------
Let me show you with some examples what I want to get:
1- Example:
- Quantity: 15
- Delivery: Expedited
- Total Cost = 15 * 1,75 = 26,25$
1,75$ is the returned value after looking on the table using the variable Quantity and Shipping Method.
I have tested doing =IF statements but sure that there is an easier way to do it.
I'm not very good on Excel programming so any help will be appreciated
Best regards and have a great day!
Assuming that your table has the delivery types in column A in rows 4 through 6 and that the quantities are in row 3 (columns B through E) the following formula should do it for you:
=INDEX(B4:E6,MATCH(B9,A4:A6,0),MATCH(C9,B3:E3,1)) * Quantity
Note, that the quantities in row 3 must be a number. So, the numbers should be 1, 2, 10, and 50 and not 1, 2-9, 10-59, 50+. There are two possibilities to achieve that:
Create a helper row and hide it (while only showing the row with the "names" as you wish for.
Change the number format on these cells: for the column containing the 2 the number format should then be "2-9" (custom number format). For the number 10 the number format would be "10-49" and the number format for the last column would be "50+". Like this you see what you wan to see while the cells still contain numbers only (for the upper formula to work correctly).

Executing a function over a grouped set

I have two columns of data in Excel, one containing dates and the other values 1 or 2 which represent a specific status (like an enum).
Here is an example data set:
2014/07/04 | 1
2014/07/04 | 1
2014/07/04 | 2
2014/07/04 | 1
2014/07/05 | 2
2014/07/06 | 1
2014/07/06 | 1
2014/07/06 | 2
I need to get a graph of the percentages of the number 1 over days; in the above example
July 4th: 75%
July 5th: 100%
July 6th: 66%.
I've tried pivot tables and charts with no luck because I can't write my own function for values (COUNTIF for ones divided by COUNT), only use the predefined ones which aren't of any use.
Does anyone know how I would go about doing this?
You could do this with a pivot chart.
I made up my own data so it doesn't quite match but it is the same idea.
For the pivot table fields I used
Legend Fields:Compare
Axis Fields:Date
Values: Count of compare
For values under Value field settings goto show value as and change this to % of column total and summarize value by Count.
If you only want to see number one just put a filter on the column labels.

Resources