Finding past occurrence - excel

I have a set of stock units transacted (ordered by date from old to new).
I want to find the Previous Quantity that was transacted.
Here is a sample dataset:
Date(A) Type(B) Stock(C) Qty(D) PrevQty(E) CumulativeQty(F)
2016-01-03 Buy MSFT 100 0 100
2016-01-04 Buy GOOG 500 0 500
2016-01-05 Buy MSFT 100 100 200
2016-01-06 Sell MSFT 100 100 100
I can compute CumulativeQty via:
=SUMIFS(D:D,C:C, C4,A:A, "<=" &A4)
Question: How do I figure out PrevQty?

This formula is designed for your cell E3.
=IFERROR(LOOKUP(2,1/($C$2:$C2=$C3),$D$2:$D2),0)
Basically, it looks for the last occurrence of the 'Stock' in C3. However, that last occurrence would naturally by in C3 itself. Therefore the row in which the formula resides is excluded from the range in which the last occurrence is sought.
The presumption is that your first data are in row 2 in your worksheet.

PrevQty
{=index(D:D,match((A4-1)&C4,A:A&C:C,0))}
Update:
For non-sequential dates, to get the value from the same Stock Name, you'll need to do 2 sorting - 1 date from old to new, 2 stock name from A-Z or Z-A
Leave the first (oldest stock) row empty and start the formula from the second oldest row of that stock name. Plus N/A handling.
{=IFERROR(INDEX(D:D,MATCH(MAX(A$3:A3)&C4,A:A&C:C,0)),0)}
Example below: (above formula in cell E4)

Related

Find row when Cumulative sum reaches certain value with condition

I have a table with 3 columns Date, Item and Number. Each row indicates how many items of each Item was received on that date. I am trying to find the date on which cumulative sum reaches 100 or more in that month. Each month will have a target for each item which will be saved in another sheet but for simplicity we can assume that its a fixed number of 100.
Example Data:
Date Item Number
1/2/2018 A 10
2/2/2018 B 10
2/2/2018 A 15
5/2/2018 C 25
6/2/2018 A 50
7/2/2018 B 10
7/2/2018 C 10
8/2/2018 A 25
9/2/2018 A 20
I am looking for the formula which should act on the data similar to above and give the result as 8/2/2018 which is the date on which the cumulative sum for Item A reached 100.
Each month will have different target number, and will have different number of entries.
I have tried using SUMIF and adding a additional column etc but this data is just part of a big data and each item limit is saved in a different sheet etc which is not easy to merge. Thanks in advance for help.
In Excel only, you can use Offset to develop an array of items and numbers containing 1,2,3...9 rows and then SUMIF to add each of them up. Then use Match to find the first one =100 and Index to find the matching date (in F2):
=INDEX(A2:A10,MATCH(100,SUMIF(OFFSET(B2,0,0,TRANSPOSE(ROW(A2:A10))-ROW(A1)),"A",OFFSET(C2,0,0,TRANSPOSE(ROW(A2:A10))-ROW(A1))),0))
Must be entered as an array formula using CtrlShiftEnter.
EDIT
To find the first sum which is >=100 (in G2):
=INDEX(A2:A10,MATCH(TRUE,SUMIF(OFFSET(B2,0,0,TRANSPOSE(ROW(A2:A10))-ROW(A1)),"A",OFFSET(C2,0,0,TRANSPOSE(ROW(A2:A10))-ROW(A1)))>=100,0))
Sum which reaches exactly 100
Sum which does not reach exactly 100 (the number in row 9 has been changed to 24):

Populating Dates and Numeric Values from one table into another table

Below is a simple list from a much longer list of what I'm trying to accomplish.
Suppose I have a supply forecast with 10 dates and Quantities from And I have a list of projects in column A1 to C10
Date . Qty
5/7/2018 80
6/3/2018 100
7/9/2018 100
.....
Project . Qty Required . Qty Left
1 10 50
2 20 30
3 50 -20
4 20 -40
5 . 50 -90
....
I would like to populate two extra columns : Supply Date, Supply Qty Added. So my table would ideally look like this.
Project . Qty Required . Qty Left Supply Date . Supply Qty Added
1 10 50
2 20 30
3 50 60 5/7/2018 80
4 20 40
5 50 90 6/3/2018 100
If there were, lets say, more than 100 projects and a lot more supply dates, doing this exercise would be a little bit too manual.
I have formula for the first date and Qty(If Qty Required for Project row x is < Qty Left in Project row x-1), then bring over the Supply Date and Supply Qty Added.
but my trouble is adding the extra formula where if If Qty Required for Project row x is > Qty Left in Project row x-1, then don't do anything (or put in ''). BUT if the formula (If Qty Required for Project row x is < Qty Left in Project row x-1) is true, then use the 2nd supply date 6/3/2018.
I want my logic similar to Python where you initiate a variable (e.g. row = 0) , and then +1 once the first occurrence occurs so that it knows the grab the next row of values.
Is there a way to perform this in googlesheets, or do I have to leverage Appscripts for this type of exercise?
This can be done without a script.
One way to do this is to count the current number of non blank cells above the current row to decide which date and Qty to choose. The full formula looks something like this:
=IF($B2>$C1,INDIRECT("Sheet1!A"&COUNTA(INDIRECT("D2:D"&MAX(2,ROW()-1)))-COUNTBLANK(INDIRECT("D2:D"&MAX(2,ROW()-1)))+1),"")
Broken down into steps:
If($B2>$C1 checks if Qty required for project row x is > Qty left in project row x-1.
INDIRECT("Sheet1!A"... this is indirectly referencing the sheet that contains the dates and quantities that we want to pull. We know it comes from column A, we just have to decide which row is the "current" row to pull from
COUNTA(INDIRECT("D2:D"&MAX(2,ROW()-1)))-COUNTBLANK(INDIRECT("D2:D"&MAX(2,ROW()-1)))+1) Count the number of blank cells above the current row. Using this method to account for cells with formulas.
MAX(2,ROW()-1) since we start the formula on row 2, we only want to check back for blank cells as far as row 2.
Check out this test sheet.

Excel conditional SUMPRODUCT / SUMIFS / Array Formula for optional dimension

I have a sheet of data with multiple dimensions like this:
A B C D E
1 COUNTRY FLAVOUR SIZE DATE SALES ($)
2 Japan Strawberry 100ml 10/12/14 100
3 Japan Banana 100ml 10/03/15 100
4 China Orange 200ml 14/04/15 30
5 France Strawberry 200ml 11/04/15 400
6 UK 200ml 23/04/15 250
7 ....
I want to aggregate this data over a date range, where the summary sheet has each dimension (country & flavour), and if I do not specify a dimension it sums all rows for that dimension.
A B C
1 COUNTRY FLAVOUR SALES TOTAL
2 Japan Strawberry 100
3 Japan 200
4 Strawberry 500
I can do this if all the dimensions are present (i.e. row 2 above) using a SUMPRODUCT or SUMIFS:
=SUMPRODUCT((data!A$2:A$100=A1)*(data!B$2:B$100=B1)*(data!D$2:D$100>[start_date]*(data!D$2:D$100<[end_date])*(data!E$2:E$100))
However I have not been able to figure out how to include all rows for a dimension if that input cell is empty (e.g. row 3 above). I tried:
Adding an IF statement or OR statement within the criteria (e.g. OR(data!A$2:A$100=A1,isblank(A1))).
Using a + in a SUMPRODUCT as an OR statement, (per this answer https://stackoverflow.com/a/27536131/1450420)
One solution is to have different branches of the formula depending on which summary dimensions are present, but that would quickly get out of control if I extend this same behaviour to further dimensions like Size.
Any help appreciated!
(I'm running Excel Mac 2011).
EDIT
Per #BrakNicku's comment one of the formulas I tried was =SUMPRODUCT(((data!A$2:A$100=A2)+ISBLANK(A2))*((data!B$2:B$100=B2)+ISBLANK(B2))*(data!E$2:E$100))
The reason this doesn't work is that sometimes my data has blank attributes (edited above). For some reason this formula double-counts rows where the attribute present matches (e.g. data!A6) but the other attribute is missing (e.g. data!B6).
EDIT 2
I can see why this double-counting is happening, because the + is summing the match because data!A$2:A$100=A2 (they match because they are both blank) and the match because ISBLANK(A2) (it is indeed blank). The question would remain how to achieve this without double counting. If needed a workaround could be to fill all blank cells on my data with some placeholder value.
The reason for double-counting values is here:
((data!A$2:A$100=A2)+ISBLANK(A2))
If a cell in A column is blank, both parts of this sum are equal 1. To get rid of this problem you can change it to:
(((data!A$2:A$100=A2)+ISBLANK(A2))>0)
Try this (I only included the first two, I left the dates out):
=SUMPRODUCT((((Data!$A$2:$A$5=A2)+(A2=""))>0)*(((Data!$B$2:$B$5=B2)+(B2=""))>0)*(Data!$E$2:$E$5))

Excel, finding the highest date value from a list with duplicate values

I have a deep column A with IDs and each ID has in next column B date value.
Problem is that A contains up to 4 duplicate values at different places, thus e.g. 25430 could be there 4 times with different dates, e.g. 1.1.2014, 3.2.2015 etc.
I`d need to get into column C sorted column A - only 1 unique ID and if there are duplicate IDs in A then pick the highes date value.
Of course I could do it manually by sorting A by duplicate IDs and then manually delete other IDs with lower date but it`s not realistic to do it this way..
Thanks ;)
To identify the highest date value in ColumnB associated with each of the unique values in a labelled list in ColumnA:
=MAX(IF(A:A=A2,B:B))=B2
enter with Ctrl+Shift+Enter and copy down to suit.
So I was able to do this with the following formula if you have this data:
A B
Date Rank
01/01/2015
01/01/2015
01/01/2015
02/02/2015
01/01/2014
03/03/2015
04/04/2015
04/04/2015
03/03/2015
04/04/4015
So in B2, you should put the following code:
=SUM(1*(A2>$A$2:$A$11))+1+IF(ROW(A2)-ROW($A$2)=0,0,SUM(1*(A2=OFFSET($A$2,0,0,INDEX(ROW(A2)-ROW($A$2)+1,1)-1,1))))
then CTRL+SHIFT+ENTER which will turn it into an array and drag it down, this is what you will end up with:
Date Rank
01/01/2015 2
01/01/2015 3
01/01/2015 4
02/02/2015 5
01/01/2014 1
03/03/2015 6
04/04/2015 8
04/04/2015 9
03/03/2015 7
04/04/4015 10
1 Being the earliest date and 10 being the latest date.
I hope this helps :)

Excel: Trailing 3 month decline using data from a pivot table

(http://stackoverflow.com/questions/5283466/calculate-moving-average-in-excel seems to be somewhat related. I'm curious to know the non-vba way to do this.)
My my pivot table has source data that creates a count of how many sales a person had in the past 36 days. Each day os 1 column in the pivot table, and the persons name is on the row.
Some of these people did not have sales on day1, day2, etc, and may only have 3 days where they sold something on days 14,15 and 16. No matter what their sequence, I want to to find the most recent sale data (closest to the right edge of pivot table) and calculate three sales increases e.g. C20/C19 will be >1 if they had more sales on the whatever day C20 is. The increase ca nbe fond by subtracting 1 and changing to percent. The problem is, if a person only had sales on d10, d11, d12 then how can I put a general formula in Excel to say "look for the most recent consecutive sales, then calculate this ratio"? For a person who had sales in the past three days, that's easy. It will be chaotic to hardcode where to look for each sales value.
d1 d2 d3 d4...d7 d8 d9...d34 d35 d36 mostrecentincrease nextrecent
ant 1 5 7 7/5=1.4 5/1=5
bat ...10 11 12... 12/11=1.blah 11/10=1.1
cat 2 6 9 13
dog 19 20 20/19=1.blah 19/blank=0
elf 4 4/dnexist=0
i don't have excel here, but i hope this will guide you to the correct result (use , instead of ; if that is your list separator):
define sales as a range of values from d1 to d36 for a given row (or use actual ranges)
compute positions of the last values for each row using these array formulas (use ctrl+shift+enter instead of just enter after you write these formulas):
position_1 =max(if(sales<>0;column(sales)))
position_2 =max(if((sales<>0)*(column(sales)<>position_1);column(sales)))
position_3 =max(if((sales<>0)*(column(sales)<>position_1)*(column(sales)<>position_2);column(sales)))
retrieve the values:
value_1 =index(sales;position_1)
do some error handling (=iferror(...;0)) and the like...

Resources