# Calculate price difference on product in FIFO order

#### JackDanIce

##### Well-known Member
Hi,

I have a table in date order (oldest to newest, by day, including duplicate dates) with ledger entries for stock purchases and stock sold.

I will never sell more than what is available to sell

 Date Order Product Quantity Unit Price Unit Price Difference (£) Unit Price Difference (%) 01/01/2019 Buy A 50 £2 02/02/2019 Buy A 100 £1 03/03/2019 Sell A -50 £10 ? ?

If I need to calculate difference in price for A as £8 (£10 - £2) for date 03/01/2019, what formula would I need, based on first Buy price for Product A vs first Sell price for Product A
Related, if for 03/01/2019 -100 were sold, how would I calculate price difference as: £8.17 for Product A? £10 - (50*£2 + 10*£1)/60
And formula for % difference for same!

TIA,
Jack

### Excel Facts

Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

#### KOKOSEK

##### Active Member
 Date Order Product Quantity Unit Price Unit Price Difference (£) Unit Price Difference (%) 01/01/2019 Buy A 50 £2 02/02/2019 Buy A 100 £1 03/03/2019 Sell A -50 £10 ?
If I need to calculate difference in price for A as £8 (£10 - £2) for date 03/01/2019, what formula would I need, based on first Buy price for Product A vs first Sell price for Product A

Code:
``=E4-SUMIFS(E:E,A:A,MINIFS(A:A,C:C,C4))``

Related, if for 03/01/2019 -100 were sold, how would I calculate price difference as: £8.17 for Product A? £10 - (50*£2 + 10*£1)/60

I think, if sold 100 --> £8.5 =10-((50*2+50*1)/100)

#### JackDanIce

##### Well-known Member
Thank you Kokosek,
Code:
``=E4-SUMIFS(E:E,A:A,MINIFS(A:A,C:C,C4))``
If more dates are added with similar data, will this formula only pull for the earliest MINIF date? I need it to work on a FIFO basis (first in, first out)

My error, meant if 60 were sold

#### KOKOSEK

##### Active Member
How do you want to solve this manually it the case if there are few deliveries on the same date (MIN date) with different price?
What your 'First Buy price' will be? Maybe it is worth to create additional column with weighted average price for each delivery date?

#### JackDanIce

##### Well-known Member
Happy to use a weighted average or other helper column, may need similar for quantity i.e. running total available in stock

Maximum 1 delivery per product per date. Different products can exist on matching dates.

'First Buy price' is first price for a product after it's quantity increases from 0 (assume all products quantities start at 0)

i.e.

 Date Order Quantity Price 01/01/2019 Buy 100 £1 02/02/2019 Sell -100 £10 03/03/2019 Buy 100 £2
So in data above, 'first buy price' from 01/01/2019 - 02/02/2019 is £1

After 02/02/2019 next 'first buy price' is £2 until quantity falls back to 0

At the moment there is just a single ledger table that is meant to show change in cash and change in quantity and then margin calculations are needed from price changes over time (like table in #1)

#### KOKOSEK

##### Active Member
Thank you Kokosek,
Code:
``=E4-SUMIFS(E:E,A:A,MINIFS(A:A,C:C,C4))``
If more dates are added with similar data, will this formula only pull for the earliest MINIF date? I need it to work on a FIFO basis (first in, first out)
If it is only 1 delivery per day First Buy date is MIN date.

Replies
0
Views
338
Replies
2
Views
90
Replies
3
Views
71
Replies
4
Views
137
L
Replies
1
Views
316

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

1,163,492
Messages
5,832,008
Members
430,103
Latest member
BIGGAZ

### 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.

### Which adblocker are you using?

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

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