Comments on: Can you help me with an Excel question?
http://ask.metafilter.com/151900/Can-you-help-me-with-an-Excel-question/
Comments on Ask MetaFilter post Can you help me with an Excel question?Thu, 22 Apr 2010 10:25:09 -0800Thu, 22 Apr 2010 10:25:09 -0800en-ushttp://blogs.law.harvard.edu/tech/rss60Question: Can you help me with an Excel question?
http://ask.metafilter.com/151900/Can-you-help-me-with-an-Excel-question
Can you help me with an Excel question? <br /><br /> I have an Excel spreadsheet. Each row represents a customer. Columns include customer ID, then each product that customer uses. Sometimes a customer uses 2 of a product. Each product column contains either the number of that product the customer uses, or a zero. I need the easiest way to figure out something like this: For customers who only have one product, the commonest product is A (ok, that one's easy). For customers who have 2 *different* products, the commonest products are A and B. There are 70 products. I think that to be useful, I would want to blow this out through about 20 of these products. I would appreciate any help!post:ask.metafilter.com,2010:site.151900Thu, 22 Apr 2010 10:12:05 -0800ersatzkatExcelBy: jckll
http://ask.metafilter.com/151900/Can-you-help-me-with-an-Excel-question#2176510
You want the LARGE() function. It is like MAX() but allows you to specify the 2nd largest or 3rd largest (or nth largest) number in an array. Make sure to use CTRL+SHIFT+ENTER when you input the formula.<br>
<br>
If your Customer ID is in col A, and each of your 70 products are in rows B through XX, and your first Customer is in row 2, you want this function:<br>
<br>
<i>=LARGE(B2:XX2,1)</i> will return the largest number<br>
<i>=LARGE(B2:XX2,2)</i> will return the second-largest number, etccomment:ask.metafilter.com,2010:site.151900-2176510Thu, 22 Apr 2010 10:25:09 -0800jckllBy: jckll
http://ask.metafilter.com/151900/Can-you-help-me-with-an-Excel-question#2176514
<i>and each of your 70 products are in <s>rows</s> columns B through XX</i>comment:ask.metafilter.com,2010:site.151900-2176514Thu, 22 Apr 2010 10:26:20 -0800jckllBy: thewildgreen
http://ask.metafilter.com/151900/Can-you-help-me-with-an-Excel-question#2176596
Here I was thinking of some really complex way to solve this using COUNTIF and COUNTBLANK and whatnot. jckll, that is cool. I learnt something new today.comment:ask.metafilter.com,2010:site.151900-2176596Thu, 22 Apr 2010 11:10:26 -0800thewildgreenBy: ersatzkat
http://ask.metafilter.com/151900/Can-you-help-me-with-an-Excel-question#2177128
Apologies if I'm being thick-headed or not plain (and I do appreciate you taking the time to look at this). When I applied this function, it gives the me largest number (or second- or third-largest) in each customer row. What I'm trying to get to is information across all customers. Say I have a list of customers, and they all have some combination of fruits - apples, bananas, oranges, and pears. I need to know what's the commonest fruit amongst all customers. We'll say apples. That's easy for me to get to because all I have to do is total each fruit column. But for every customer that has apples AND another fruit, what's the second commonest fruit? Then third, then fourth.<br>
<br>
In my head, it sounds all professorial and sense-making - "For customers who have only one fruit, the most common fruit is apples. For customers who have two fruits, the most common fruits are apples and pears. For customers who have three fruits, etc".<br>
<br>
Either I don't know how to apply the function that you've so kindly shared, or I don't know how to articulate what I'm really trying to get to. It's been a trying day, I apologize if I'm sending folks 'round the way for something that's really simple.comment:ask.metafilter.com,2010:site.151900-2177128Thu, 22 Apr 2010 16:54:59 -0800ersatzkatBy: mhum
http://ask.metafilter.com/151900/Can-you-help-me-with-an-Excel-question#2177218
How are you defining "commonest"? For example, say you have the following situation:<pre>Customer Apples Oranges Bananas<br>
A 1000 0 0<br>
B 0 1 0<br>
C 0 2 0</pre>In this case, all customers have exactly one fruit. Is the most common fruit Apple (because it occurs 1000 times) or is the most common fruit Orange (because it occurs between two customers)?<br>
<br>
Also, in the following situation:<pre>Customer Apples Oranges Bananas<br>
D 100 0 1<br>
E 0 1 100</pre> Would we say that Apples and Bananas are the most common fruits among customers who have two fruits despite the fact no customer actually has both Apples and Bananas?comment:ask.metafilter.com,2010:site.151900-2177218Thu, 22 Apr 2010 18:10:01 -0800mhumBy: ersatzkat
http://ask.metafilter.com/151900/Can-you-help-me-with-an-Excel-question#2177359
mhum - with your chart (and thank you, the visual helps me), here's what I would glean that fits what I'm trying to say. For customers (A through E) who have one fruit, the most common fruit is oranges. Three of my customers have at least one orange. For customers who have an orange and another fruit, the most common second fruit is bananas - one orange customer has at least one banana, and two have no second fruit.<br>
<br>
Each step through the analysis would take the previous "most common fruit" into the calculation, because what's ultimately important to me is understanding the common fruit groups that people tend to have.<br>
<br>
The number of oranges per customer is not important - the number of customers with oranges is. I now see I might have confused that point when I mentioned above that "sometimes a customer uses 2 of a product". What I should have said is that if a customer doesn't use a product, there's a zero, otherwise there's a "non-zero", which I think must be useful information in some way to get me where I need to go.comment:ask.metafilter.com,2010:site.151900-2177359Thu, 22 Apr 2010 19:43:35 -0800ersatzkatBy: mhum
http://ask.metafilter.com/151900/Can-you-help-me-with-an-Excel-question#2177527
Okay, this is a little more clear now, although I'm still not exactly sure what your ultimate goal is. <br>
<br>
How would your analysis proceed in the following case:<pre>Customer Apples Oranges Bananas<br>
A 1 1 0<br>
B 1 0 0<br>
C 1 0 0<br>
D 1 0 0<br>
E 1 0 0<br>
F 0 1 1<br>
G 0 1 1<br>
H 0 1 1</pre>If I understand correctly, you would first start by observing that Apple is the most common fruit. Next, of all the customers with at least one apple, the most common second fruit is Orange. At this point we would have to stop since there are no customers with three fruits. However, Apple and Orange is <b>not</b> the most common combination of two fruits; the most common pair of fruits is actually Orange and Banana.<br>
<br>
If your goal is really to find the most common pair, triple, quadruple, ..., n-tuple of fruits, this is actually much easier (in my mind) than my understanding of your "previous most common fruit" analysis.comment:ask.metafilter.com,2010:site.151900-2177527Thu, 22 Apr 2010 21:57:54 -0800mhumBy: thewildgreen
http://ask.metafilter.com/151900/Can-you-help-me-with-an-Excel-question#2177530
That is a great example chart by mhum. ersatzkat -- your latest explanation makes the problem completely different and more complex. I cannot think of a way to solve this without using VBA code. It would be not too difficult to do this with VBA (my initial try would be a few nested loops, with the inner-most one parsing the data row-by-row), but I'd be pleasantly surprised if anyone could show how to solve this with just excel formulae.comment:ask.metafilter.com,2010:site.151900-2177530Thu, 22 Apr 2010 22:01:01 -0800thewildgreenBy: ersatzkat
http://ask.metafilter.com/151900/Can-you-help-me-with-an-Excel-question#2177697
mhum - your second example is spot on (and thank you again). If your data set was the one I was working with, I would stop at apple and orange. I wish I could just glance at my own data and figure it out, instead of 70 products and upwards of 18k customer records.comment:ask.metafilter.com,2010:site.151900-2177697Fri, 23 Apr 2010 03:58:07 -0800ersatzkatBy: mhum
http://ask.metafilter.com/151900/Can-you-help-me-with-an-Excel-question#2178089
Okay, I think I have an idea of how I'd do this although it'll be rather manual and tedious. <br>
<br>
But first, I just want to confirm that you're looking for what I think you're looking for. Consider the following expansion of my last example: Customer 1 has Apple and Orange. Customers 2 through 51 have just Apple. Customers 52 through 100 have Banana, Grape, and Strawberry. In your analysis, you'd first start by observing that Apple is the most frequent fruit (Customers 1 through 51). Then, of the customers with Apple, Orange is the most frequent second fruit (in fact, the only other second fruit). Now, do we stop here because there are no customers with Apple, Orange, and some third fruit? Are you more interested in knowing that exactly one customer has Apple and Orange than the fact that just under half of your customers have Banana, Grape, and Strawberry?comment:ask.metafilter.com,2010:site.151900-2178089Fri, 23 Apr 2010 09:36:13 -0800mhumBy: ersatzkat
http://ask.metafilter.com/151900/Can-you-help-me-with-an-Excel-question#2178135
mhum - that's exactly it. I could explain more about why that makes sense to me, perhaps over MeMail. It's work-related and I've probably dropped enough info about my job in some of my answers that it'd be a not-great idea (for me) if I were to air project-type detail here. Thanks so much for being so patient.comment:ask.metafilter.com,2010:site.151900-2178135Fri, 23 Apr 2010 10:25:28 -0800ersatzkatBy: mhum
http://ask.metafilter.com/151900/Can-you-help-me-with-an-Excel-question#2178197
Okay. Here we go. This will be extremely manual so you might want to familiarize yourself with keyboard shortcuts if you haven't already (I'll point them out as I go along).<blockquote>1) Make a copy of your sheet. We'll be modifying your table in this process (e.g.: deleting rows) so we don't want to touch the original data. Let's say that Customers are in Column A and Fruits are in Columns B through BS. I'll assume that Row 1 has the column headers (e.g.: "Customer", "Apple", "Orange", etc...)<br>
<br>
2) Insert a row at the the very top (so that your table now starts in row 2 instead of row 1).<br>
<br>
3) In cell A1, put in a huge number, bigger than the number of rows in your table (say, 99999999). This huge number will be used to keep the Customer column at the left-hand side when we start sorting the table.<br>
<br>
4) In cell B1 put this formula: <tt>=COUNTIF(B3:B18000,">0")</tt> where B18000 is the bottom of the column. Copy this formula across through BS1. Now, cells B1 through BS1 should contain the count of customers who've ordered each fruit.<br>
<br>
5) Select the entire range (i.e.: A1:BS18000). You should be able to do this by hitting Ctrl-A. Hit Alt-D and then S to bring up the sort menu. Sort <b>left to right</b> (in Excel 2007, this is under the Options button) and decreasing based on row 1. Now, Column B should be the commonest fruit.<br>
<br>
6) Select just your table (i.e.: A2:BS18000, excluding row 1). If you don't know how to use Ctrl-Shift-Arrow Keys to select large ranges, now's a good time to <a href="http://matthom.com/archive/2006/06/11/excel-keyboard-navigation">learn</a>. Sort this range from top to bottom, decreasing, based on Column B. <br>
<br>
7) Scan down Column B (visually) until you see zeros (they should all be at the bottom). You can use <a href="http://www.wikihow.com/Apply-Conditional-Formatting-in-Excel">conditional formatting</a> to help highlight where the zeros start, but it's not necessary. Select all the rows with a zero in Column B and delete them. Ctrl-Shift-Arrows will help here too. When you delete these rows, the numbers in row 1 will change.<br>
<br>
8) Do Step 5 again (i.e.: select the entire range, sort left to right decreasing on the first row). Now, Column C should contain the most common fruit among all people who had the fruit in Column B (Column B should remain the same).<br>
<br>
9) Do Step 6, except this time you should sort based on Column C.<br>
<br>
10) Do Step 7, except this time you're scanning down Column C for zeros.<br>
<br>
11) Do Step 5 again.<br>
<br>
12) Do Step 6, except this time you should sort based on Column D.<br>
<br>
13) Do Step 7, except this time you should scan down Column D.<br>
<br>
14) You should get the idea here. Keep looping through steps 5, 6, and 7, each time moving over one column.<br>
<br>
15) When you're done (after 20? iterations), row 2 should have a list of fruits in the order that you want. Column B should be the commonest fruit, Column C should be the commonest fruit associated with the Column B fruit, Column D should be the commonest fruit associated with both fruits in Columns A and B, etc...<br>
</blockquote>If this is a one-shot (or, at least, once in a blue moon) thing, I could see this as tolerable. If this is an all-the-time thing, I'd start thinking about developing a programming solution, either VBA inside Excel or exporting your data out of Excel and using some script-y language like Perl, Python, or Ruby.comment:ask.metafilter.com,2010:site.151900-2178197Fri, 23 Apr 2010 11:20:29 -0800mhumBy: ersatzkat
http://ask.metafilter.com/151900/Can-you-help-me-with-an-Excel-question#2180815
mhum - thanks so much for this. Parental health drama has kept me away this weekend, but I'm taking this to work this morning to give it a go. I'll post the results - thank you!comment:ask.metafilter.com,2010:site.151900-2180815Mon, 26 Apr 2010 03:40:26 -0800ersatzkat