Count number occurrences of each string in a column [duplicate] - excel

This question already has answers here:
How many times does each value appear in a column?
(3 answers)
Closed 6 years ago.
I have an excel table which contains information about bugs containing columns such as Product, Owner, etc.
The Product column contains values (strings) such as PTS, SDE, etc.
How do I find the count of occurrences of each of product (PTS or SDE)?
For example, the following table -
╔════╦══════════════╦═════════╗
║ id ║ Product ║ Owner║
╠════╬══════════════╬═════════╣
║ 1 ║ PTS ║ thiru║
║ 2 ║ PTS ║ sajeesh║
║ 3 ║ SDE ║ varun║
║ 4 ║ SPB ║ ranjith║
╚════╩══════════════╩═════════╝
when queried for Product column would return
PTS - 2
SDE - 1
SPB - 1

The easiest solutions are PivotTable, Power Query, PowerPivot, etc.
Unlike the formula solutions, they do not auto-update when the source range is changed, but you can configure how often they are refreshed or refresh them manually.

Product Formula Result
PTS =countif(Product_Range, "PTS") 2
SDE =countif(Product_Range, "SDE") 1
SPB =countif(Product_Range, "SPB") 1
The countif formula is looking through the data range with product names, and counting the number of items that matches the name you specified in the formula.
Here are the arguments that countif takes
=countif(range, criteria)

Related

Check if a Cell Value is between Two Values using Vlookup

In Excel, I have a table as follows, which shows pricing based on volume.
If you buy up to 4 items per month, the unit price is $100, 5 to 8 is $90, 9 to 20 is $80, anything above 20 is $50.
A | B | C
----------------
1 | 4 | 100
5 | 8 | 90
9 | 20 | 80
21| 1000 | 50
I have my monthly purchase volumes in another column, say d:
D
--
3
6
2
4
3
10
7
7
10
2
I need to find the unit prices (C column values) based on this series falling between the values of A and B columns. I know I can use a compound if statement like =IF(AND(D$1>=A1,B1>=D$1),C1,0) ... but since my pricing table is actually much larger than my example, this approach becomes convoluted. How can I do this with a Vlookup in an elegant way?
I'd go with the following in E1:
=INDEX(C$1:C$4,MATCH(D1,A$1:A$4))
which, at worst should be just as fast as VLOOKUP but at best is much faster.
This can be done by dragging the following formula down to cover the full column D:
=LOOKUP(2,1/($A$2:$A$5<=D2)/($B$2:$B$5>=D2),$C$2:$C$5)
This will take each D value, compare with A and B, locate which bucket it falls into, and pull the C value. If not found, will return an N/A.
Here is an approach using SUMIFS:
=SUMIFS($C$1:$C$4,$A$1:$A$4,"<="&E1,$B$1:$B$4,">="&E1)

How to sum values in excel only if they match a category id in a lookup

Say I have a product category lookup like so:
Sheet 1
Product Name | Product Category
--------------------------------------
product 1 | A
product 2 | A
product 3 | B
product 4 | A
product 5 | B
product 6 | C
and I also have a list of purchases which only use Product Name like this:
Sheet 2
Product Name | Purchase Quantity
---------------------------------------
product 1 | 35
product 4 | 10
product 5 | 5
I would like to produce a rollup like this:
Product Category | Purchase Quantity
------------------------------------------
A | 45
B | 5
C | 0
I've tried a variety of ways to solve this like:
SUMIF(LOOKUP('Sheet 2'!A2:A6,'Sheet 1'!A:A,'Sheet 2'!B:B), "=A", 'Sheet 2'!B2:B6)
SUMPRODUCT(LOOKUP('Sheet 2'!A2:A6, 'Sheet 1'!A:A, 'Sheet 2'!B:B)="A"*'Sheet 2'!B2:B6)
Excel doesn't like the first one. It says the formula is incorrect, but I'm not seeing why. The second one yields #VALUE. Any help on this would be much appreciated. Thanks in advance!
With A in D2, use this as an array formula.
=SUM(SUMIFS(Sheet2!B$2:B$4, Sheet2!A$2:A$4, IF(Sheet1!B$2:B$7=D2, Sheet1!A$2:A$7)))
Array formulas need to be finished with Ctrl+Shift+Enter, not just Enter.
In my opinion there is no need to use complex formulas for such an easy question. Just add another column next to Purchase Quantity sheet 2 to get the Product Category and simply use =SUMIF. I have prepare a solution to illustrate my thoughts:
Formula for VLOOKUP:
=VLOOKUP(D2,$A$2:$B$7,2,FALSE)
Formula for SUMIF:
=SUMIF($F$2:$F$4,"=" & A10,$E$2:$E$4)
Results:

Using COUNTIFS on Multiple Columns

I have a table named "Input" that takes the form below, with the actual table extending to around 20 columns and over 1000 rows. The "Site" column on the left will regularly have repeat data, but it will never be the case that, for example, W01 appears twice in the same row - though Site01 may have W01 in twice, just in different rows.
Site | Item1 | Item2 | Item3
------------------------------------------
Site01 | W01 W02 S01
Site02 | W01 S02
Site03 | S01 W01
Site01 | W01
I then have another table called "Results" with all of the sites along the top row and all of the items along the leftmost column. The idea would be to have the table show how many times a site used a given item, so the result from the above table should be:
| Site01 | Site02 | Site03
---------------------------------
W01 | 2 1 1
W02 | 1 0 0
S01 | 1 0 1
S02 | 0 1 0
It seems relatively easy to do this over one column in the Input table using a COUNTIFS formula that takes the form:
=COUNTIFS('Input'!$C$4:$C$1000,'Results'!C$2,'Input'$D$4:$D$1000,'Results'!$B3)
Which can then be copied across the entire "Results" table.
I can use this approach over two or three columns by stringing together multiples of the above COUNTIFS with pluses, and just changing the next COUNTIFS to check 'Input'$E$4:$E$1000 in the second part of the COUNTIFS - but this quickly becomes unfeasible over a sheet with up to 20 rows - so I need a formula or VBA code which does exactly as this COUNTIFS approach does, but that scales over a much larger number of rows.
Switch to SUMPRODUCT:
=SUMPRODUCT((Input!$C$4:$C$1000=Results!C$2)*(Input!$D$4:$F$1000=Results!$B3))
Amend the F in the portion:
Input!$D$4:$F$1000
as desired.
Regards

How do I specifiy the last row of a list in a range as it grows?

I have a list of items sold which grows longer with each sale. On a summary page I want to sum particular rows of a column.
Example:
Sheet1
Person Item Amount
1 5 6.00 This page has the knows the last column
1 7 10.50 by =counta($a2:$a20)+1 in cell f6
2 5 6.00
3 2 20.00
3 9 16.00
3 5 6.00
4 1 2.50
Summary Page:
Person Sales
1 =sumif(sales!$A$2:$A$8,$a2,sales!$C$1:$C$8) = 16.50
2 =sumif(sales!$A$2:$A$8,$a3,sales!$C$1:$C$8) = 6.00
3 =sumif(sales!$A$2:$A$8,$a4,sales!$C$1:$C$8) = 42.00
4 =sumif(sales!$A$2:$A$8,$a5,sales!$C$1:$C$8) = 2.50
On one page I create an indirect reference by =concatenate("sales!$c$2:$c$",sales!$a$10) but that won't work on this one page. Is there another way?
Please try:
=SUMIF(sales!A:A,A2,sales!C:C)
copied down to suit.
I would use dynamic named ranges:
=SUMIF(Sales!People,A2,Sales!Amounts)
Here is an article on how to create dynamic named ranges:
http://www.excel-easy.com/examples/dynamic-named-range.html
If this is really a "Table", in other words, you used the Insert Table method from the Insert Ribbon to set it up, then you can merely used structured references which will automatically expand as you add rows.
An equivalent of your formula, using structured references, and assuming you have renamed your table SalesTbl, might look like:
=SUMIF(SalesTbl[Person],$A2,SalesTbl[Amount])

Dynamic height directive to autofill remaining area

Is there any good implementation of a directive that dynamically change heights of element to fill remaining container's height?
In my layout I've header (actually menu) fixed height in pixels, content area (that could overflow above window height so scroll bars should appear) and sticky footer with fixed height. Also on a left and right of content area there could be panels
So something like that:
┌──────────────────┐
│ Header │ ~100px
├─┬──────────────┬─┤
│P│ ▲│P│
│a│ Content ▒│a│ fill remaining area, with scroll bars
│n│ ◙│n│
│e│ ▒│e│
│l│ ▼│l│
├─┴──────────────┴─┤
│ Footer │ ~100px
└──────────────────┘
I've tried such solutions:
Using height: 100%. But as header/footer are in pixels, so its hard to measure their height in percents, this solution din't solved problem.
Using haks like "display: table-row" didn't allowed to have scroll bars for content area
As it is quite common layout for web sites, there are quite a lot of solutions using JS, but mostly all of them were not generic - or without possibility to have scroll bars, or panels would not be possible.
So I as understand, the only solution is to use JavaScript for adjust height of en element, so the best place where to put is directive.
Do anybody have some implementation of directive for this task?
Currently I've this solution:
http://plnkr.co/edit/Nimw68KXJRCU3RZzu3Bc?p=preview
Directive fill work this way::
Calculate padding-bottom + margin-bottom + border-bottom-width of all elements, that contains current element
Add heights of all elements beneath the current element and beneath all elements, containing current element
Subtract from window height offsetTop of the element and result of previous calculations
Set element's height to resulting value
So the idea of height calculation (actually height of area beneath of current element) is:
╔══════════════════════════╗
║ ║
║ ╔═════════════╗ ║
║ ║ Element ║ ║
║ ║ to autofill ║ ║
║ ............... ║
║ ╚═════════════╨───────╫─────────
║ ║ + margin/padding
║ ╔════════════════════╥──╫─────────
║ ║ some element ║ ║
║ ║ below ║ ║ + height
║ ║ ║ ║
║ ╚════════════════════╨──╫─────────
║ ║ + margin/padding
╚══════════════════════════╨─────────
There are some restriction:
I assume that widths of paddings, margins, borders are specified in pixels (convert from other units seems to be quite complex)
I didn't tested it on other browsers except Chrome.
Improvements are welcome!

Resources