Sum of max in Excel Pivot Table - excel

Please do not mark this as duplicated of Sum of Max in PivotTable for Excel because there is no even answer for it.
I have a pivot table of items grouped by ID and month, and getting the maximum Q among all items. So the totals get the maximums as well.
+------------+---------+----------------+
| A | B | C |
+-+------------+---------+----------------+
|1| Month | ID | Max(Q) |
+-+------------+---------+----------------+
|2| 1 | A | 23 |
+-+------------+---------+----------------+
|3| 1 | B | 11 |
+-+------------+---------+----------------+
|4| Subtotal 1 | 23 |
+-+------------+---------+----------------+
|5| 2 | C | 85 |
+-+------------+---------+----------------+
|6| 2 | D | 6 |
+-+------------+---------+----------------+
|7| Subtotal 2 | 85 |
+-+------------+--------------------------+
|8| Total 85 |
+-+---------------------------------------+
What I want to do is still get the max of each ID, but the totals to be the sum of the maximums. Like the following.
+------------+---------+----------------+
| A | B | C |
+-+------------+---------+----------------+
|1| Month | ID | Sum(Max(Q)) |
+-+------------+---------+----------------+
|2| 1 | A | 23 |
+-+------------+---------+----------------+
|3| 1 | B | 11 |
+-+------------+---------+----------------+
|4| Subtotal 1 | 34 |
+-+------------+---------+----------------+
|5| 2 | C | 85 |
+-+------------+---------+----------------+
|6| 2 | D | 6 |
+-+------------+---------+----------------+
|7| Subtotal 2 | 91 |
+-+------------+--------------------------+
|8| Total 125 |
+-+---------------------------------------+
I can't do the trick of this video https://www.youtube.com/watch?v=URfAkq0_dj0 because:
I can't alter the DB.
The origin table is in the data model so I think it's the reason why I'm not able to select a custom subtotal (Not sure, correct me if I'm wrong).
Any ideas?
Oh, also I'm open to use VBA.

You can't do it in pivot. But if you can add helper column to source table, you can use following array formula to calculate MAX value in group and then use this column in pivot:
{=IF(SUM((A2=$A$2:$A2)*(B2=$B$2:$B2))=1;MAX((A2=$A$2:$A$15)*(B2=$B$2:$B$15)*($C$2:$C$15));0)}
Change semicolons to commas if needed.
Array formula after editing is confirmed by pressing ctrl + shift + enter

Assuming your pivottable looks something like this:
You can right click the subtotal line, go to "Summarize Values By" and select "Sum":
Which results in:

Related

Excel - COUNTIF(s) & SUMPRODUCT

I'm trying to count clusters of values in one column but only if a value in another column is above a certain value.
I started with the below code to count how many unique clusters were in a column.
=SUMPRODUCT(1/COUNTIF(B1:B10,B1:B10))
| A | B |
| -------- | -------------- |
| 50 | 1 |
| 200 | 1 |
| 190 | 2 |
| 10 | 5 |
| 100 | 1 |
| 70 | 5 |
| 130 | 2 |
| 10 | 5 |
This would return a value of 3 as there are 3 unique clusters (1,2,5)
However, I am wanting to add a dependacy based on column A. Only count clusters in B if A>100. As there are no values of 5 in column B where A>100, the cluster count in B would be 2.
Any help to achieve the above would be very much appreciated!!
For any version:
=COUNT(1/(FREQUENCY(IF(A1:A8>100,B1:B8),B1:B8)))
array entered (with Ctrl+Shift+Enter) if non-365. If you have 365 use JvdV's answer. :)
With Microsoft365:
=COUNT(UNIQUE(FILTER(B1:B8,A1:A8>100,"")))
With older versions:
=SUMPRODUCT((A1:A8>100)*IFERROR(1/COUNTIFS(A1:A8,">100",B1:B8,B1:B8),0))

Distinct rows on Pivot Table

I'm trying to display data with a pivot table with a distinct of rows
Data
Id | Date | Count | Date 2 | ...
1 | 21/02/2020 | 1 | | ...
1 | 21/02/2020 | 1 | 21/02/2020 | ...
2 | 15/05/2021 | 0 | 15/05/2021 | ...
3 | 17/09/2020 | 2 | 17/09/2020 | ...
3 | 17/09/2020 | 2 | 19/10/2021 | ...
3 | 17/09/2020 | 2 | 25/11/2021 | ...
Expected result in the Pivot Table
Id | Date | Count
1 | 21/02/2020 | 1
2 | 15/05/2021 | 0
3 | 17/09/2020 | 2
Can you please explain me quickly how to setup the Pivot Table to get this result ?
Indeed, I'm getting 2 for id 1 and 6 for id 3 ... SUM is performed (instead of 1 and 2).
Moreover, I'm not able to put the value of the Date as a column ...
Maybe that Pivot Table is not the correct way to do it.
I hope it is clear ...
Many thanks in advance
Just adding the answer per the comment..
To achieve your Expected result, you can use a pivot table where you put those 3 fields in your "rows" value. Then change your design to Tabular mode, do not show Grand or Sub totals.
You could also simply copy those 3 columns, and paste elsewhere, then with all 3 columns highlighted do to Data and remove duplicates.

How to divide two cells based on match?

In a table 1, I have,
+---+---+----+
| | A | B |
+---+---+----+
| 1 | A | 30 |
| 2 | B | 20 |
| 3 | C | 15 |
+---+---+----+
On table 2, I have
+---+---+---+----+
| | A | B | C |
+---+---+---+----+
| 1 | A | 2 | 15 |
| 2 | A | 5 | 6 |
| 3 | B | 4 | 5 |
+---+---+---+----+
I want the number in second column to divide the number in table 1, based on match, and the result in third column.
The number present in the bracket is the result needed. What is the formula that I must apply in third column in table 2?
Please help me on this.
Thanks in advance
You can use a vlookup() formula to go get the dividend. (assuming table 1 is on Sheet1 and table 2 in Sheet2 where we are doing this formula):
=VLOOKUP(A1,Sheet1!A:B, 2, FALSE)/Sheet2!B1
Since you mention table, with structured references, though it seems you are not applying those here:
=VLOOKUP([#Column1],Table1[#All],2,0)/[#Column2]

Formula to get the month of the last value

I have sales data by customer as follows:
| - | A | B | C | D | E | F | G |
|---|---------------|--------|--------|--------|--------|--------|--------|
| 1 | Customer Name | Jan-18 | Feb-18 | Mar-18 | Apr-18 | May-18 | Jun-18 |
| 2 | Mr.A | 1000 | 500 | 0 | 200 | 0 | 0 |
| 3 | Mr.B | 0 | 300 | 200 | 0 | 0 | 100 |
I need the formula to know the last sales of the respective customer booked (the name of the month)
in this case, Mr. A last order is in Apr-18 while Mr.B is in Jun-18.
I have 2,000 plus customer and sales data since Apr 2016 up to last month, it will be a huge time saving to have a formula to help.
Assuming your 'months' are dates, not Text. Courtesy #barry houdini:
=LOOKUP(2,1/(B2:G2<>0),B$1:G$1)
in Row2 and copied down to suit, formatted mmm-yy.
Ref
An alternative to using LOOKUP() as in this answer, not sure what impact it has performance-wise as both need to create an array but I would take a stab in the dark that this is less performant:
=INDEX($B$1:$G$1,,MAX((B2:G2<>0)*COLUMN(B2:G2)-1)) - Ctrl+Shift+Enter
Ofcourse this could be edited to a dual lookup on the customer too:
=INDEX($B$1:$G$1,,MAX(INDEX(($B$2:$G$3<>0)*COLUMN($B$2:$G$3)-1,MATCH("Mr.B",$A$2:$A$3,0),0)))
This doesn't require the CSE as INDEX() handles the array manipulation

Getting Summation from a Table, with matching values from another Table in Excel

I have 2 tables created in Excel, which are identical in structure and the column and row names.
The only difference is that the first table has data (for the effort in work days) in it while the second is a reference table stating which milestone each cell belongs to. A sample of these tables is:
TBL1:
| | App1 | App2 | App3 |
| T1 | 32 | 12 | 48 |
| T2 | 40 | 16 | 30 |
| T3 | 56 | 18 | 36 |
TBL2:
| | App1 | App2 | App3 |
| T1 | 1 | 2 | 3 |
| T2 | 2 | 1 | 2 |
| T3 | 1 | 1 | 1 |
I want to collate these values so that I get SUM of 1, 2 and 3
| | Days Summation |
| 1 | =32+56+16+18+36 |
| 2 | =40+12+30 |
| 3 | =48 |
So basically, want to find:
IF(COL_VAL_IN_TBL2=1) THEN SUM ALL VALUES IN TBL1 CORRESPONDING TO THE ROW-COL IN RESPECTIVE
Is it possible to get a formula which I can use to do this without using something like a Pivot Table?
You can use sumif() to do this:
Here it's just looking at table2 values and comparing them to your 1, 2, or 3 and then summing the corresponding cells from your table1
SUMIF will do the trick if I understand correctly:
If you put 1 in A1, then 2 in A2, etc. Then enter in B1=SUMIF(TBL2Range,A1,TBL1Range) and copy down. Where TBL2Range is the address of your table.

Resources