Dynamic GETPIVOTDATA formula to SUM with different date ranges per column - excel

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 :)

Related

How to refer to an Excel table column by name when table changes over time

Short question: Is there a way to get a formula that references a column in an Excel table to always refer to only the column with that name? This is a straight-up formula question, not VBA-related.
Formula:
=COUNTIFS(Table35[[dc]:[dc]],$A6)
In this case column A has a data center name in it. Table 35 is the output of another program that produces data for an ETL and also most of the same data for this spreadsheet that is used for both analysis and status reporting.
Problem
Due to the nature of analysis, the shape of the data changes. It seems like about once a week there is another variable (column) that needs to be added to the ETL and analysis. I've simply been copying the program output, going to the upper left hand of the table and pasting. For all of my PivotTable analysis of this data it works exactly the way I want it to.
The problem happens to my dashboard built with various Countifs() formulas like the one above. My initial expectation of behavior is that by referencing the data (first by column name and now as a range of columns that contain only one column of data) that Countifs() would select the data based on column header value (much like using vlookup() + match() together).
Instead what happens is that when a column gets added the names in all of my formulas change. It appears the internal representation essentially says I want column #N. No matter what data happens to be in column N, that is what shows up. So for example, [dc] above turns into [CS Windows Error Message]
Is there any way to indicate that I want the column named "dc" regardless of where in the table dc is located? Even the great and omniscient Google has failed me on this one. Help appreciated.
Try wrapping the table reference into the Indirect function. That will preserve the column name and return the correct value.
=COUNTIFS(INDIRECT("Table1[dc]"),2)
or, with your formula
=COUNTIFS(INDIRECT("Table35[[dc]:[dc]]"),$A6)

Excel Dynamic Array Spill Area - can it go along a row or just down a column?

The point of this exercise is to try to make this analysis quicker/easier to update. I had it pretty dialed in using a data tab and a pivot table, copying and pasting values/formulas as needed to update. I am trying to improve on that by using Excel's new Dynamic Arrays.
My table is setup with four employee detail columns (Location Name, Location Number, Employee Name, Employee Number), and several (many) columns for dates worked. My issue is the dates worked.
I am using UNIQUE and FILTER to get a list of flagged employees - works as I intended/hoped; lookup formulas to find the location name and number data for each employee from the data table - works as I intended/hoped; and I have been copying/pasting the dates from the pivot table column headers into my "counting"/analysis table header.
What I would like to do is use the UNIQUE formula to get the dates worked - BUT, I want the spill area to go along the row so the dates are column headers. Okay, I probably could have made that a shorter ask, but I hope explaining a little of what I'm trying to do helps. I want the dates to automatically update when I paste new payroll/hours worked data into the data table, the same way the employee data updates.
The key is to TRANSPOSE the UNIQUE formula, not the data:
=TRANSPOSE(UNIQUE(A1:A9))
I cannot find anything about having the spill area go along a row instead of down a column. My workaround for this was to create a column of unique dates next to my data table. Since the data is in a table, the unique dates will update automatically when the table is updated. I used the Offset formula to pull the dates into my column headers from the column of Unique dates next to my data table. I will say, while this setup is easier than updating via the pivot table, it is a formula heavy workbook now.
For the data headers I used : "=OFFSET('Data Table Tab'!$U1,Column()-5,0)". The "rows to offset" portion of the formula is "Column()-5". My first date header is in column 5, so as it is copied to the right this will increment the number of rows to offset from the first date.
At this point I will just need to copy the formulas over to the right to update my analysis table.
I would be surprised if someone else out there just starting to work with the new Dynamic Array formulas in Excel doesn't wonder about the same thing. If you stumble across this question and have a better solution, please feel free to share it with me.

Excel: transpose rows to columns and merge

I would like to present some data in Excel in a table. I have a sheet with three columns: date, time_spent and user_id. Every row is a data record. I want a table with a column for every date and every value with time spent below the date.
Using the transpose function under paste special I get this:
However I would like only one column per unique date, so the amount of columns decrease like this:
Does someone know how to do this?\
I have more than 10000 rows, so manually is too much effort...
Using Pivot Tables as Tim Biegeleisen suggested I cannot get what I want either, I'm not an expierenced user of Pivot Tables so probably I'm doing something wrong:
Excel file as shown in above picture
Here is one way of using a Pivot Table as suggested by Tim above in the comments, but you must use it on your original, non-transposed data: (and note which parameters go to which areas.
A disadvantage of the Pivot table is that the column headers are text strings and not dates. If your version of Excel is 2010+, you can use Power Pivot or Get and Transform to do a better job:
Open the Query Editor, change the format of the Date column, and Pivot the column:
Then Save the results:
Advanced filter dates to unique values in a new column
Copy and transpose them to make your column titles
Insert a new column B with the formula =TEXT(A2,"yyyyddmmm")&COUNTIF($A$2:A2,A2) and fill down
Below your new column headers put in the formula =IFERROR(VLOOKUP(TEXT(H$1,"yyyyddmmm")&ROW()-1,$B$2:$C$8,2,FALSE),"") and fill down and across. You will need to fill it down to cover the maximum number of instances of each date.
Adding a sumif to this table and your initial table would be a good way to check you got everything.

Formula to reference data from a PivotTable

I am having some difficulty writing a specific Excel formula. I have a summary sheet that pulls data from various other sheets extracted from a database. One of these is a PivotTable whereby using the Item Number in the first column and the dates along the top row as a reference I can pinpoint the data I need. eg:
To address the highlighted cell I would normally manually write:
=GETPIVOTDATA(HighPiv,"SPN010977-204 11333")
HighPiv is the name I gave to the pivot table as I am referring to it from my summary sheet.
This works, however the Week numbers along the top will continuously be changing in the pivot every month and therefore this formula will not pick up the values accurately once the pivot is updated. I have been looking into a way to make the referencing more dynamic. This is the summary where the data is required:
Rather than within the quotation marks of the formula (adding the specific Item number and Week number word for word), I was hoping to refer to the cell references of the summary sheet. (So if I wanted Item number, say A55, and Week number, say H50). The dates in the summary sheet change according to the pivot so referring to the dates on the summaries to get the data would be a better way for it to be kept up-to-date.
The problem here is I don't know how to go about it. I have tired to refer to the cells in question but it doesn't seem to work giving me #REF! or #VALUE! errors.
I think what you would like is:
=GETPIVOTDATA("Qty",HighPiv,"Item",A55,"Week",H50)
I find the easiest way to write such a formula is to start by ensuring that Pivot Table Tools > Options > PivotTable – Options, Generate GetPivotData is checked then in the desired cell enter = and select the required entry from the PT (here63). That would show (for example) “SPN010977-204” and 11333 or ”11333” but these can be changed to A55 and H50.

Pivot table and manual correction in excel

I have a pivot table, from a data source which contains false values for a specific date.
Some people in my department provided with the correction of the agregated numbers.
Do I have a way to insert a manual correction just for this cell point or is the only way to copy by value etc....?
I believe correcting the source data is your best option. Of course the best thing would be if they could give you the corrected individual data points.
If you can't get that, then I think you need to add a helper column to the source data that will contain corrected numbers that you calculate based on the ratio between the incorrect aggregate and the correct one:
In this example, to the right I've calculated the ratio between the incorrect and correct aggregates. Column C is the new helper column. It contains a formula with an IF statement that checks the date. IF it's the date with the problem it multiplies each individual amount by the Correction Ratio in G2. You can then change your pivot table to use Column B instead of Column C.
It's important to to note that Column C now contains incorrect data by row. So again, stating the obvious, the best thing would be if they give you the detail data!

Resources