Excel: Average Difference in a single formula - excel

I have a dataset like this:
10, 23, 43, 45, 56;
12, 25, 21, 23, 40;
I want to know the average of the difference between the two rows like
mean (10 - 12, 23 - 25, 43 -21 ...)
Of course, this is only an example and the actual rows are hundreds of element long. I would like to compute the average of the difference without having to compute somewhere the difference and then having the average. (The sheet is already pretty big)
Thanks a lot

Mathematically, what you are asking for is identical to:
=AVERAGE(A1:E1)-AVERAGE(A2:E2)
Regards

Try,
=AVERAGE(INDEX((A1:E1)-(A2:E2), , ))

If there were missing values in one range or the other, you would need something like
=AVERAGEIFS(A1:G1,A1:G1,"<>",A2:G2,"<>")-AVERAGEIFS(A2:G2,A1:G1,"<>",A2:G2,"<>")
(I have tested it with blanks in G1 and F2)

Related

random numbers from geometric distribution such that their sum equals SUM

I want to draw k random numbers i_1,...,i_k with min <= i <= max from an exponentially shaped distribution of values with m,std being median and standard of the population's values. The sum(i1,..,ik) should equal a given parameter SUM.
Example:
Given:
k = 9 SUM = 175 min = 8 max = 40 m = 14
Desired:
[9, 10, 11, 12, 14, 17, 23, 30, 39]
I don't know if this is actually possible without depending on luck to draw a combination satisfying the SUM rule. I'd appreciate any kind of help or comment. Thank you.
EDIT: In a former version I wrote about exponentional distributions where an exact solution is impossible, rather I meant an exponentially shaped distribution with discrete values like a geometric distribution for instance.
EDIT2: Corrected the number k in the example.

Sum minimum of corresponding column values - limited to date range

I have a data set with four columns: Start Date, End Date, Scheduled Qty, and Actual Quantity:
Start Date End Date Scheduled Qty Actual Qty
04/13/15 04/17/15 35 19
04/20/15 04/24/15 35 42
04/27/15 05/01/15 35 41
05/04/15 05/08/15 35 41
I want to find the total actual, except when the actual exceeds the scheduled I want to used the scheduled number.
In an already answered question (Sum minimum of corresponding column values) I found an array formula that works to total the lesser values of each row for the Qty columns (quotes used to display the less than symbol):
=SUM(IF(C1:C4"<"D1:D4, C1:C4, D1:D4))
This gives me a total for my whole range, but now I'd like to limit it to a date range such as end dates within a given month. I've used SUMIFS in other situations to look at my end dates and only sum data that falls within a given month, but I'm not figuring out how to combine that idea with the one from the array formula.
Any ideas how to make this happen? I'm working in Excel 2013.
Here's an extension of chancea's approach:
Excel's SUM function (and AVERAGE, STDEV, etc.) have the useful behavior of "skipping" over text values. For example AVERAGE(3, 4, "dog", 5) returns 4. You can leverage this behavior nested IF's inside a sum. For instance,
=SUM(IF(MONTH(B1:B4)=4,IF(C1:C4<D1:D4,C1:C4,D1:D4),"NO"))
will sum
(a) the lesser of scheduled and actual
(b) when the month is 4
This is accomplished by nested IF's. The outer IF is
IF(MONTH(B1:B4)=4,...,"NO") [if month <> 4, IF returns text ("NO"), which SUM skips]
The inner IF is the same one that chancea showed.
You can nest as many tests/filters for your data as you need
To add criteria onto an array function most of the time you are just going to be multiplying the extra condition onto whatever set of conditions you already have.
The reason why this works is simply because we start with our list of numbers we want to sum:
IF(C1:C4<D1:D4, C1:C4, D1:D4) => { 19, 35, 35, 35 }
Then we multiply 1's or 0's to each of the values that meet the extra criteria.
So for an example lets say that we only want to check the quantity of values that have an end date within the month of 4. We can do that with:
MONTH(B1:B4)=4
Just multiply that criteria in the SUM function to basically create a boolean and condition for that criteria:
=SUM(IF(C1:C4<D1:D4, C1:C4, D1:D4)*(MONTH(B1:B4)=4))
= SUM({ 19, 35, 35, 35 } * { 1, 1, 0, 0}) => SUM( {19, 35, 0, 0} ) = 54
This is the same if we want to add n condition's:
=SUM(IF(C1:C4<D1:D4, C1:C4, D1:D4)*(MONTH(B1:B4)=4)*(`Condition2`)*(`Condition3`)...)
You can use any formula or operator that returns a true/false value within your conditions.
Such as: = > < >= <= <> IF(...,TRUE)
To add OR logic as a criteria you need to use addition instead of multiplication and then group them inside a (..)>0 like this:
(((Or_Condition1)+(Or_condition2)+...+(Or_conditionN))>0)
So if we wanted to sum months 4 OR 5 we can write:
=SUM(IF(C1:C4<D1:D4, C1:C4, D1:D4)*(((MONTH(B1:B4)=4)+(MONTH(B1:B4)=5))>0))

Excel formula for working with multiples (factors)

I need a formula on Excel which would deduct the multiple of 20 from a given number. For example:
2 would give you 18,
10 would give 10,
23 would give 17,
118 would give 2,
321 would give 19.
It's worth noting that Excel has built-in functions for working with multiples: CEILING and FLOOR (in newer versions you have CEILING.MATH and FLOOR.MATH).
In your case, this should work:
=CEILING(A1,20)-A1
You want to divide it by 20, round it up and multiply it by 20, the rest is trivial.
=-(A1-ROUNDUP(A1/20,0)*20)

How to count the number of values that are between intervals in excel

Problem
Let v = {v1, v2, v3,.., vn} be a set of unsorted real numbers.
I want to count to number of values vi that fall in the intervals [0,10], [20,28] etc.
Example
Let v = {2.6, 3.1, 7, 10, 22, 21, 27}
The number of values that fall in the interval [0,10] are: 2.6,3.1,7,10
The number of values that fall in the interval [20, 28] are : 22, 21 ,27
I used the following formula: =SUMPRODUCT((range>0)*(range<10))
Change > and < to >= and <= in your SUMPRODUCT. You can also try COUNTIFS formula - should work faster. With the following sample layout:
Enter in C3 and drag it down:
=COUNTIFS($A$1:$G$1,">=" &A3,$A$1:$G$1,"<="&B3)

How to distribute data into matrix format in VBA excel

Because of the structual nature of dataset, I plan to use VBA in Excel 2010 to process the dataset. The main characteristics of the dataset include three main columns as illustrated:
FromID, ToID, Amount
10, 10, 50
10, 11, 67
10, 12, 56
11, 10, 60
11, 11, 80
12, 10, 17
12, 11, 57
Of course this is the simplified version of the data and the origional data is much complicated than this. The FromID include the point who sends the data and ToID is the poin who receives the data. The amount indicate the size of the data. What I want is, based on the FromID and ToID, to generate a n*n matrix to store the dateset in matrix format in excel,
What i want, the matrix should be as follows:
10 11 12
10 --- 50 67 56
11 --- 60 80 17
12 --- ...
I now have such type of data in columns but I am a noob in VBA and i dont have too much experience. I am wondering is that possible to give me some suggestions about the logics (detailed?) and if possible, could you provide some code snippets with some explanations about how to do this?
Many thanks!
You may not need any VBA coding. Excel has a facility called a Pivot Table to create two dimensional tables of this type. See:
Introduction

Resources