Formula to SUM last/previous three values - text in two columns and corresponding values also in two columns

smide

Board Regular
Joined
Dec 20, 2015
Messages
162
Office Version
  1. 2016
Platform
  1. Windows
Hello.


In columns A and B (A2:A500 and B2:B500) I have a list of product names (text cells) and in columns C and D their corresponding values (numbers in the range from 0 to 1000).
When product is in column A his value is in column C (in the same row of course) and when the product is in column B his value is in the column D.
I need a formula (if it is possible to create such a formula) to SUM values of last three product's occurences (previous three more precisely) and to place result in the columns E or F in the same row where referent product appears.


example.


Calculation only for Product1 in this example.

Sheet1 (current status)

ABCD
1
2Product1Product326
3Product4Product6914
4Product8Product11245
5Product1Product2711
6Product3Product943
7Product1Product8186
8Product5Product157
9..............
10.............

<tbody>
</tbody>




Sheet1 (after calculation)



ABCDEF
1ResultsResults
2Product1Product326
3Product4Product6914
4Product8Product11245
5Product1Product2711
6Product3Product943
7Product1Product818654
8Product5Product15770
9............
10............

<tbody>
</tbody>

4th Product1 is in row 7, in column A then his result is in column E also in row 7 : 7+45+2 = 54 (sum of previous three values for Product1)
5th Product1 is in row 8, in column B then his result is in column F also in row 8 : 18+7+45 = 70 (sum of previous three values for Product1)
 
Last edited:

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Try this array formula:
Excel 2012
A
B
C
D
E
F
1
Results
Results
2
Product1
Product3
2
6
3
Product4
Product6
9
14
4
Product8
Product1
12
45
5
Product1
Product2
7
11
6
Product3
Product9
4
3
7
Product1
Product8
18
6
54
8
Product5
Product1
5
7
70
9
Product6
Product4
4
9
10
Product1
Product6
11
13
32
11
Product6
Product3
21
18
31

<tbody>
</tbody>
Sheet2


Array Formulas
Cell
Formula
E2
{=IFERROR(INDIRECT(TEXT(LARGE(IF($A$1:$B1=A2,ROW($A$1:$B1)*100+COLUMN($C$1:$D1)),3),"R00C00"),0)+INDIRECT(TEXT(LARGE(IF($A$1:$B1=A2,ROW($A$1:$B1)*100+COLUMN($C$1:$D1)),2),"R00C00"),0)+INDIRECT(TEXT(LARGE(IF($A$1:$B1=A2,ROW($A$1:$B1)*100+COLUMN($C$1:$D1)),1),"R00C00"),0),"")}

<tbody>
</tbody>
Entered with Ctrl+Shift+Enter. If entered correctly, Excel will surround with curly braces {}.
Note: Do not try and enter the {} manually yourself

<tbody>
</tbody>


Put the formula in E2, then copy to F2, and then down the columns.

If you have the same product in both columns on the same row, you will get uncertain results. Let me know if this works for you.


Edit: I didn't think this shorter version would work, but I tried it and it seems to work just the same:

=IFERROR(SUM(INDIRECT(TEXT(LARGE(IF($A$1:$B1=A2,ROW($A$1:$B1)*100+COLUMN($C$1:$D1)),{1,2,3}),"R00C00"),0)),"")
also with Control+Shift+Enter
 
Last edited:
Upvote 0
Both formulas work great! Thank you.;)


Can I just ask you, suppose I need (on some other worksheet with same data as this) SUM of values for product's but this time values from opposite columns like this:

example.


value in cell E7 would be: 11(D5) + 12(C4) + 6(D2) = 29


Value in cell F8: 6(D7) + 11(D5) + 12(C4) = 29 (it turned out by chance the same as E7 here)


Do you have any idea how to solve this?
 
Upvote 0
Sure, in E2 put this variation of the shorter formula:

=IFERROR(SUM(INDIRECT(TEXT(LARGE(IF($A$1:$B1=A2,ROW($A$1:$B1)*100+7-COLUMN($C$1:$D1)),{1,2,3}),"R00C00"),0)),"")

It's a 7 since you're looking at columns 3 and 4, and 7-3=4, and 7-4=3, so it's a way to swap the values.

:)
 
Upvote 0
Yeah, works perfect. What else can I say...
Thank you so much.
 
Upvote 0

Forum statistics

Threads
1,214,957
Messages
6,122,466
Members
449,086
Latest member
kwindels

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top