Percentage of Sum of two Pivot cells - excel

I'm trying to work out a small problem with my excel Pivot table. I have data from a Excel Sheet which i have made a Pivot table of. The data is structured as below
Name | Count Cell1 | Sum of Cell 2 |
Eric | 25 | 5 |
Sam | 5 | 1 |
Joe | 10 | 5 |
What i want to have is a formula that takes the Count of Cell 1 and divide it by Sum of Cell 2 and display it in % like the example below.
Name | Count Cell1 | Sum of Cell 2 | Difference|
------------------------------------------------
Eric | 25 | 5 | 20% |
Sam | 5 | 1 | 20% |
Joe | 10 | 5 | 50% |
All formulas i have tried only uses the original Table cells and not the sums of them.
So is there a smart way to have a formula lookup inside of a pivot table and display it in %?

In your Pivot Table, you can enter a calculated field to do what you want.
Select somewhere in your pivot table (e.g. one of the Sum of Cell2 fields)
In the PivotTable Tools > Options ribbon, in the Calculations section, click Fields, Items & Sets and from there pick Calculated Field
Change the name to Difference and the Formula =Cell2/Cell1
In the Field Settings for that field, change the Custom Name to Difference and Number Format to Percentage
EDIT - question updated for Count & Sum
So, as far as I can see, trying to do the combination of Sum/Count really upsets it... the only workaround I could find was adding a helper column in the data source with just the number 1... in that way, the sum of that gives you the count, and so the Calculated Field can be Cell2/HelperColumn -horrible!

Related

Excel Sumproduct/Sumif based on multiple criteria across two tables not in order

I've been struggling with an issue for a while now and haven't been able to find a solution, so any help would be greatly appreciated!
I'm trying to build a formula that sums up values from a column based on criteria spread across two tables (I've simplified below):
Table 1
+-------------+---------+---------------------+------------+----------+------+
| Customer ID | Twin ID | Customer Entry Date | Exit Date | Spending | Days |
+-------------+---------+---------------------+------------+----------+------+
| 111 | 333 | 24.12.2015 | 28.05.2018 | 5000 | 200 |
| 222 | 444 | 19.06.2014 | | 4000 | 300 |
+-------------+---------+---------------------+------------+----------+------+
Table 2
+-------------+---------+---------------------+-----------+----------+------+
| Customer ID | Twin ID | Customer Entry Date | Exit Date | Spending | Days |
+-------------+---------+---------------------+-----------+----------+------+
| 444 | | | | | 200 |
| 333 | | | | | 0 |
+-------------+---------+---------------------+-----------+----------+------+
I now need to find a formula, that will allow me to sum up the column "Spending" from table 1 based on the following criteria:
"Twin ID" in Table 1 is not empty and the value matches the value "Customer ID" in Table 2 --> this has been the main complication for me, as the Customer IDs in Table 2 are in a different order than the Twin IDs in Table 1
"Entry Date" in Table 1 is < a specific date
"Exit Date" in Table 1 is >= a specific date or empty
"Days" in Table 2 is >0 (for the respective Customer ID that matches the Twin ID from Table 1)
Or in other words: "If customers 111,222 etc. have a twin, and this twin has days >0, and the entry and exit dates of the customer are < > a specific date or empty, then sum up the spending of those customers"
I've tried various iterations of the SUMPRODUCT formula, and this one currently works as long as the two tables are in the same order (i. e. Twin ID "333" is in row 2 in Table 1 and in row 2 in Table 2):
=SUMPRODUCT(--(Table1!Customer Entry Date<DATE1);--(Table1!Exit Date>=Date2);--(Table1!TwinID<>"");--(Table2!Days>0);Table1!Spending)
Is there any way to make this formula work regardless of the order of the row items (i. e. Twin ID "333" is in row 2 in Table 1 and in row 3 in Table 2)?
Any help would be greatly appreciated!
Try this
=SUMPRODUCT((Table1!Customer_Entry_Date<Date1)*(Table1!Customer_Entry_Date>Date2)*(Table1!Twin_ID<>"")*(COUNTIFS(Table2!Customer_ID,Table1!Twin_ID,Table2!Days,">0")>0)*Table1!Spending)
It's similar to your formula, but uses Countifs to see if a matching Customer ID for Table 1's Twin ID is anywhere in table 2.
Note that your named ranges (if that's what you're using) should not include the column headers or else you'll get a #Value error when it tries to do the multiplication.
You could avoid it by putting IF(Isnumber()) around the last part of the bracket, but then it would have to be entered as an array formula
=SUM((Table1!Customer_Entry_Date<Date1)*(Table1!Customer_Entry_Date>Date2)*(Table1!Twin_ID<>"")*(COUNTIFS(Table2!Customer_ID,Table1!Twin_ID,Table2!Days,">0")>0)*IF(ISNUMBER(Table1!Spending),Table1!Spending))
I managed to solve the issue.
Anyone facing a similar problem, please see the example file for solution: https://wetransfer.com/downloads/90aedc5943f52274e36102a79e23c18e20180628212338/2fd1c1
=+SUMPRODUCT(SUMIF(Table1TwinID;Table2CustomerID;Table1Spending)*(Table2Days>0)*((COUNTIFS(Table1TwinID;Table2CustomerID;Table1EntryDate;"<"&DATE1)*COUNTIFS(Table1TwinID;Table2CustomerID;Table1ExitDate;">="&DATE2))+(COUNTIFS(Table1TwinID;Table2CustomerID;Table1EntryDate;"<"&DATE1)*COUNTIFS(Table1TwinID;Table2CustomerID;Table1ExitDate;""))))

Excel: Average of parts of a column based on another column

Using Excel 2011 for mac, I have three columns: ID, Start date, end date and time to completion. (Date format: dd/mm/yyyy)
ID | Start | End | Time
1 | 01/01/2016 | 05/01/2016 | 4
2 | 04/01/2016 | 08/01/2016 | 4
3 | 01/02/2016 | 14/02/2016 | 13
4 | 02/02/2016 | 20/02/2016 | 18
5 | 01/03/2016 | 05/03/2016 | 4
6 | 06/03/2016 | 08/03/2016 | 2
7 | 12/03/2016 | 15/03/2016 | 3
Column D is basically the difference between column C and column B.
Now I have the total average, which is easy to calculate but I'd also like to have the averages for the different months.
And this is where my knowledge falls apart. I've tried several things but I can't seem to figure out how to calculate an average using only the cells in a column that match a certain value in another column. I could sort the tickets by date and do it manually by doing the average for only a certain range but as this list constantly changes this is definitely not a nice option.
Check out the AVERAGEIF function
AVERAGEIF(selection_range, criteria, averaging_range)
It uses the values in the selection_range to filter which values in the averaging_range will be averaged.
In your case you could say AVERAGEIF(B1:B8, "01/01/2016", D1:D8)
There are multiple ways. I would personally use an array formula, but that may be a bit advanced and overly complicated.
I suggest adding a column E "Month" - into E2 add:
=MONTH(B2)
Then copy cell E2 to E3:E8.
Now you can easily get a monthly average by applying the AVERAGEIF command:
=AVERAGEIF(E2:E8,2,D2:D8)
The second argument, 2, indicates February, but may be exchanged with any number from 1-12.

How to automatically insert a blank row after a group of data

I have created a sample table below that is similar-enough to my table in excel that it should serve to illustrate the question. I want to simply add a row after each distinct datum in column1 (simplest way, using excel, thanks).
_
CURRENT TABLE:
column1 | column2 | column3
----------------------------------
A | small | blue
A | small | orange
A | small | yellow
B | med | yellow
B | med | blue
C | large | green
D | large | green
D | small | pink
_
DESIRED TABLE
Note: the blank row after each distinct column1
column1 | column2 | column3
----------------------------------
A | small | blue
A | small | orange
A | small | yellow
B | med | yellow
B | med | blue
C | large | green
D | large | green
D | small | pink
This does exactly what you are asking, checks the rows, and inserts a blank empty row at each change in column A:
sub AddBlankRows()
'
dim iRow as integer, iCol as integer
dim oRng as range
set oRng=range("a1")
irow=oRng.row
icol=oRng.column
do
'
if cells(irow+1, iCol)<>cells(irow,iCol) then
cells(irow+1,iCol).entirerow.insert shift:=xldown
irow=irow+2
else
irow=irow+1
end if
'
loop while not cells (irow,iCol).text=""
'
end sub
I hope that gets you started, let us know!
Philip
Select your array, including column labels, DATA > Outline -Subtotal, At each change in: column1, Use function: Count, Add subtotal to: column3, check Replace current subtotals and Summary below data, OK.
Filter and select for Column1, Text Filters, Contains..., Count, OK. Select all visible apart from the labels and delete contents. Remove filter and, if desired, ungroup rows.
This won't work if the data is not sequential (1 2 3 4 but 5 7 3 1 5) as in that case you can't sort it.
Here is how I solve that issue for me:
Column A initial data that needs to contain 5 rows between each number -
5
4
6
8
9
Column B -
1
2
3
4
5
(final number represents the number of empty rows that you need to be between numbers in column A) copy-paste 1-5 in column B as long as you have numbers in column A.
Jump to D column, in D1 type 1. In D2 type this formula - =IF(B2=1,1+D1,D1)
Drag it to the same length as column B.
Back to Column C - at C1 cell type this formula - =IF(B1=1,INDIRECT("a"&(D1)),""). Drag it down and we done. Now in column C we have same sequence of numbers as in column A distributed separately by 4 rows.
Figured it out.
Step 1
Put a new column to the left of column1 and copy+paste the following formula
=B2=B3
=B3=B4
=B4=B5
... all the way to the bottom (assume column B here is column1 in the original question).
This formula evaluates whether or not the next row is a new value in column1. Deopending on the result, you will have TRUE or FALSE. Copy and Paste these results as values and then swap "FALSE" for nil and "TRUE" for 0.5
Step 2
Then add that column full of only 0.5's to the column1 which will yield the following table:
newcolumn0 | column1 ("B") | column2 | column3
-----------------------------------------------------
| 1 | small | blue
| 1 | small | orange
1.5 | 1 | small | yellow
| 2 | med | yellow
2.5 | 2 | med | blue
3.5 | 3 | large | green
| 4 | large | green
4.5 | 4 | small | pink
Step 3
Lastly, copy and paste the values from newcolumn0 right below the values in column1 and then sort the table by column1 and you should have a blank row in between each distinct whole number in column1, with the table something like this:
newcolumn0 | column1 ("B") | column2 | column3
---------------------------------------------------------------
| 1 | small | blue
| 1 | small | orange
1.5 | 1.5 | |
| 1 | small | yellow
| 2 | med | yellow
| 2 | med | blue
2.5 | 2.5 | |
| 3 | large | green
3.5 | 3.5 | |
| 4 | large | green
| 4 | small | pink
4.5 | 4.5 | |
Alternative Solutions (still no VBA)
Put a value of 1 Column 1, Row 2 (assume this is A2)
Put this formula in A3 =IF(B3=B2,A2,A2+1) and copy+paste this formula for the rest of column 2
Then copy and paste all the values from column 1 into a new temp excel sheet, remove duplicates, then add 0.5 to all numbers, then paste these values below the values in original spreadsheet below the data in column 1, paste all data in column as values and then sort by that column, delete the temp excel sheet
Just an idea, if you know the categories, as small, medium, and large mentioned above...
At the bottom of the sheet, make 3 rows that only say small, medium, and large, change the font to white, and then sort so that it alphabetizes, placing a blank row between each section.
Insert a column at the left of the table 'Control'
Number the data as 1 to 1000 (assuming there are 1000 rows)
Copy the key field to another sheet and remove duplicates
Copy the unique row items to the main sheet, after 1000th record
In the 'Control' column, add number 1001 to all unique records
Sort the data (including the added records), first on key field and then on 'Control'
A blank line (with data in key field and 'Control') is added
I have a large file in excel dealing with purchase and sale of mutual fund units. Number of rows in a worksheet exceeds 4000. I have no experience with VBA and would like to work with basic excel. Taking the cue from the solutions suggested above, I tried to solve the problem ( to insert blank rows automatically) in the following manner:
I sorted my file according to control fields
I added a column to the file
I used the "IF" function to determine when there is a change in the control data .
If there is a change the result will indicate "yes", otherwise "no"
Then I filtered the data to group all "yes" items
I copied mutual fund names, folio number etc (no financial data)
Then I removed the filter and sorted the file again. The result is a row added at the desired place. (It is not entirely a blank row, because if it is fully blank, sorting will not place the row at the desired place.)
After sorting, you can easily delete all values to get a completely blank row.
This method also may be tried by the readers.

MS Excel - finding the first row after a certain date

Say I have a spreadsheet with the following, and for convenience say all of this starts from cell A1.
---------------------------------------
| Date | Item | Account |
---------------------------------------
| 01/09/2011 | Testing 1 | USD |
| 03/09/2011 | Testing 2 | USD |
| 11/09/2011 | Testing 3 | USD |
| 20/10/2011 | Testing 4 | JD |
| 22/10/2011 | Testing 5 | JD |
| 25/10/2011 | Testing 6 | USD |
| 03/11/2011 | Testing 7 | USD |
| 05/11/2011 | Testing 8 | JD |
---------------------------------------
Now, I want to run a report for a month, starting on 1/10/2011 and ending on 31/10/2011. I need to find the first row on or after the starting date, and then get every subsequent row until the end date. If I can figure out how to get the row reference for the first and end dates, then I can figure out the rows in between (obviously!).
I have only been able do these sorts of matches on exact matches ie. no idea how to do 'greater/less than' matches.
How would I go about matching on both the date and the account columns?
Needless to say, this needs to be in a formula.
=match(date(2011,10,1),a2:a9,1)+1
=match(date(2011,10,31),a2:a9,1)
First formula shows row for the first record for October, second formula for the last day. Data must be sorted in ascending order.
Use the following Array Formula for finding the Row containing the earliest date, which is equal to or greater than the date mentioned in cell C1 (in your case this is 1 October).
=MATCH(MIN(IF($A$1:$A$30>=C1,1,9999)*$A$1:$A$30),$A$1:$A$30,0)
Date list is in cells A1 to A30. Change the references as required.
Data need not be sorted in ascending or descending order.
Use the following Array Formula for finding the Row containing the latest date which is equal to or less than the date mentioned in cell D1 (in your case this is 31 October). Data need not be sorted in ascending or descending order.
=MATCH(MAX(IF($A$1:$A$30<=D1,1,0)*$A$1:$A$30),$A$1:$A$30,0)
If you want the earliest and latest dates, use the following Array Formulas.
=MIN(IF($A$1:$A$30>=C1,1,9999)*$A$1:$A$30)
=MAX(IF($A$1:$A$30<=D1,1,0)*$A$1:$A$30)
All the formulas used above are Array Formulas. To enter an array formula, use Control+Shift+Enter instead of Enter.
Vijaykumar Shetye, Goa, India
I would recommend using a pivot table for this. Look at the second link on in the "Excel Templates - Pivot Table" section on this page on the Contextures site.

Excel Formulas: Show total based on date entry

I've got a spreadsheet with two columns that represent the number of processed records, and the date the records were processed. In some cases, the records can be processed in multiple batches, so the document looks something like this:
33 4/1/2009
22 4/1/2009
12 4/2/2009
13 4/4/2009
36 4/4/2009
I'm trying to add a new set of columns that contain a date, and shows the total number of records for that date, automagically:
4/1/2009 55
4/2/2009 12
4/3/2009 0
4/4/2009 49
I know how to do this algorithmically, and I could probably manipulate the spreadsheet outside of Excel, but I'm trying to do this in the live spreadsheet, and am a bit bewildered as to how to pull it off.
Any ideas?
Thanks!
IVR Avenger
Will the SUMIF function work for you? SUMIF([range],[criteria],[sum_range]) I think you could set range = the set of cells containing dates in your first listing, criteria would be the cell containing the date in the second listing, and sum_range would be the counts in the first column of your first listing.
I would suggest using a Pivot Table. Put the dates into the row area and 'sum of' records in the data area. Nothing in the columns area.
A pivot table will be more dynamic than a formula solution because it will only show you dates that exist.
Assuming your dates are in column B and the numbers to be accumulated are in A, you could try something like this:
| A | B | C D
1 | 33 | 4/1/2009 | =MIN(B:B) | {=SUM(IF(B1:B5=C1,A1:A5,0))} |
2 | 22 | 4/1/2009 | =C1+1 | {=SUM(IF(B1:B5=C2,A1:A5,0))} |
3 | 12 | 4/2/2009 | =C2+1 | {=SUM(IF(B1:B5=C3,A1:A5,0))} |
4 | 13 | 4/4/2009 | =C3+1 | {=SUM(IF(B1:B5=C4,A1:A5,0))} |
5 | 36 | 4/4/2009 | =C4+1 | {=SUM(IF(B1:B5=C5,A1:A5,0))} |
Note the {} which signifies an array formula (input using Control-Shift-Enter) for any non-trivial amount of data it's heaps faster than SUMIF().
I'd be inclined to define dynamic names for the A1:A5 and B1:B5 parts, something like
=OFFSET(A1,0,0,COUNT(A:A),1)
so that I didn't have to keep fixing up my formulae.
There's still a manual element: adding new rows for extra dates, for example - that might be a good place for a little VBA. Alternatively, if you can get away with showing, for example, the last 90 days' totals, then you could fix the number of rows used.

Resources