ADVANCED EXCEL HELP: Divide when multiple conditions are satisfied. - excel

Hi I am currently working on a large data spreadsheet and want to divide only when all conditions are satisfied. Below is the sample data
Article UOM Quantity
1002121 CAS 500
1002121 EA 1
1002121 INN 10
1002121 LAY 2,000
1002121 PAL 10,000
1002127 CAS 500
1002127 CS1 250
1002127 CS2 10
1002127 EA 1
1002127 INN 10
1002127 LAY 3,000
1002127 PAL 12,000
1002129 CAS 500
1002129 CS1 250
1002129 EA 1
1002129 INN 10
1002129 LAY 1,750
1002129 PAL 7,000
Column 1 is SKU nos, Column 2 is unit of measure and column 3 is quantity
What I want to know is how many cases(CAS) = one layer(LAY) when SKU is same.
E.g.: for SKU 1002121, one layer will be equal to 4 cases (2000/4)(LAY/CAS).
Hence I need a formula which satisfies three conditions
1. SKU must be the same
2. when column b=LAY and Column B=CAS then divide column C and show result in column D in layer's Row
Thanks for your help. Any suggestion is welcomed.

This expression is rather a complicated one:
=IF(B2="CAS",SUMIFS($C:$C,$A:$A,$A2,$B:$B,"LAY")/SUMIFS($C:$C,$A:$A,$A2,$B:$B,"CAS"),"")
(or
=IF(B2="CAS";SUMIFS($C:$C;$A:$A;$A2;$B:$B;"LAY")/SUMIFS($C:$C;$A:$A;$A2;$B:$B;"CAS");"")
if your locale uses as delimiters ; instead of ,)
Put in in the cell D2 and copy it to cells in the column D just bellow it.
Explanation:
IF decides if it is a cell where you want put the result.
The result is a quotient. Both the dividend and divisor use almost the same formula.
SUMIFS($C:$C,$A:$A,$A2,$B:$B,"LAY") details:
$C:$C is the range of numbers for creating the sum (in your case from only 1 number)
$A:$A is the range for making comparisons with:
$A2 a value to be compared with
Next 2 parameters have the same meaning as 2nd and 3rd, they make an another condition.
So there are 2 conditions. They are evaluated again and again for each pair of corresponding cells in the columns A and B (so A2 and B2, A3 and B3, etc.) and if both of them are satisfied, the corresponding cell from the 1st argument (i. e. C2, C3, etc.) are included to the sum (in your case at most 1 cell).

You could make a pivot table with your table as source. You would then put SKU in the filter and UOM in column fields. In the UOM you just filter the CAS and LAY. Then you get your numbers for CAS and LAY, which you just need to divide. You can also do the division in a pivot table calculated field.

Related

Multiple Complex IF conditions using 3 Tables

If negative value is found in Total row (A2) in Table 1
Then look for fruit with the highest qty in that month in Table 2
Add the fruit qty to Table 1, a month prior so that the Total is equal to or greater than 0 at an incremental qty found in Table 3.
Please note that Total is sum of all fruit qty + previous month's qty - demand (not shown), it's cumulative.
So basically, we have -5 in March in Table 1.
Apple is the highest in March in Table 2.
So add 8 (2 boxes of Apple) to C4 (a month prior to March) in Table 1
I'm not sure if this is even possible using just a formula, maybe VBA is needed?
If this can be done using a formula would go into B3:G5
Put this in B3,
=IF(AND(C$2<0,MAX(C$8:C$10)=C$10),IF(MAX(C$8:C$10)=C$8,(INT((0-C$2)/$B$13)+1)*$B$13,IF(MAX(C$8:C$10)=C$9,(INT((0-C$2)/$B$14)+1)*$B$14,IF(MAX(C$8:C$10)=C$10,(INT((0-C$2)/$B$15)+1)*$B$15,C$2))),"")
and drag to G3. then put this in B4,
=IF(AND(C$2<0,MAX(C$8:C$10)=C$8),IF(MAX(C$8:C$10)=C$8,(INT((0-C$2)/$B$13)+1)*$B$13,IF(MAX(C$8:C$10)=C$9,(INT((0-C$2)/$B$14)+1)*$B$14,IF(MAX(C$8:C$10)=C$10,(INT((0-C$2)/$B$15)+1)*$B$15,C$2))),"")
and drag to G4. then put this in B5,
=IF(AND(C$2<0,MAX(C$8:C$10)=C$9),IF(MAX(C$8:C$10)=C$8,(INT((0-C$2)/$B$13)+1)*$B$13,IF(MAX(C$8:C$10)=C$9,(INT((0-C$2)/$B$14)+1)*$B$14,IF(MAX(C$8:C$10)=C$10,(INT((0-C$2)/$B$15)+1)*$B$15,C$2))),"")
and drag to G5.
Idea : convert input to positive value, find max, amount to order = ( (Positive input/incrementalQty)+1 ) * incrementalQty
Have a check.

Median Selling Price Excel Table

I have a spreadsheet with different products, listing units and retail value sold like the example below
Product Units Value
A 10 100
B 15 80
C 30 560
I'd like to compare the Average Selling Price with the Median Selling price, so I am looking for a quick formula to accurately calculate the median.
The median function requires the entire series, so for Product A above I would need 10 instances of 10 etc. How can I calculate the Median quickly considering the condensed form of my data?
Without writing your own VBA function to do this there are a couple of approaches that can be taken.
The first expands the data from its compressed frequency count format to generate the full set of observations. This can be done manually or formulaically. On the assumption the latter is required, it can be achieved using a few columns.
All the blue cells are formulae.
Column Eis simply the cumulative of column B and F is an adjusted version of this. Column H is just the values 1 to 55, the total number of observations given by cell L2. Column I uses the MATCH() with its final argument as 1 to match each observation in H against the adjusted cumulative in F. Column J uses the INDEX() function to generate the value of the observation. (Observations 1-10 have value 100, 11-25 have value 80 and 26-55 have value 560 in this example). The MEDIAN() function is used in cell M2 with column J as its argument.
This approach can be refined to take account of varying numbers of products and data points through the use of the OFFSET function to control the range arguments of the MATCH(), INDEX() and MEDIAN functions. And, of course, adjacent cells in columns I and J could be combined using a single formula - I've shown them separately for ease of explanation.
The second approach involves sorting the data by value (so in this case the data rows would become Product B in row 2, product A in row 3 and product C left as-is in row 4). It is then a case of identifying the middle observation number (if the number of observations is odd) or the middle pair of observation numbers (if the number of observations is even) and then determining the value(s) corresponding to this/these middle observation(s). In this approach the adjusted cumulative in column F is still used but rather than explicitly calculating the values in column I and J for every observation it can now be restricted to just the middle observation(s).
I think there is no way around compromises. Either using big amounts of helper cells or having the table sorted by the values.
Helper cells:
Formula in F4:AS6:
=IF(COLUMN()<COLUMN($F$4)+$B4,$C4,"end")
Formula in D2:
=MEDIAN(F4:AS6)
Sorted:
Formula in F4 downwards:
=SUM($B$3:B3)+1
Formula in D2:
=SUM(LOOKUP(INT(SUM(B4:B6)/2+{0.5,1}),F4:F6,C4:C6))/2

Find distinct values based upon multiple columns

I have a spreadsheet of sales with (to keep the example simple) 3 columns
NAME -- STATE -- COUNTRY
It's easy to find how many sales. (sum all the lines)
I can find out how many customers I have but how about finding out how many customers from a particular state (and country)
NAME -- STATE -- COUNTRY
p1----- CA------ USA
p2----- CA------ USA
p1----- CA------ USA
p1----- CA------ USA
p3----- NY------ USA
p3----- NY------ USA
The above example would give 2 unique customers from CA and 1 unique customer from NY and 3 from the USA
EDIT:
The desired result from the above table would be
STATE - UNIQUE CUSTOMERS
CA ---- 2
NY ---- 1
COUNTRY - UNIQUE CUSTOMERS
USA ---- 3
Assuming your data have headers in row 1 of columns A, B, and C, follow these directions.
In cell F1 enter STATE.
In cell G1 enter COUNT.
In cell F2 enter this array-formula (must be confirmed with Ctrl+Shift+Enter↵):
=IFERROR(INDEX(B$2:INDEX(B:B,COUNTA(B:B)),MATCH(0,COUNTIF(F$1:F1,B$2:INDEX(B:B,COUNTA(B:B))),)),"")
In cell G2 enter this regular formula (confirmed with Enter):
=IF(LEN(F2),COUNTIF(B2:B13,F2),"")
Select F2:G2 and copy.
Now select F3:F51 and paste.
UPDATE
The nature of the question changed. The first formula is exactly the same as before. It gets the distinct states in the source data and culls them so they display with no blanks.
The second formula is now different. It needs to count the number of distinct customers in each state, and it is now an array formula confirmed with Ctrl+Shift+Enter↵).
=IF(LEN(F2),SUM(IF(F2=$B$2:$B$50,1/(COUNTIFS($B$2:$B$50,F2,$A$2:$A$50,$A$2:$A$50)),)),"")
This formula (entered as an array formula CTRL-SHIFT-ENTER) will count the number of occurrences of a Name in MyState
=COUNTIFS(Names,Names,States,MyState)
So if MyState="CA" this would return {3;1;3;3;0;0}
To get the number of names in CA you can sum the reciprocals of this array, EXCEPT taking the reciprocal of zero is invalid/infinite. So wrap the formula above in a test for zero: if it's zero, output zero, otherwise take the reciprocal (one of the rare situations where you get to set infinity equal to zero!):
=IF(COUNTIFS(Names,Names,States,MyState)=0,0,1/COUNTIFS(Names,Names,States,MyState))
(Still an array formula.)
For CA this will return {0.333333;1;0.333333;0.333333;0;0}
The final step is to sum with the array formula:
=SUM(IF(COUNTIFS(Names,Names,States,MyState)=0,0,1/COUNTIFS(Names,Names,States,MyState)))
It's possible that this could return say 2.99999... instead of 3 due to rounding errors. If that's a problem you can fix it by wrapping it with the ROUND function or setting the display format zero decimal places.
It should be straightforward to modify this to count by country. Hope that helps.
Since the question also has an 'google-spreadsheets' tag, this would be my suggested formula to use in a google spreadsheet:
For the state counts:
=query(unique(ArrayFormula({A2:A&B2:B, A2:C})), "select Col3, count(Col1) where Col3 <> '' group by Col3 label count(Col1)''",0)
And for the country counts:
=query(unique(ArrayFormula({A2:A&B2:B&C2:C, C2:C})), "select Col2, count(Col1) where Col2 <> '' group by Col2 label count(Col1)''",0)
Also see this example spreadsheet.
Easy with a PivotTable in Excel 2013:
Also easy with Google Spreadsheets:

Consolidate two or more columns of data into one column using formula

I need to collect Sch Code from different columns into one column as shown below.
First priority is by formula or UDF Function if possible.
My Data:
Column A Column B Column C Column D Column E Column F Column G
SCH Code Value SCH Code Value Rating SCH Code Value
C01-3-1 4 C01-4-1 8 300 C02-3-1 8
300 C02-3-5 9
C01-3-2 5 C01-4-2 12 300 C02-3-2 12
C01-3-3 6 C01-4-3 21 300 C02-3-3 21
300 C02-3-6 10
C01-3-4 7 C01-4-4 4 300 C02-3-4 4
Required Result (Only Sch Code required in summary sheet but it is required by formula or VBA UDF function) :
Column A
C01-3-1
C01-3-2
C01-3-3
C01-3-4
C01-4-1
C01-4-2
C01-4-3
C01-4-4
C02-3-1
C02-3-5
C02-3-2
C02-3-3
C02-3-6
C02-3-4
You can collect unique non-blank values from column A with an array formula e.g. =INDEX($A$2:$A$99,MATCH(0, IF(LEN($A$2:$A$99),COUNTIF(I$1:I1,$A$2:$A$99),1),0)). Since this returns #N/A where it has no more values to return from its column, you can pass control over to a similar formula that references another column with IFERROR.
    
To choose from your three columns of SCH Codes, you would need to stack this 3 deep. The formula in I2 is:
=IF(LEN(I1),IFERROR(INDEX($A$2:$A$99,MATCH(0, IF(LEN($A$2:$A$99),COUNTIF(I$1:I1,$A$2:$A$99),1),0)),IFERROR(INDEX($C$2:$C$99,MATCH(0, IF(LEN($C$2:$C$99),COUNTIF(I$1:I1,$C$2:$C$99),1),0)),IFERROR(INDEX($F$2:$F$99,MATCH(0, IF(LEN($F$2:$F$99),COUNTIF(I$1:I1,$F$2:$F$99),1),0)),""))),"")
This array formula requires Ctrl+Shift+Enter, not just Enter. Once entered correctly, it can be filled down to catch all possible values. I would fill down for at least three times as many rows as you have in order that the blanks would have a place if they were filled in at a later date.
In theory, you could stack this much deeper but for practical purposes, I wouldn't go much deeper than this. Array formulas eat up calculation resources at a logarithmic rate so the size of your data is going to be a key factor on whether this is a suitable solution.
One solution:
Copy and paste all values in column C below column A data. Highlight column A and go to Data>Remove duplicates then Data>Sort.

Issues with VLOOKUP in Excel

I have a tax table in one sheet that has a list of tax values. For example:
Sheet1: Tax Tables
A B C
1 Min Max Taxed
-------------------
2 50 100 10
3 100 200 20
4 200 300 30
In another sheet I have a gross income value of say 120 in cell A1. What I want to do is have a vlookup (I'm assuming that's what I should use) that checks cell A1 to see if it's between the Min and Max and then outputs the taxed amount in B1.
Sheet2: Income
A B
1 Gross FedTax
-----------
2 120 Value from Column C goes here
I already have the sheet in Tax Tables set up with named spaces A:C=Min and B:C=Max
I tried doing this:
=AND(VLOOKUP(<A1,Min,3,False),VLOOKUP(>A1,Max,2,FALSE))
But not even close...
I just want to check column A in the first sheet to see if it's less than the the value in the second sheet, and check column B in the first sheet against the value in the second for if its more, then put the value in column C in the first sheet into the cell next to the value in the second sheet.
To use a VLOOKUP, put your maximums and minimums in the same column.
Then use the TRUE argument, which means it looks for the next value that matches. Assuming the value you're looking up in D2, you'd put a formula like this in E2:
=VLOOKUP(D2,$A$2:$B$5,2,TRUE)
First of all it is unclear what you would apply when the amount is exactly 50/100/200/300/... So i decided to include the lower limit in the interval and exclude the upper limit.
For this problem I would use a sumifs like this (you have to decide on which side to put the equal sign:
=SUMIFS(Sheet1!C:C;Sheet1!A:A;"<="&A1;Sheet1!B:B;">"&A1)
This would only take those elements in column C that have a value in column A smaller than or equal to 120 and a value in column B greater than 120

Resources