Skipping blank lines and doing math on subsequent

2019-08-20 08:27发布

So I am interested in using excel to analyze a data set the data is layed out similar to below

Color  | WEIGHT  
------ | ------  
Blue   | 1000  
Blue   | 2000  
Blue   | 3000  
Red    | 4225  
Red    | 5000  
Red    | 5750  
Yellow | 6250  
Red    | 7220
Red    | 9280
Blue   | 9720
Blue   | 11000
Blue   | 12000

I would then like to find the amount of weight gained so for example blue initial value 1000 final value 3000 change of 2000. And For red 5750-3000 = change of 2750 I know how to find when a color change occurs eg. If(A1<>A2,A1,0). doing this gives me an output like

Color  | WEIGHT  
------ | ------  
Blue   | 1000  | 1000
Blue   | 2000  |
Blue   | 4225  | 
Red    | 4225  | 4225
Red    | 5000  | 
Red    | 5750  |
Yellow | 5750  | 5750
Yellow | 7220  |
Red    | 7220  | 7220
Red    | 9280  |
Blue   | 9280  | 9280
Blue   | 11000 |
Blue   | 12000 | 1200

I would like to make a new table that shows the color and the weight change. With outputs similar to the following

I am using the following formula to get the output

=IFERROR(MMULT(AGGREGATE({14,15},6,$B$1:$B$78/($A$1:$A$78=G2),1),{1;-1}),"")

Credit to @XOR LX for the above formula where my color selection is in G2 and my data is in A and B

Using this formula I can get an output like

Color|Change  
Blue  | 11000   
Red   | 5055
Yellow| 970

Instead I would like to get an output like the following.

Color|Change  
Blue  | 3225   eg (4225-1000)
Red   | 1525
Yellow| 1570
Red   | 2060
Blue  | 2720

标签: excel
1条回答
Animai°情兽
2楼-- · 2019-08-20 09:14

Array formula**:

=MMULT(INDEX(B:B,N(IF(1,SMALL(IF(MMULT(COUNTIF(E2,CHOOSE({1,2},A$1:A$14,A$2:A$15)),{1;1})=1,LOOKUP(ROW(A$2:A$15),ROW(A$2:A$14))),2*COUNTIF(E$2:E2,E2)-{1,0})))),{-1;1})

where it is assumed that:

1) You have Excel 2010 or later

2) Your table is in A1:B14 (with headers in row 1)

3) Your colour of choice, e.g. "Blue", is in E2

Copy down to give similar results for colours in E3, E4, etc.

Regards

**Array formulas are not entered in the same way as 'standard' formulas. Instead of pressing just ENTER, you first hold down CTRL and SHIFT, and only then press ENTER. If you've done it correctly, you'll notice Excel puts curly brackets {} around the formula (though do not attempt to manually insert these yourself).

查看更多
登录 后发表回答