excelpivot-tablecalculated-columns

Using column group subtotal in calculated field formula


I am pulling data into a Table in Excel using Power Query and then presenting this in a Pivot Table.

The Pivot Table has one field included in the "Columns" area, causing distinct values in that field to be transposed into the header as columns, allowing me to sum another field but grouped by each of those values, all as normal and expected.

But now I have also grouped those distinct values into two groups and displayed a column subtotal for each group, which also works fine.

But finally, I want to add an additional column to the table, showing the Grand Total as a proportion of one of the two column subtotals, and I just cannot find a way to do this.

I've tried searching around the built-in Pivot Table functionality, but couldn't find anything. I also tried instead adding the source data table to the Data Model and using Power Pivot to create a measure to achieve this, but again, had no success.

To try and help illustrate, here's a minimal example with some dummy data in it - firstly some source data in a table:

Source Data

Edit: As requested, here is the dummy source data in text form:

Project No Customer Name Document No Posting Date Account No Amount Department
P001 ACME Inc. POR-001 01/01/2024 5010 £300.00 Operations
P001 ACME Inc. POR-002 03/01/2024 5020 £100.00 Operations
P001 ACME Inc. INV-001 05/01/2024 4010 -£500.00 Operations
P001 ACME Inc. INV-002 08/01/2024 4010 -£200.00 Electrical
P001 ACME Inc. INV-003 09/01/2024 4010 -£100.00 Electrical
P001 ACME Inc. POR-003 09/01/2024 5030 £700.00 Operations
P001 ACME Inc. INV-004 12/01/2024 4010 -£950.00 Operations
P001 ACME Inc. POR-004 13/01/2024 5010 £150.00 Operations
P001 ACME Inc. CRD-001 13/01/2024 4050 -£200.00 Operations
P001 ACME Inc. INV-005 15/01/2024 4010 -£250.00 Electrical
P001 ACME Inc. CRD-002 16/01/2024 4050 -£50.00 Electrical
P002 SO Ltd POR-005 02/01/2024 5040 £850.00 Operations
P002 SO Ltd INV-006 04/01/2024 4010 -£1,000.00 Electrical
P002 SO Ltd INV-007 12/01/2024 4010 -£250.00 Electrical
P002 SO Ltd POR-006 18/01/2024 5020 £300.00 Operations
P002 SO Ltd INV-008 20/01/2024 4010 -£475.00 Electrical
P002 SO Ltd POR-007 22/01/2024 5030 £250.00 Electrical
P002 SO Ltd POR-008 25/01/2024 5010 £275.00 Operations
P002 SO Ltd POR-009 26/01/2024 5010 £290.00 Operations
P002 SO Ltd POR-010 26/01/2024 5030 £625.00 Electrical
P002 SO Ltd POR-011 27/01/2024 5020 £125.00 Electrical
P002 SO Ltd POR-012 28/01/2024 5020 £50.00 Operations
P002 SO Ltd INV-009 29/01/2024 4010 -£2,500.00 Operations
P002 SO Ltd CRD-003 29/01/2024 4050 -£250.00 Electrical
P002 SO Ltd CRD-004 29/01/2024 4050 -£150.00 Operations

The full .xlsx file I've used for this example is also available to download here.

And then how my Pivot Table looks:

Pivot

So my Pivot Table setup looks like this:

Pivot Table Setup

The "Account No2" field in the Columns area only appeared once I grouped the columns into Revenue and Cost of Sales.

So what I'm looking to achieve here is a Gross Margin % which will show on every row, regardless of what is expanded/contracted, so like this:

GM Example 1

or like this if I collapse down to Customer:

GM Example 2

Essentially, the Gross Margin % for Project P001 is 1000/2250 and for P002 it's 1860/4625 (and similarly for the figures shown when Department is expanded, showing the Gross Margin per Dpertment for that Project, instead of the entire Project overall), so I am trying to find a way to show a calculated field or similar which can perform this calculation and include it in my Pivot Table.

I hope someone has faced a similar problem before and can offer some suggestions! The example file I used to generate these screenshots can be downloaded here if it's helpful to have a play around with my dummy data and pivot.


Solution

  • Add two columns to your source data table.Making the formulae as follows:

    Invoice Val =IF([@Amount]<0,ABS([@Amount]),"")

    PO Val =IF([@Amount]>0,[@Amount],"")

    enter image description here

    In your pivot table add calculated fields. =ABS(Amount)/InvoiceVal This should give you a percentage of the absolute amount which you then need to format as percentage.

    enter image description here