Average if cell and and adjacent cell are not NA (Excel) - excel

I am trying to calculate an average value if the rows in two columns are not NA. This is to aid me in calculating an average weight of fish. Here are some example data:
Country,fish weight,fish number
Belgium,264.5,NA
Channel Islands,NA,NA
England,625,281000
Netherlands,737,690000
France,189.5,NA
In this example, the average function would create an average of England and Netherlands weights but not include Belgium and France's weights as the number of fish is not known.
I tried doing this 'averageifs' statement but I can only get it to includes all data.
=AVERAGEIFS(weights,weights,"<>0", numbers,"<>0")

NA is not the same as 0, therefore your formula will see those cells as different from 0 and take them into account. Change your criteria into "<>NA"

Related

I'm trying to reduce ten columns of Excel formulas into one cell with one large, nested command if possible

Weights
I have built a tab in an Excel spreadsheet that uses base data from 6 columns and then formulas in 9 additional columns that determine the final result in another column, which then gets copied into another cell on another tab (see 'weights' picture at top of this post):
The base data is in 6 columns A-F and has 729 rows - each column has a certain weight (30%, 20%, 20%, 10%, 15%, 5%). Each cell is either 1, 2, 3, 4, 5, N/A, or blank. The goal is to look for N/A or blanks and then redistribute the weight for those categories to the remaining non-N/A and non-blank categories, so if the 20% column was N/A and the other 5 columns were 1, 2, 3, 4, or 5, I would add 4% to each of the 5 scored categories to calculate the final score. Everything works great but I am interested in doing everything in one column after the 6 base columns. I start by converting N/A to 0 and then handle things in multiple columns by doing this:
In Column G, I get the total weight of the columns with N/A or 0:
=SUMIF(A2:F2,0,$A$729:$F$729)
Then in Column H, I get the number of columns that the weight percentage in Column G will get distributed to:
=COUNTIF(A2:F2,">0")
Then in Column I, I calculate the percentage to be added to the remaining columns:
=IF(H2=0,0,SUM(G2/H2))
Then in Columns J-O, I calculate the new weight percentages for each weight (the original weight percentages are in cells H2-H7 on a different tab called 'Controls':
=SUM(Controls!$H$2+$I2) is for Column J, =SUM(Controls!$H$3+$I2) is for Column K, etc.
Then finally I calculate the 'new' score with the 'new' weights. If there were no N/A or 0's, it keeps the original overall score and if not it multiples the score in a cell (1,2,3,4,5) by the new weight and adds the six totals together:
=IF(H2=6,Q2,SUM(A2J2)+(B2K2)+(C2L2)+(D2M2)+(E2N2)+(F2O2))
It all works great, but how can do what I'm doing in 10 columns with just one statement in one cell? I'm assuming there are lots of elegant references and nesting required but I'm not sure how to do it.
Thanks in advance!
You can do this step by step:
e.g.
cell (G2) =SUMIF(A2:F2,0,$A$729:$F$729)
cell (H2) = COUNTIF(A2:F2,">0")
cell (I2) =IF(H2=0,0,SUM(G2/H2))
In cell I2 replace the cell references of G2 and H2 with the formula expression of the respective cells.
More elegant with LET:
https://support.microsoft.com/en-us/office/let-function-34842dd8-b92b-4d3f-b325-b8b8f9908999

How to calculate average rating in excel with excluding blank cells?

How to calculate average rating in excel with excluding blank cells?
In other way
if cells is empty --> not calculate rating
if 0 or greater than 0 to 5 --> calculate rating
then calculate average ratings.
Do you mean that your range includes values of less than 0 and/or larger than 5 and empty, but you only want to calculate value from 0 to 5?
I think you'd better use pair of SUMIFS and COUNTIFS, with set of your conditions (>=0, <=5, not blank...).
For example, assumed your value range B1:B6:
=SUMIFS(B1:B6,B1:B6,">=0",B1:B6,"<=5")/COUNTIFS(B1:B6,">=0",B1:B6,"<=5")
This give out result of average of 2.5
And please note that COUNTIFS or AVERAGE... they do not count empty cells by default.

How to create a dynamic formula to find the average of a set of values for a given vector

I am trying to create a formula that gives me the average of the last 12 entries in a given dataset depending on the associated vector.
Let's make an example:
I have in column F2,G2,H2 and I2 dates, Company1, Company2 and Company3 respectively. Then from row3 to row 33 I have months dates starting from May 2016.
Date Company1 Company2 Company3
May-16 2,453,845
Jun-16 13,099,823
Jul-16 14,159,037
Aug-16 38,589,050 8,866,101
Sep-16 63,290,285 13,242,522
Oct-16 94,005,364 14,841,793
Nov-16 123,774,792 7,903,600 41,489,883
Dec-16 93,355,037 12,449,604 69,117,105
Jan-17 47,869,982 13,830,712 83,913,764
Feb-17 77,109,905 10,361,555 68,176,643
The goal is to create a formula that, when I drag it down, correctly calculates the average of the last 12 values for a given company.
So for example i would have, say in table "B2:C5":
Company1 76,856,345
Company2 11,120,859
Company3 65,674,349
And, if a new Company4 is added to the list, then I just have to drag it down the formula, to calculate the average of the last 12 months for Company4.
Until now, I have came up with this formula:
=AVERAGE(LOOKUP(LARGE(IF(ISNUMBER(G:G),ROW(G:G)),ROW(INDIRECT("1:"&MIN(12,COUNT(G:G))))),ROW(G:G),G:G ))
This formula correctly calculates the average of a given column, considering only the last 12 values. The last step would be to come up with a formula that includes all the columns and then calculates the average for the given company.
Thanks!
I recommend that you use a named range to define your data in columns G:I. When a company is added, just modify the named range's specs. I used the name Target. Of course, you can replace it with $G:$I if you feel so inclined but I would rather recommend reducing the number of rows in the range, which is easier to manage when it is named.
Use the formula below to extract the company names from the first row of Target into the first column of your averages table. This is to ensure that the names are spelled identically in both locations.
=INDEX(Target,1,ROW()-2)
The number 2 indicates the number of rows above the row containing the formula. it is copied here from cell M3. There, ROW()-2 creates the number 1, counting sequentially as the formula is copied down.
Now I have the formula below in my cell N3 and copied down.
=SUM(INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0)))
The formula simply sums up the columns G, H, and I in 3 consecutive rows.
In the final step I inserted the range definition established above, meaning excluding the SUM() function, into your existing formula.
=AVERAGE(LOOKUP(LARGE(IF(ISNUMBER(INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0))),ROW(INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0)))),ROW(INDIRECT("1:"&MIN(12,COUNT(INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0))))))),ROW(INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0))),INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0))))

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

Excel conditional SUMPRODUCT / SUMIFS / Array Formula for optional dimension

I have a sheet of data with multiple dimensions like this:
A B C D E
1 COUNTRY FLAVOUR SIZE DATE SALES ($)
2 Japan Strawberry 100ml 10/12/14 100
3 Japan Banana 100ml 10/03/15 100
4 China Orange 200ml 14/04/15 30
5 France Strawberry 200ml 11/04/15 400
6 UK 200ml 23/04/15 250
7 ....
I want to aggregate this data over a date range, where the summary sheet has each dimension (country & flavour), and if I do not specify a dimension it sums all rows for that dimension.
A B C
1 COUNTRY FLAVOUR SALES TOTAL
2 Japan Strawberry 100
3 Japan 200
4 Strawberry 500
I can do this if all the dimensions are present (i.e. row 2 above) using a SUMPRODUCT or SUMIFS:
=SUMPRODUCT((data!A$2:A$100=A1)*(data!B$2:B$100=B1)*(data!D$2:D$100>[start_date]*(data!D$2:D$100<[end_date])*(data!E$2:E$100))
However I have not been able to figure out how to include all rows for a dimension if that input cell is empty (e.g. row 3 above). I tried:
Adding an IF statement or OR statement within the criteria (e.g. OR(data!A$2:A$100=A1,isblank(A1))).
Using a + in a SUMPRODUCT as an OR statement, (per this answer https://stackoverflow.com/a/27536131/1450420)
One solution is to have different branches of the formula depending on which summary dimensions are present, but that would quickly get out of control if I extend this same behaviour to further dimensions like Size.
Any help appreciated!
(I'm running Excel Mac 2011).
EDIT
Per #BrakNicku's comment one of the formulas I tried was =SUMPRODUCT(((data!A$2:A$100=A2)+ISBLANK(A2))*((data!B$2:B$100=B2)+ISBLANK(B2))*(data!E$2:E$100))
The reason this doesn't work is that sometimes my data has blank attributes (edited above). For some reason this formula double-counts rows where the attribute present matches (e.g. data!A6) but the other attribute is missing (e.g. data!B6).
EDIT 2
I can see why this double-counting is happening, because the + is summing the match because data!A$2:A$100=A2 (they match because they are both blank) and the match because ISBLANK(A2) (it is indeed blank). The question would remain how to achieve this without double counting. If needed a workaround could be to fill all blank cells on my data with some placeholder value.
The reason for double-counting values is here:
((data!A$2:A$100=A2)+ISBLANK(A2))
If a cell in A column is blank, both parts of this sum are equal 1. To get rid of this problem you can change it to:
(((data!A$2:A$100=A2)+ISBLANK(A2))>0)
Try this (I only included the first two, I left the dates out):
=SUMPRODUCT((((Data!$A$2:$A$5=A2)+(A2=""))>0)*(((Data!$B$2:$B$5=B2)+(B2=""))>0)*(Data!$E$2:$E$5))

Resources