Excel - Pivot Slicer or Via VBA? - excel

I am posting this to all Excel Champs and Code Experts. I am familiar with Pivot and Slicer feature of excel however came with an requirement and I could not think of any way I can achieve it via pivot ( preferably )
The Requirement is like this. I have team data in col 1 and Sales figures in col 2. Now with a simple pivot, I can have the team totals and things like that but I need to put in slicer on team name. If I select Team one , it should show Team one sales against total of other three team . PLease advice if there is a way to do that. In summary I need to compare a person's /team performance against total peers
Team Sales
One 20000
Two 15000
Three 500
Four 35000
One 500

You can achieve this with formula next to your PivotTable values. You will use formula against your total range of values in col2. If your columns are set to range A:B, and PivotTable is in range D:E, then put this formula =E2/(SUM($B$2:$B$30)-SUMIF($A$2:$A$30,D2,$B$2:$B$30)) in cell F2. You should get result as follows.
If you need to have result inside your PivotTable, then you should use PowerPivot and create a measure. try looking at this for more info: https://msdn.microsoft.com/en-us/library/gg399161(v=sql.110).aspx

Related

How to calculate average using calculated field in Excel Pivot Table?

I have a question concerning calculated fields in Excel Pivot Table.
I have attached an image for perusal.
Observed that in the attached image below, what happened was that I tried to find out using the calculated field in pivot table, the Average Profit Per Genre.
I have summarized both the Sum Of Budget and Sum Of Box Office Revenue to Average Of Budget and Average Of Box Office Revenue respectively.
However, it seems that calculated field won't allow me to minus the Average Of Budget and Average Of Box Office Revenue, so that I can get the Average Profit Per Genre.
It seems that calculated field would only calculate the Sum Of Budget and Sum Of Box Office Revenue.
Would like to ask if there are any other way around this? So that i can find out the Average Profit Per Genre?
Thanks!
Calculated Fields always sum fields, no matter what aggregation you set via the Value Field Settings dialog box.
You can either add your table to the Data Model, i.e. use Power Pivot, or else add a dummy column to your table - named 'Dummy', say - which contains the value 1 in every row. You can then use the following Calculated Field:
= ('Box Office Revenue' - Budget) / Dummy
I see the Excel Pivot Table you are trying to create is from the Hands-On Activity in the Google Professional Certificate in Data Analytics - Course 5.
I have a workaround to solve the problem, though it does not directly address the Calculated Fields issue.
STEP ONE:
As Pivot Table will not allow you to add a column, start typing in Cell D3, the heading of the column as "Average Profit".
In Cell D4, type the formula =C4-D4 (which calculates Average Box Office minus Average Budget = Average Profit). Then fill down from Cell D4 to get Average Profit for each genre.
STEP TWO:
Select the Pivot Table along with column D. Copy and go to cell F3 and click paste. Now, select the range of cells where your data is, from F3 to I20. Go to Sort & Filter and choose "Custom Sort". Make sure "My data has headers" is checked.
SORT BY - Average Profit
SORT ON - Values (leave as default, no need to change)
ORDER - Largest to smallest
You will then find all the genres arranged by Average Profit, which will help you answer the activity's requirement - "Which movie genre generates the most profit on average?"
Hope this helps. Feel free to ask for any clarifications. Thank you and keep going, guys!
P.S. I thought of adding a screenshot, but that might be considered a breach of Coursera Honor Code, as it would have the answer to the activity's requirement!

Dynamic GETPIVOTDATA formula to SUM with different date ranges per column

I have below an example pivot table with Countries as my columns, and Dates as my rows. The data for this pivot is based on A1:C13. Cells F1:G3 is what the user of this model can adjust. They can change the country and/or the date ranges.
What I am needing is a single formula to output the total of their selection. As my actual data set contains many countries, simply writing an nested IF statement won't suffice. The dataset also needs to be in pivot table - hence the GETPIVOTDATA requirement.
I've gotten about half of the way there using this formula here:
=SUM(GETPIVOTDATA("Sum of Value",$A$15,"Date",ROW(INDIRECT(F2&":"&F3)),"Country",F1:G1))
The problem here is that this formula is not dynamic for the differing date selections per country.
Can anyone assist?
You need a separate GETPIVOTDATA for each country:
=SUM(GETPIVOTDATA("Sum of Value",$A$15,"Date",ROW(INDIRECT(F2&":"&F3)),"Country",F1),GETPIVOTDATA("Sum of Value",$A$15,"Date",ROW(INDIRECT(G2&":"&G3)),"Country",G1))
The answer provided by #basic is the closest one for the original question, i.e. using GETPIVOTDATA to work out the total per given parameters.
If the calculation can be done on the original data table (Range A1:C13), then here is another solution to calculate the total.
=SUMPRODUCT((A2:A13=F1:G1)*(B2:B13>=F2:G2)*(B2:B13<=F3:G3)*(C2:C13))
In order to make the above calculation "dynamic", i.e. the calculation updates as the rows expands in the table, you can put the source data into a Structured Reference Table, give a Name to each column, such as "List_Ctry" for Column A, "List_Date" for Column B, and "List_Value" for Column C, then the range behind each name will update automatically when the source data changes. Using these names, the formula will be something like:
=SUMPRODUCT((List_Ctry=F1:G1)*(List_Date>=F2:G2)*(List_Date<=F3:G3)*List_Value)
You can also give names to the three parameters, being the range of country, range of start date, and range of end date. I will not illustrate further as if you understand the above concepts, you should be able to do this step yourself.
By using Name in the excel workbook, it will enable you to refer to the source data conveniently without the need to lay it out in your final report, so I do not understand what is the limitation here to stop you from calculating from the source data but have to work on the pivot table, as the calculations on pivot table are limited and not that intuitive.
Feel free to let me know if you have any questions :)

Finding the total sum based a few criteria

I am currently working with the following excel workbook:
Workbook 1
I wanted a feature where I can find the total number of doors that need fixing, but based on Brand and Model of the cars. For example in the workbook, there are 5 Honda Vios (Blue) that needs to fix 1 door, then 2 Honda Vios (Red) that needs to fix 2 doors. Therefore, is there any formulation (sumifs / countifs) that I can use so that I can easily find the total number of doors that need fixing?
In cell G2 add this formula:
=COUNTIF(E2:F2;"YES")*D2
And drag it down.
This will count the number of doors on this row.
Then create a new table with the unique items and add the following formula which will sum all blue Honda Vios doors.
=SUMPRODUCT((A:A=M2)*(B:B=N2)*(C:C=O2)*G:G)
If "ES" is included:
=(IF(ISNUMBER(FIND("ES",E2)),1,0)+IF(ISNUMBER(FIND("ES",F2)),1,0))*D2
Regarding pivot table.
First make the table a official table by clickin on a cell in the table and go to insert -> table
Then again, click inside the table and go to insert -> Pivot table.
(To make it easier I added the pivot table on the same sheet)
Now set up the pivot as following:
When you add new items to your table, the pivot table must be refreshed.
Click on pivot table and go to Analyse -> refresh

Excel: Pivot Table Filtering Using VBA/Slicer?

Gold star for whoever solves this! :)
I have a pivot table that has data from various attributes attached to unique ID numbers from an OLAP cube report. When I select ID# as a pivot field and all the corresponding attributes ie: Name,age,etc I end up getting about 10k ID's with 10k corresponding attribute values. I have a list of 3k ID#'s which I want to filter my report by. What is the best way to do this?
Simple example: Pivot table displays ID# 1,2,3,4,5 and corresponding attributes such as name,age,etc
I want to filter ID#'s from a column of values I have in another worksheet for example the column in other worksheet would have ID#'s 1,2,3 (Which in theory would omit values 4 and 5 from pivot)
(Keep in mind I am dealing with thousands of rows this example is made simple just for explanation)
I thought a VBA slicer combo might work, but I have no clue how to set up a slicer so when it is created it has only values selected from a column of values.
In my example VBA slicer will have ID#'s 1,2,3 selected upon creation or an easy way to select multiple ID#s from a list or something as I can't feasibly click through 3k values
Possible Solutions I Brainstormed
I was also thinking maybe just a VLOOKUP or something that will pull ID#'s against the column and also bring all corresponding attributes? The thing is I like the cube report being dynamic and updating with the slicer.
I then also thought to create a slicer based off of another pivot table which is composed simply of a header of ID#'s and lists ID# 1,2,3 I created in another worksheet and try and use that simple slicer against the OLAP cube, but the issue is the slicers have different connections and they can't overlap. :(
Any and all help is much appreciated!!! :)

Excel: Multiple Vlookups to pull in 1 data element

I have 2 separate sheets in Excel. On sheet 1, I am using the following if statement in column C (hours) to pull in a number in column D (hours) from sheet 2.
=IF(ISERROR(VLOOKUP(A2,Sheet2!A:D,1,FALSE)),"N/A",VLOOKUP(B2,Sheet2!A:D,4,FALSE))
The first vlookup is validating the project number on the second sheet, then the second vlookup is validating the team name and pulling in the hours from the second sheet.
The problem is that when I copy the formula down the column, the hours value repeats for the same team name. Does anyone have any suggestions?
Sheet 1
columns
Proj_Number Team Name Hours
123456 R&D 26
123456 Dev 50
123456 QA 10
777777 R&D 26
Sheet 2
Proj_Number Team Name Organization Hours
777777 R&D AUTO 26
123456 DEV AUTO 50
123456 QA AUTO 10
123456 R&D AUTO 20
You are asking VLOOKUP to do something that it cannot do. The second VLOOKUP in your IF formula:
VLOOKUP(B2,Sheet2!A:D,4,FALSE)
is being asked to return the first instance of 'Team Name' that it finds on sheet 2; which it is doing.
If you want to return the number of hours based on the 'Team Name' and 'Proj_Number', you need to do it a bit differently.
One way is to create another column on Sheet2, maybe called 'Proj_plus_Team', (inserted at column C location) that concatenates the values in 'Proj_Number' and 'Team Name'. Then you can search on this new column using the following formula:
VLOOKUP(A2 & B2,Sheet2!C:E,3,FALSE)
Also, when doing this, make sure that the data on Sheet2 is sorted alphabetically on the values in column 'Proj_plus_Team'. VLOOKUP requires that the search column be sorted alphabetically or inaccurate results may be returned.
Option 1 - using formulas
LOOKUP is designed to retrieve a single value, which is what it's doing. It loops through a data range, checking whether the value is bigger/smaller than the reference value, and retrieves the first transition point. In your case, it finds the first match and stops, not what you're after at all.
If you want to do this using formulas, you'll probably find SUMIF() and the newly added in 2007 SUMIFS() to be a much cleaner route. Also, side-note, in Excel 2007 Tables are your new best friend, they tidy the formulas for this kind of thing right up and look after things like expanding ranges.
Your example above, using tables, would be translated to:
=SUMIF(Table2[Team],Table1[[#This Row],[Team]],Table2[Hours])
where Table1 is the equivalent of your Sheet1, and Table2 maps to Sheet2
Of course the reason that this won't provide what you're after is that this is summarising by team only, whereas you want to apply 2 filters, so you'd move to SUMIFS() and end up with:
=SUMIFS(Table2[Hours],Table2[Proj_Number],Table1[[#This Row],[Proj_Number]],Table2[Team],Table1[[#This Row],[Team]])
The benefit is that your formula will always recalculate automatically, the downside is that you have to manually update your Table1.
Option 2 - pivot tables
Insert --> Pivot table will create your pivot table, set the range as appropriate.
Then drag Project Number into Row labels, drag Team name below it also in Row labels, and drag Hours into the Values box.
That'll give you a breakdown by project by team of how many hours. You can then mess around with the options to get it exactly how you want, by doing things like turning off the level 1 summaries. Those kind of options are best found by experimentation, they live in the PivotTable Design tab. The catch is the need to manually refresh the pivot table, the benefit is that when you add new projects they'll be taken care of easily.

Resources