Excel: How to get average of the between nonzero values? - excel

I would need to get the average of the 2nd to the 8th nonzero value per row. Meaning, it would always move depending where the nonzero begins at.
I think what I would need is to determine the following:
Location of the 2nd nonzero value
Location of the 8th nonzero value
Average of numbers between 2nd and 8th nonzero values
Is that possible?
For example
0 | 6 | 10 |5| 9 | 0 | 6 | 0 |3 | 10| 1|9|
Those in bold have to be averaged. The zeroes in between have to be ignored

If you are using Windows Excel 2019, then:
=AVERAGE(FILTERXML("<t><s>" &TEXTJOIN("</s><s>",TRUE,IF(row_ref<>0,row_ref,""))&"</s></t>","//s[position()>1 and position()<10]"))
TEXTJOIN extracts only those values which are non-zero (and non-blank)
By using the appropriate delimiters, we create an XML
The xPath with the position function then extracts the 2nd to 8th values (positions 2 through 9)
AVERAGE

Assuming your data is laid out as per the example below, place the following formula in column T and copy down as required.
=IFERROR(AVERAGE(INDEX(22:22,AGGREGATE(15,6,COLUMN(B22:S22)/(B22:S22<>0),ROW($2:$9)))),"Less than " & rows($2:$9) & " non zero numerical entries")
B22:S22 - represents the row of data you are looking at. Feel free to change the column reference letters to suit your needs. Just ensure all the references with in the formula match.
$2:$9 - Represents the number of entries you want to use as part of the average. 2 is the starting number that you want to use and corresponds to 2nd non zero. 9 is the last number you want to include and makes a total of 8 numbers. Adjust these number to change the data range you want to include.
Ensure you keep the $ to prevent the row number from changing as the formula is copied.
Aggregate performs array like operations. As a result it may cause your system to bog down or crash if there is an excessive number of rows you are looking at. Also, full column references should generally be avoided within the aggregate function to avoid excess calculation.

I placed my answer not in A1 to make sure it will work anywhere on your sheet.
Assuming a layout like this (your first row of numbers from D2 to T2):
Paste this to Y2 (in the combined) column:
=AVERAGEIF(OFFSET(C2,0,AGGREGATE(15,3,((D2:T2)>0)/((D2:T2)>0)*COLUMN(D2:T2)-COLUMN(C2),2)):OFFSET(C2,0,AGGREGATE(15,3,((D2:T2)>0)/((D2:T2)>0)*COLUMN(D2:T2)-COLUMN(C2),8)),">0")
Then copy down to the other rows.
Data I used:
1 2 0 6 4 9 0 7 3 7 1 1 1 2 6 9 7
0 0 7 0 1 1 4 4 1 5 8 3 5 6 4 6 4
1 7 2 8 0 6 4 9 9 7 8 4 6 9 4 2 9
0 1 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0
8 7 8 4 10 0 2 2 10 4 4 8 3 3 0 4 10
In this formula, C2 is the cell (empty or not) before the beginning of the row of numbers and not included in the numbers to be counted.

Assuming you have excel 365, this can be easily done using simply FILTER, INDEX and AVERAGE function
=AVERAGE(INDEX(FILTER(row_ref,row_ref>0),{2,3,4,5,6,7,8,9}))
Example
=AVERAGE(INDEX(FILTER(B5:N5,B5:N5>0),{2,3,4,5,6,7,8,9}))

Related

Finding maximum value in group

I want to create a column of data that finds the largest value in column BD, based on individual values in Column B. I would have thought this equation would work! Anyone have any recommendations??
(first attempt)
=IF(BD3=0,0,SUMIFS($BD$3:$BD3,$B$3:$B3,B3,$BD$3:$BD3,MAX($BD$3:$BD3)))
(Second attempt)
=IF(BD3=MAXIFS($BD$3:$BD3,$B$3:$B3,B3),MAXIFS($BD$3:$BD3,$B$3:$B3,B3),0)
Projectid(B)
cumulative production(BD)
result I want()
1
20
0
1
60
0
1
70
70
2
0
0
2
0
0
3
20
20
4
0
0
5
0
0
6
0
0
7
10
0
7
40
0
7
60
60
this code should work:
=IF(MAXIFS($BD$1:$BD$12,$B$1:$B$12,A1)=BD1,MAXIFS($BD$1:$BD$12,$B$1:$B$12,B1),0)
If you have access to LAMBDA() function then can use below formula at one go.
=LET(a,BYROW(A2:A13,LAMBDA(x,MAXIFS(B2:B13,A2:A13,x))),IF(B2:B13=a,a,0))
Similar to SUMIFS you find MAXIFS
https://support.microsoft.com/en-us/office/maxifs-function-dfd611e6-da2c-488a-919b-9b6376b28883
Then just compare the MAX with the actual row value.
Edit based on your reply:
One way is to start with one operation in the cell first then it is easier to follow.
Your MAXIF should be like this (looking for the max in column B of those rows having the same value in column A as the value of current column A)
=MAXIFS(B$2:B$13;A$2:A$13;A2)
Then compare to B2
So, basically you got it, need just to adjust your second part of the MAXIFS :-)

Excel using SUMIF to calculate totals of multiple columns

I'm trying to use Excle's SUMIF to calculate totals of Col1 to Col5 for dates that are similar.
My formula is as follows =SUMIF($A2:$A7,A10,$B2:$F7), but this only gives me the total of a single column.
How can I get the Totals of all the columns based on the date like I've shown in my results.
Date Col 1 Col 2 Col 3 Col 4 Col 5
1/5/2017 1 2 2
1/5/2017 5 3 1
1/5/2017 9 5 5
2/5/2017 10 5 3
2/5/2017 20 10 3
2/5/2017 6 8 1 5
Desired Results
1/5/2017 15 7 7 3 1
2/5/2017 30 11 11 11 8
use below formula in cell B11
=SUMIF($A$2:$A$7,$A11,B$2:B$7)
Per the example you provided, One solution is to use SUMPRODUCT
Multiplies corresponding components in the given arrays, and returns the sum of those products
Microsoft Docs give a thorough example, but per SO etiquette, here is an example in case of link-rot: [FYI, I used absolute reference for easier filling across, arbitrary how you get it done though]
Forumlas shown:
Formula is kind of hard to see without clicking on image:
=SUMPRODUCT(($B$3:$B$8=$B$11)*C3:C8)
This basically breaks down like this, it searches the B:B column for a match, and it will naturally return a true or false for the match, or 0/1 counterparts, and multiplys that by the number found in the column to the right (C3:C8), so it will either be 1 * # = # or 0 * # = 0

vlookup with multiple resulting values

I have a table array that looks like this:
A B
1 2
1 3
1 9
2 3
2 4
2 11
2 23
2 56
3 7
4 13
My VLOOKUP formula is to check for 1 in column A and then return the corresponding B value. Is there anyway I can get all the values for 1? Currently it just returns back the last corresponding number for 1 i.e. 9 in column B.
You can Pivot that data, or you can try INDEX and MATCH together, perhaps even an IF command with it.

How to combine special rows of two worksheets in one worsheet?

In excel I have two worksheets. The first worksheet is like follows:
1 1
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
10 1
and the second worksheet is like follows:
1 1
2 0
3 0
4 0
9 1
10 1
11 0
12 0
13 1
14 0
(they do not necessarily contain the same number of elements). The first column is some ID (which might differ in the worksheets) and the second column is a flag whether to take this element in the combined worksheet. The combined worksheet should look like:
1
9
10
13
i.e, this worksheet contains only those elements which have a '1' in any of the other two worksheets (second column).
How can I achieve this WITHOUT MACRO? Is there a solution?
Append one to the other in a new sheet, sort on the first column, Remove Duplicates (check both columns), Filter to select 0 in the second column and delete those rows and that column.
An alternative for updating with just a single click is a PivotTable constructed from multiple consolidation ranges with Sum of Value for VALUES and Value filtered to select 1 (and possiby hide all but the first column).

How to compute the maximum series of a specific condition returning true

i have a slight issue to count the MAX frequency of where the third colmn is bigger than the second. This is just a statistic with scores.
The issue is that i want to have it in one single formula without a macro.
B C
------
2 0
1 2
2 1
2 3
0 1
1 2
0 1
3 3
0 2
0 2
i have tried it with:
{=MAX(FREQUENCY(B3:B100;B3:B100>=C3:C100))} to get 1 for B
{=MAX(FREQUENCY(C3:C100;C3:C100>=B3:B100))} to get 7 for C
I excpected it to deliver me the longest series where the value in the one column was bigger than in the other one, but i failed hard...
Try this version to get 7
=MAX(FREQUENCY(IF(C3:C100>=B3:B100,IF(B3:B100<>"",ROW(B3:B100))),IF(C3:C100<B3:B100,ROW(B3:B100))))
confirmed with CTRL+SHIFT+ENTER
obviously reverse the ranges to get your other result
See example here

Resources