Macro using Dynamic Pivot table values - excel

I get a lot of data every day with one of the columns being the name of stores. These stores are divided into two regions, say north and south. So , I have a superset with store names including both storesN and storesS. Everyday I get reports from a subset of these names. I use Pivot tables to display the data.
I want to write a macro which will show me just the data from storesN or storesS. If all the stores reported everyday, I could write a fixed macro. But every day the stores that report changes. So is there a way, I could write a macro, which will automatically detect and which names are present and adjust accordingly.
I am using
With PivotTables("PivotTable30").PivotFields("Name")
.PivotItems("Store1").Visible=False
The names are always a subset of the superset.
I hope the question is clear.

Just refresh your pivot table:
ActiveSheet.PivotTables("PivotTable30").PivotCache.Refresh
Good Luck.

If you are deleting all the old rows, just make sure that "Show items with no data" is turned off in the Field settings. Then refresh the table.
If you are only replacing the updated rows, then the easiest solution is probably to just add a "last updated" column and have that filtered for "today" (Or PivotFilters.add2 Type:=xlDateToday if you build your table via code)

Related

How do I lock an additional column to rows imported from Power Query in Excel 2016 without a unique key column?

I am using Power Query in Excel 2016 to combine data from 12 different workbooks within the same folder system into one table, and need to add an additional column in the master table that tracks the status of each row. However, when I refresh the data, the Status column does not follow the rows to which it is initially applied.
I have already looked at [ Inserting text manually in a custom column and should be visible on refresh of the report ] but this solution only works with a unique ID column. Because each of the 12 workbooks is edited separately and because there is no single column that can be guaranteed to have unique values between all of the different spreadsheets, I don't have a key to join the data to the additional column.
I believe there is always a way of finding a Unique ID. If you can get your head around this, it is not that difficult to solve your problem.
See my below example, I used three sample workbooks saved in a Test folder. Depends on the way you add them to the query editor, in my example I used From Folder and follow the prompts without making any changes and combined the tables automatically. Once combined there is a Source.Name column automatically added. I suggest to leave this column in your output table as it can form part of the Unique ID if your data is highly identical across the workbooks.
An optional step (not in my screenshot) is to add an Index column and concatenate the index number with a product/task name so it can make that specific line of data entry even more unique.
Once you added the Status column with data entered manually on the master table, load the master table back to query editor.
Then go back to the original query (Test (Input) in my example) and merge it with the reloaded output query. See my screen-shot for how to 'uniquely' merge the two tables.
The rest is self-explanatory. I think the key is finding elements of the Unique ID and incorporate it in the merge part.
Let me know if you have any questions. Cheers :)

Calculated Field

I am trying to create a simple pivot table which will tell me how many community residents reported a particular problem, and what percentage of them reported each problem type. I have a data set with name, and then columns for each type of problem. Here's an small sample of the data set:
I have created a pivot table which sums each of these columns and also provides me the total number of people who reported any type of problem at all. Here's what I have:
I want to add a second column to this pivot table that gives the percent of times each problem type was reported. Sounds simple, but because of the structure of the original data set, I can't figure out how to do it. I can set up formulas outside of the Pivot Table which reference the table, but in doing so I forfeit the ability to graph the percentages on a pivot chart. Any ideas how to create a calculated field for this pivot table?
Just to be clear, what I want is something like this, except all contained in the structure of the pivot table:
Edit: I've changed the example of the data set. Here's an explanation of the pivot table. The values under the "# Reporting Issue" column are counts of all the 1's under each corresponding column in the data set. This meant that I had to add each row to the pivot table independently, as you can see here:
I'm open to the idea that I need to change the formatting of the data set, but I'm not sure of the best way to do it. This was set up initially because it allowed for easy compilation into a data table, but Pivot Tables seem to be a different story.
Hopefully this edit clarifies things.
You need to unpivot your data so that you turn it into a Flat File...something that the PivotTable can consume properly.
The easiest way is to use something called PowerQuery, which is baked in to Excel 2016 but available as a free addin from Microsoft for any other versions. Google PowerQuery Unpivot and you will turn up hundreds of tutorials, such as this one from my good pal Chandoo . PowerQuery looks slightly daunting at first to a first time user, but it is freakin easy once you get your head around how to use it. PQ is by far the best addition to Excel in years. PowerPivot being a close second.
If you can't install PowerQuery, then you can use your current data structure to make a 'staging pivot', and then drag the Values label that will appear in the Columns area to the bottom of the ROWS pane, like in this excerpt from a book I'm writing:
Note that my Year categories are equivalent to your Issues categories.
That will emulate the flat file layout you’re after. All you need to do then is turn this intermediate PivotTable back into a normal range, change that Values heading to Issue, and add a Count heading and you’ve got the flat file you need to build a useable PivotTable.
You can also use VBA. Google Unpivot VBA and turn up hundreds of results, including this blazingly fast code I posted some time back. (Look for the code under the —Update 26 November 2013— heading.)
You can also use the DoubleClick extraction trick.

Excel 2010: Automatically combine multiple tables into one dataset

I thought there would be a simple way of doing this, but unfortunately I have not come across one. My company has an Excel workbook with 12 sheets (1 for each month), into which I enter sales data as accounts are written. I reformatted each month's data into tables, thinking that this would provide an easy reference to gather the data into a pivot table that joins all the months and would be updated as I enter data; however, a pivot table based on multiple sets of data allows highly limited manipulation.
So what I want to do is create a new table that is automatically populated as I enter data in any of the 12 current tables, to combine them into a master listing. I have tried doing a query, but when I try to set up the data sources, it doesn't recognize my tables. I tried Power Query, but I couldn't get it to update the data as I updated the source. Consolidate also was not a useful feature, as it required all the data to be somehow calculated, and my columns need to simply be copied over, not summed or averaged.
As you can probably tell from my explanations and terminology, I'm no Excel expert. I don't know what VBA even is, let alone know how to use it, but I've seen it mentioned a lot, so I figure at some point in my life I should learn it.
Is there a formula or some other Excel 2010 feature that can automatically copy all of this data onto one running list, and keep it updating as I enter data in the source tables? It would have to run automatically.
I believe your end goal is to have a pivot table which consolidates data from each of the individual 12 sheets/tables and not really to have the intermediate "single running list which is an aggregation of all the 12 sheets".
If so, I suggest to create an Excel Pivot table directly based upon the 'Multiple consolidation ranges'.
To start, create a new spreadsheet and select a cell (say A3) and use the click sequence Alt+D+P, this will bring up the PivotTable and PivotChart Wizard, and proceed further using the third option - 'Mulitple consolidation ranges'.
I will have to refer you to the below site for a detailed step by step instructions on the above: http://www.contextures.com/xlPivot08.html
Please be aware that the Difficulty level for this solution is Medium, suggest you to bookmark the solution from maintainability reasons, in case you choose to implement it.

How to add condition to pivot table showing part of or manipulated source data

I have an Excel sheet that has headers customers, number of cases, missed targets. Missed target tells how many out of all the cases has been missing target time.
I would like to create a pivot table and a chart of it showing the ratio of the missed target cases divided by all the cases related to a customer, basically telling you which the most problematic customers are.
You can create a calculated field which you will be able to use in your pivot table.
In the Pivot Table tools, you'll find an option called Fields, Items, & Sets. From there, choose Calculated Field. Give the field a name like "Ratio of Missed Target" and then enter the formula you require: ='missed targets' / 'number of cases' Click Add.
This is how it would look in Excel 2003 (the version I have access to at the moment)
Then you will be able to use this field in your Pivot Table just as any other field.

VBA to update multiple sheets from main spreadsheet based on department name

I have a spreadsheet that is used to track audit scores for 9 different departments in a plant. The 9 departments are broken down to 117 different area locations. Once a month I add a new column and update the scores. What I would like is a seperate sheet for each department with the audit scores updating automatically when I import the original data in. I'm new to VBA programming and this may be over my head a little bit, is there a way to do this?
Why not using a pivot table in each tab ?
You'll only have to use the "actualize" command once ( Data-->Refresh all) whenever you're adding a column.
The thing you have ot be careful with is to set-up you master table as a list (Home tab-->Format as a table command ) or else the pivot table will not take account of an added new column.
Without seeing the actual data and the format the it is currently in...
Assuming that you make a separate sheet for each department each with a row corresponding to its respective area...
I would utilize a class to hook the application's events and update each sheet on the Worksheet_Change event. Chip Pearson provides an excellent example of this concept here and I'm sure that a cursory search of SO will turn up quite a few examples as well.
From that point it is a matter of identifying the imported data and it's corresponding location in the workbook.
If you have an example I would be happy to look at it...

Resources