I have a Google Sheet (Excel formulas apply) containing 2000 transactions that are structured like below:
Link to Google Sheet with example calcs here.
I need to calculate the Profit (ie. FIFO Capital Gains) for every sell order and place this amount in the 5th column (as pictured). This can be achieved with either traditional formulas or with a Google App Script. Preferably in Google Sheets.
I'm finding it difficult via formulas to match the sell order with the buy as you proceed down the table as some sell orders are split across multiple buy lines. If anyone could help with a formula or solution to tackle this problem it would be much appreciated. This is an updated question to the original here.
PRELIMINARY CONTEXT
Screenshot below refers:
Google sheets here
IMPORTANT UPDATE: re: 2000+ rows, see link in my last comment (for OneDrive template, s.t. expiry, and bottom of this proposal for screenshot of error "too large to import")
fyi: (Office 365 - Excel - fns. such as 'filter' etc., so #Name! will be ubiquitous upon opening this link; however, sharing this should serve as a convenient means to replicate above screenshot in the correct version of Excel)
REQUIREMENTS
Office 365
Adequate space to the right or ability to utilise extra sheet for calcs as req.
APPROACH
High-level | Take-aways
• Relies on 2 additional tables: Qty 'depletion' table (here - G:P) and corresp. Profit Vectors (here - R:Z)
• Compressed representations (2x2 tables, per Summary Tables, here: rows 19:32) could be explored to simplify matters (albeit these are still Work in Progress ['WiP'])
• VB could be attractive alternative too
Low-level | Procedural
Single-cell function could not be found without exceptional complication (however, suspect it could be possible e.g. using FilterXML?)
Qty label transposes filtered negative quantities (grey shaded, G3: J3)
These values are depleted in turn (from left to right, i.e. dependency on having data sorted in ascending order by date (in this depiction, data first sorted by 'fruit' as these are assumed to be independent in the context of the profit calc.)
For example, -5 quantity (G3) offset by +10 (B3) to yield +5 which, in turn contributes towards the -35 qty (H3) to yield -30. No further contribution can be made (left to right); next row (5): +20 avail (given), no offset against -5 (G3, already 'reimbursed', so 20 offset against -30 (H4) to yield -10, and so forth.
Worksheet set up to accommodate longer list of fruit / profits
Result table of residual 'quantities' (~ G:J) applied to price differential to yield 'profit vectors' (R:U) which are summed against corresponding negative quantities to produce Profit score (yellow shaded cells)
FUNCTIONS
Salient functions (all of which should be available within Google Sheet, but for completeness):
1) Profit calc. (E4, drag down):
=IF(B4<0,SUM(INDEX($R$4:$AA$13,0,MATCH(A4&C4,$R$2:$AA$2&$R$3:$AA$3,0))),"")
This can be dragged down / to right as far as desired (in this case, goes to: Z13)
2) Qty table - contents (G4)
=IF($A4=G$2,IF(G$3<>"",IF(G$2=E$1,IF($B4<0,IF(G3<0,G3,0), IF(E4<0,0,E4)+IF(G3<0,G3,0)), IF(G$3<>"", IF($B4<0,IF(G3<0,G3,0),$B4 + IF(G3<0,G3,0))))),G3)
3) Qty table - headers (G2, G3 resp.)
=MID(TRANSPOSE(FILTER(A4:A13&B4:B13,B4:B13<0)),1,SEARCH("-",TRANSPOSE(FILTER(A4:A13&B4:B13,B4:B13<0)))-1)
=-1*MID(TRANSPOSE(FILTER(A4:A13&B4:B13,B4:B13<0)),SEARCH("-",TRANSPOSE(FILTER(A4:A13&B4:B13,B4:B13<0)))+1,LEN(TRANSPOSE(FILTER(A4:A13&B4:B13,B4:B13<0))))
Drag to right as far as desired / able to (this case have been dragged to col. P)
4) Profit table - contents (R4, drag down/right)
=IF(R$2<>"",IF(G3>0,0,IF(G4>0,ABS(G3),G4-G3))*(R$3-$C4),"")
5) Profit table - headers (R2, R3 resp.):
=MID(TRANSPOSE(FILTER(A4:A13&"-"&$C$4:$C$13,B4:B13<0)),1,SEARCH("-",TRANSPOSE(FILTER(A4:A13&"-"&$C$4:$C$13,B4:B13<0)))-1)
=1*MID(TRANSPOSE(FILTER(A4:A13&"-"&$C$4:$C$13,B4:B13<0)),SEARCH("-",TRANSPOSE(FILTER(A4:A13&"-"&$C$4:$C$13,B4:B13<0)))+1,LEN(TRANSPOSE(FILTER(A4:A13&"-"&$C$4:$C$13,B4:B13<0))))
MISC
Alternate solutions derived & explored include triangular frameworks
(rows 17-32).
These tabulate the interaction between values assoc.
with positive and negative quantities positive/negative quantities
(and corres. dates)
Should be noted these are otherwise unnecessary (in terms of functions, previous 'section'
Continuing in the same vein as before....
6a) 1st summary table (Qty): row and col headers (A22 - drag down, C19 - drag right resp.):
=FILTER(D4:D13,1*IF($A$21="*",1,(A4:A13=$A$21))*(B4:B13<0))
=TRANSPOSE(FILTER(D4:D13,1*IF($A$21="*",1,IFERROR((A4:A13=$A$21),1))*(B4:B13>0)))
6b) 1st summary table (Qty): running total (c19, drag down / right as req., in this case: to P19)
=SUM($C21:C21,$B22#)
6c) 1st summary table (Qty): content (c22, drag down or tonight as req.)
=IF(OR(C$20="",$A22=""),"",IF($A22>C$20,MAX(0,C21+MIN(0,SUM($B22:B22))),C21))
7) 2nd summary table (Profits): C29, not draggable
=MIN(C$21,C$21+$B22)*($B29-C$28)
Note; other features of 2nd summary table (row / col headers) follow from 1st. Also, placing '*' in the fruit selection cell (A21) returns all (this is still WiP as mentioned previously).
SCREENSHOTS FOR LARGER UPLOAD:
I am using the excel's pivot table to sort and filter data from Super Store Dataset from tableau . Here is a screen shot of my output.
The steps that I followed are:
a. Put Order Date in the Rows.
b. Put Sub-Category in the Columns.
c. Put the Profit in the Sum of Values.
After that I took these steps:
d. Sort the Grand Total of the Sub Category from largest to smallest.
e. Sort the Grand Total of Order Date from largest to smallest.
f. Filter Top 3 from the Sub-Category.
g. Filter Top 10 from the Order Date.
And the above image of an excel sheet shows my output.
Now the problem is , even though the excel sheet was supposed to show me the top 10 orders , it only manages to show 7 to 8 orders. The rest 2 to 3 of them are either blank or should not even belong to the top 10 category.
Does anybody knows why is this happening. And how can this be prevented.
Thanks.
Edit: This is how the top 15 looks like :
I believe top 10 is based on the vertical Grand Total and it should also be a subset of the top 15.
And this is the top 20 :
It looks like this is caused by opening the .xls workbook in Compatibility Mode. This means that Version 10 pivot tables are created, which have different filtering functionality to the later, Version 12 Pivot Tables.
Change the workbook to an upgraded file format (.xlsx), and refresh your pivot tables - you'll get an interesting message informing you of the changes made to Top 10 filtering, and then the multiple Top N filters will apply correctly:
Here's some more information about Pivot Table versions / compatibility, from Microsoft
The more I have to work with Excel in helping our customers with advanced reporting, the more I'm amazed at it's capabilities. That being said, I can't seem to find exactly what this customer wants Excel to do.
Scenario:
We have a datasource which refreshes a worksheet containing many rows and columns of a data export from the customer's database. That worksheet then drives the other tabs' charts, tables, etc.
They want to add a table (pivot) which will take their milestones, sum the (ontime) column, and then take the (rownumber) sum for that area and then divide it so they can see how many projects were on time. Simplified data tab looks like this:
RowNum Area OnTime Milestone
------ ---- ------ ---------
1 North 1 M2
2 East 1 M4
3 East 1 M2
4 North 0 M1
5 East 0 M4
and here's the table they want it to produce.
We can get it to do both using a Sum(value) and calculated field, however we can't get the Sum(value) field to go directly beneath the calculated percentage field. Any ideas?
If I understand correctly, it will be enough to move the Values drawer to your Rows:
If you need to change the order of the values (which one will be at the top etc.), you can do this moving the drawers within Values Window at bottom-right.
I have an excel sheet in which the chart is dynamically generated each time it runs based on a data table within the sheet. However, due the Excel Series 255 limitation, the chart stopped generating.
Is there a way I can define a subset of Series so the chart can dynamically pull its related data from the table?
For example if I have:
Product Q1 Q2 Q3 Q4
Apples 1 2 3 4
Bananas 3 4 5 6
Oranges 4 5 6 7
Strawberries 1 2 3 5
Within excel, how can I define only Apples, Oranges and Strawberries for the line chart so it will display its related Q-values?
TIA
If I understand you correctly, you can use a simple filter. For example, here is your data plotted originally.
And here is the data after having
applied a filter (Data Ribbon / Sort & Filter Tab / Filter)
Select the table area
execute GoTo Special / Visible Cells Only
Insert a new graph
If this is something that needs to be done frequently, you could write a VBA macro to better automate the process.
If you change a value in a visible series, the chart will change. But if you change the filter, you will need to go through the process again.
I have a question about excel.
There is a sports tournament with multiple phases. And the results page looks something like this:
rank phase1 phase2
1 TOM ALBERT
2 MATT TOM
3 ROBIN MATT
The first place gets 5 points, second 3 and third 1 points.
So the summary I would like to get is like this
rank name phase1 phase2 total
1 TOM 5 3 8
2 ALBERT 0 5 5
3 MATT 3 1 4
4 ROBIN 1 0 1
I cant figure out how to generate it simply, so if I copy-paste the results from phase3 to somewhere in my excel sheet, the summary page would also update.
If you can support me with advice or just a working template, I would be thankful!
Thank you for your time!
I've done you a basic example below...just list all 50 players in the summary page and copy the formula down as it is - as per the question it only records a value for the top 3 places, hope this helps if not please let me know :)
Example file here
Assuming the structure you defined and that when you paste phase 3 it will have its header ("phase3") on the same row as the other results, you could have the following formula on your Table 2:
{=SUM(IF(IFERROR(MATCH($B2,INDIRECT("Sheet1!"&CHAR(64+MATCH(C$1,Sheet1!$1:$1,0))&":"&CHAR(64+MATCH(C$1,Sheet1!$1:$1,0))),0)-1,0)={1,2,3},{5,3,1},0))}
Note: entered as Array formula: CTRL + SHIFT + ENTER
Formula uses the Table 2 headers ("phase1","phase2", etc.) and the players names to find the results for each phase. It then uses the predefined arrays that determine points given per position. The final sum is to bring the result that are in array (other values in array are 0) into single value.
Total would sum results per player.
Under the rank cells you would place the following formula:
=RANK(F2,$F:$F)
Which would provide you the rank of each player (Column F containing the totals).
Note that this would not sort your rank automatically but you could easily do this with Sorting or Autofilter. Hope this helps. Cheers.