Formula in pivot table

sharshra

Active Member
Joined
Mar 20, 2013
Messages
404
Office Version
  1. 365
I want to insert a formula in the pivot table. I tried to include a 'calculated field', but couldn't get the correct result when I have to calculate based on 2nd level. I think the example makes it clear.

It is an example from stock trading book. I have to calculate what quantity of stock is left after buy / sell actions. Also, I have to calculate if there is profit or loss on overall stock. I have added quantity & value for each stock for buy & sell actions in a pivot table. What I want to achieve is shown in separate columns (final qty, investment / gain) outside the pivot table. These columns show the difference between quantity & price for sell & buy. I want these columns to be part of the pivot table.

When I include calculated field in pivot table, it just subtracts without considering buy / sell. It doesn't give the required result. I'm sure there must be a way to do this & I'm making some silly mistake :( Can someone help me to fix this please?

Base data -
pivot.xlsx
BCDEFGH
2Sl.#DateStock nameActionQtyPriceValue
3119-Jan-22xxxBuy50061.7530,875.00
4224-Jan-22yyyBuy20097.0019,400.00
5325-Jan-22xxxSell50065.0032,500.00
6427-Jan-22yyySell20096.5019,300.00
7527-Jan-22zzzBuy10077.007,700.00
trade book
Cell Formulas
RangeFormula
H3:H7H3=F3*G3
Cells with Data Validation
CellAllowCriteria
E3:E7ListBuy,Sell


Pivot table -
pivot.xlsx
KLMNOPQ
4Column Labels
5BuySell
6Row LabelsSum of QtySum of ValueSum of QtySum of ValueFinal qtyInvestment / gain
7xxx500308755003250001625
8yyy20019400200193000-100
9zzz1007700100-7700
10Grand Total8005797570051800-6175
trade book
Cell Formulas
RangeFormula
P7:P9P7=L7-N7
Q7:Q9Q7=O7-M7
Q10Q10=SUM(Q7:Q9)
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
You are trying to do Calculated Field, you want Calculated Item.
Click on either the word Buy or Sell in the pivot heading and then under PivotTable Analyze pick calculated item.
Then calculate the difference between buy and sell.

(Buy & Sell are in the same field in the underlying data which makes them Items in a field. If they are in different columns in the underlying data then they are separate fields so you use calculated fields)
1645965477627.png
 
Upvote 0
Solution
Thanks, Alex. Your solution fixed my problem:) As always, you are of great help(y). Really appreciate?

I would like to make few changes though. Now I have the formula 'Diff=Sell-Buy'. It is correct for the value field. For quantity field, I would like to have 'Diff=Buy-Sell'. Is it possible to have 2 different formulas?

Also, I have another problem. When I try to group fields in another pivot table, it doesn't allow me to group. I believe it is due to linking of different pivot tables referring same base data. I get the following message. It is one of very annoying thing in excel? I don't know why pivot tables should be linked by default & why is it complicated to unlink them? I'm trying to search a solution to fix this. Any tips for the same will help.
1646114838543.png


Pivot table -
pivot.xlsx
KLMNOPQ
4Column Labels
5BuySellDiff
6Row LabelsSum of QtySum of ValueSum of QtySum of ValueSum of QtySum of Value
7xxx500308755003250001625
8yyy20019400200193000-100
9zzz1007700-100-7700
10Grand Total8005797570051800-100-6175
trade book


Base data -
pivot.xlsx
BCDEFGH
2Sl.#DateStock nameActionQtyPriceValue
3119-Jan-22xxxBuy50061.7530,875.00
4224-Jan-22yyyBuy20097.0019,400.00
5325-Jan-22xxxSell50065.0032,500.00
6427-Jan-22yyySell20096.5019,300.00
7527-Jan-22zzzBuy10077.007,700.00
trade book
Cell Formulas
RangeFormula
H3:H7H3=F3*G3
Cells with Data Validation
CellAllowCriteria
E3:E7ListBuy,Sell
 
Upvote 0
I would like to make few changes though. Now I have the formula 'Diff=Sell-Buy'. It is correct for the value field. For quantity field, I would like to have 'Diff=Buy-Sell'. Is it possible to have 2 different formulas?
Yes & No. You can create a new calculation (just give it a different name). However both Qty and Value will show both names.
Don't you think it is going to be a bit confusing to have the positive and negative reversed on the 2 value columns ?

why is it complicated to unlink them? I'm trying to search a solution to fix this. Any tips for the same will help.
Yes unlinking 2 pivots from the same Cache is a bit fiddly. If you are ok with running some code that is probably the quickest and easiest way.

The Code below is from Debra's Contextures site:
Excel Macros for Pivot Table Pivot Cache - Lists, Info

Just put your cursor in a cell in the pivot you want to unlink.
Copy the macro into a module and run it.

VBA Code:
' Contextures:- Create New Cache for Selected Pivot Table
Sub SelPTNewCache()
    Dim wsTemp As Worksheet
    Dim pt As PivotTable
    
    On Error Resume Next
    Set pt = ActiveCell.PivotTable
    
    If pt Is Nothing Then
        MsgBox "Active cell is not in a pivot table"
    Else
        Set wsTemp = Worksheets.Add
        
        ActiveWorkbook.PivotCaches.Create( _
            SourceType:=xlDatabase, _
            SourceData:=pt.SourceData).CreatePivotTable _
            TableDestination:=wsTemp.Range("A3"), _
            TableName:="PivotTableTemp"
        
        pt.CacheIndex = wsTemp.PivotTables(1).CacheIndex
        
        Application.DisplayAlerts = False
        wsTemp.Delete
        Application.DisplayAlerts = True
    End If
    
exitHandler:
        Set pt = Nothing

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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