Can I use COUNTIF function with or in GETPIVOTDATA? - excel

I have a pivot table something like this...
Duration
Run#| Product A | Product B | Product C |
01 | 2:01:00 | | 2:45:00 |
02 | 2:02:00 | | 2:45:00 |
03 | | 2:04:00 | 2:45:00 |
04 | 2:03:00 | 2:02:00 | 2:45:00 |
...
SUM | 6:06:00 | 4:06:00 | 11:00:00 |
I would like to use use GETPIVOTDATA to find an average of the times in the pivot table (not the source data)
So, my averages would be...
Product A | Product B | Product C |
2:02:00 | 2:03:00 | 2:45:00 |
Can accomplish that with GETPIVOTDATA? If so, how?
If not, is there another function available?
I have already built the average in a 2nd table using the Sum and a "count" function but it uses table references (i.e., COUNT(B4:B49)) which are a bit clunky since my pivot table will grow over time as more "runs" are completed (past row 49 which is the row of the latest run).

Something like this will grow with the Table:
=AVERAGE(B4:INDEX(B:B,MATCH(1E+99,B:B)-1))
It basically finds the range starting in B4 to the last row with number minus one so it does not include the total.

Related

Counting Time within ranges in table

So I did look through a couple of existing answers, but they were all in some programing language (ie, RUBY, PHP, etc). I also managed to figure out a way to do what I want but some of my formulas felt either hard coded or verbose. So my question is, is there a cleaner way to write my formulas to achieve my goal.
What I am starting with is a simple line to calculate cost of electricity usage. It look like:
+─────────────+────────+───────+───────+─────────+──────────+───────────+───────────+────────+──────────+───────────+───────────+──────────+──────────────+─────────────+
| | | | | | KW used | | | | Rate | | | Usage | Delivery | Total Cost |
| Date | Start | Stop | KW/H | Season | On-Peak | Mid-Peak | Off-Peak | Total | On-Peak | Mid-Peak | Off-Peak | Cost | Cost | |
+─────────────+────────+───────+───────+─────────+──────────+───────────+───────────+────────+──────────+───────────+───────────+──────────+──────────────+─────────────+
| 2022/12/17 | 05:00 | 22:00 | 1.44 | Winter | 0 | 0 | 24.48 | 24.48 | 0.17 | 0.113 | 0.082 | 2.00736 | 1.141296768 | 3.15 |
+─────────────+────────+───────+───────+─────────+──────────+───────────+───────────+────────+──────────+───────────+───────────+──────────+──────────────+─────────────+
The first 4 columns are user entry fields and correspond to columns B through E. The remainder of the columns F through P are all formulas.
My seasonal time bands are contained in the following table
+─────────+──────────────+─────────────+───────────+──────────+──────────+──────────+
| Season | Start | Stop | Time | Start | Stop | Rate |
| | | | Period | (HH:mm) | (HH:mm) | ($/Kwh) |
+─────────+──────────────+─────────────+───────────+──────────+──────────+──────────+
| Summer | May 01 | October 31 | Off-Peak | 00:00 | 07:00 | 0.082 |
| | | | Mid-Peak | 07:00 | 11:00 | 0.113 |
| | | | On-Peak | 11:00 | 17:00 | 0.17 |
| | | | Mid-Peak | 17:00 | 19:00 | 0.113 |
| | | | Off-Peak | 19:00 | 24:00 | 0.082 |
+─────────+──────────────+─────────────+───────────+──────────+──────────+──────────+
| Winter | November 01 | April 30 | Off-Peak | 00:00 | 07:00 | 0.082 |
| | | | On-Peak | 07:00 | 11:00 | 0.17 |
| | | | Mid-Peak | 11:00 | 17:00 | 0.113 |
| | | | On-Peak | 17:00 | 19:00 | 0.17 |
| | | | Off-Peak | 19:00 | 24:00 | 0.082 |
+─────────+──────────────+─────────────+───────────+──────────+──────────+──────────+
Note: weekends and holidays are Off-Peak
To aid with formula reading (or so I thought) I used the following named ranges:
Season_1 =$B$75 ("Summer")
Season_1_Start =DATE(YEAR(TODAY()),5,1) ("May 01")
Season_1_End =DATE(YEAR(TODAY()),10,31) ("Oct 31")
Season_1_TimeRates =$E$75:$H$79 ("Off-Peak" to 0.82)
Season_2 =$B$80 ("Winter")
Season_2_Start =DATE(YEAR(TODAY()),11,1) ("Nov 01")
Season_2_End =DATE(YEAR(TODAY()),4,30) ("Apr 30")
Season_2_TimeRates =$E$80:$H$84 ("Off-Peak" to 0.82)
TimeRates =IF($F3="Summer",Season_1_TimeRates,Season_2_TimeRates)
Step 1
Determining the season in F4
I used the following formula in F4.
=IF(AND($B3>=DATE(YEAR($B3),MONTH(Season_1_Start),DAY(Season_1_Start)),$B3<=DATE(YEAR($B3),MONTH(Season_1_End),DAY(Season_1_End))),Season_1,Season_2)
It seamed reasonable and substituted the year of the actual entry for making date comparison. I took the approach of if its not within the range for summer, then by default it has to be winter. Part of me is thinking I should check the winter date check. If both date checks fail then toss up a check date statement or the like.
Step 2
Determine how many hours are in each category according to the table and place results in G through I. This is where things go a little ugly for me. After chasing my tail in circle for a little bit I said screw it and plunged in with some hard coding and got something that worked but looked rather ugly and lengthy to me
I started out by figuring out that each time range check had 6 possible outcomes. I boiled those outcomes down to 2 possible results: 0 or Formula.
SB = Start Before
SI = Start Inside
SA = Start After
FB = Finish Before
FI = Finish Inside
FA = Finish After
So the results were either 0 because the time range in C and D are outside of the Time range of the table. Or its some form of Finish - Start.
I couldn't figure out how to make it count the hours in the range using the title (ie On-Peak) because there were sometimes more than one entry depending on the season in F. So what I did as a working step was to create a formula that would count the hours for each of the 5 time ranges instead of the names of the time range:
To get the hours I used the following formulas for each row:
=IF(OR($D3<=INDEX(TimeRates,1,2),$C3>=INDEX(TimeRates,1,3)),0,IF($D3<=INDEX(TimeRates,1,3),$D3,INDEX(TimeRates,1,3))-IF($C3<=INDEX(TimeRates,1,2),INDEX(TimeRates,1,2),$C3))*24
=IF(OR($D3<=INDEX(TimeRates,2,2),$C3>=INDEX(TimeRates,2,3)),0,IF($D3<=INDEX(TimeRates,2,3),$D3,INDEX(TimeRates,2,3))-IF($C3<=INDEX(TimeRates,2,2),INDEX(TimeRates,2,2),$C3))*24
=IF(OR($D3<=INDEX(TimeRates,3,2),$C3>=INDEX(TimeRates,3,3)),0,IF($D3<=INDEX(TimeRates,3,3),$D3,INDEX(TimeRates,3,3))-IF($C3<=INDEX(TimeRates,3,2),INDEX(TimeRates,3,2),$C3))*24
=IF(OR($D3<=INDEX(TimeRates,4,2),$C3>=INDEX(TimeRates,4,3)),0,IF($D3<=INDEX(TimeRates,4,3),$D3,INDEX(TimeRates,4,3))-IF($C3<=INDEX(TimeRates,4,2),INDEX(TimeRates,4,2),$C3))*24
=IF(OR($D3<=INDEX(TimeRates,5,2),$C3>=INDEX(TimeRates,5,3)),0,IF($D3<=INDEX(TimeRates,5,3),$D3,INDEX(TimeRates,5,3))-IF($C3<=INDEX(TimeRates,5,2),INDEX(TimeRates,5,2),$C3))*24
I wound up hard coding the entries because ultimately these are going to wind up in the formula for G3 through I3. Otherwise I could have used the Range # column as index points.
So the next problem I had was adding ranges with the same name together. Could have used SUMIF if I was keeping the table, but in my head I could not as A) I was not keeping the table, B) This would eventually need to be in a row that could be copied down.
So I looked at what needed to be done for each season and it was not bad...IF the formula was short.
So basically need an IF statement looking for summer and follow the addition of appropriate seasonal ranges for each range type. Seems simple enough but it gave me the following formulas for G3 through I3:
=$E3*IF(OR(WEEKDAY($B3)={1,7}),0,IF($F3="Summer",IF(OR($D3<=INDEX(TimeRates,3,2),$C3>=INDEX(TimeRates,3,3)),0,IF($D3<=INDEX(TimeRates,3,3),$D3,INDEX(TimeRates,3,3))-IF($C3<=INDEX(TimeRates,3,2),INDEX(TimeRates,3,2),$C3)),IF(OR($D3<=INDEX(TimeRates,2,2),$C3>=INDEX(TimeRates,2,3)),0,IF($D3<=INDEX(TimeRates,2,3),$D3,INDEX(TimeRates,2,3))-IF($C3<=INDEX(TimeRates,2,2),INDEX(TimeRates,2,2),$C3))+IF(OR($D3<=INDEX(TimeRates,4,2),$C3>=INDEX(TimeRates,4,3)),0,IF($D3<=INDEX(TimeRates,4,3),$D3,INDEX(TimeRates,4,3))-IF($C3<=INDEX(TimeRates,4,2),INDEX(TimeRates,4,2),$C3))))*24
=$E3*IF(OR(WEEKDAY($B3)={1,7}),0,IF($F3="Summer",IF(OR($D3<=INDEX(TimeRates,2,2),$C3>=INDEX(TimeRates,2,3)),0,IF($D3<=INDEX(TimeRates,2,3),$D3,INDEX(TimeRates,2,3))-IF($C3<=INDEX(TimeRates,2,2),INDEX(TimeRates,2,2),$C3))+IF(OR($D3<=INDEX(TimeRates,4,2),$C3>=INDEX(TimeRates,4,3)),0,IF($D3<=INDEX(TimeRates,4,3),$D3,INDEX(TimeRates,4,3))-IF($C3<=INDEX(TimeRates,4,2),INDEX(TimeRates,4,2),$C3)),IF(OR($D3<=INDEX(TimeRates,3,2),$C3>=INDEX(TimeRates,3,3)),0,IF($D3<=INDEX(TimeRates,3,3),$D3,INDEX(TimeRates,3,3))-IF($C3<=INDEX(TimeRates,3,2),INDEX(TimeRates,3,2),$C3))))*24
=$E3*IF(OR(WEEKDAY($B3)={1,7}),$D3-$C3,IF(OR($D3<=INDEX(TimeRates,1,2),$C3>=INDEX(TimeRates,1,3)),0,IF($D3<=INDEX(TimeRates,1,3),$D3,INDEX(TimeRates,1,3))-IF($C3<=INDEX(TimeRates,1,2),INDEX(TimeRates,1,2),$C3))+IF(OR($D3<=INDEX(TimeRates,5,2),$C3>=INDEX(TimeRates,5,3)),0,IF($D3<=INDEX(TimeRates,5,3),$D3,INDEX(TimeRates,5,3))-IF($C3<=INDEX(TimeRates,5,2),INDEX(TimeRates,5,2),$C3)))*24
So the question is, is there way to use formulas that would allow for the tidying up of what is currently in place for cells G3 to I3 (last 3 formulas in this question) that can be copied downward?
UPDATE as requested
I am working on the table on the top, below it is my building block area for thoughts. You may recognize some of the shots above from it.
Using Excel 2013

Return the running total of a specific item # from a non-static list of multiple item #'s

I am trying to create a simplified logistics spreadsheet that has a static tab of items (titled 'Items available') and their available quantities that, for each item, displays the running total of that item in inventory by referencing another tab ('Released & added') of items and their continually added & subtracted quantities.
The 'Released & added' tab will be a dynamic one and will use positive integers when a part quantity is being added and negative integers when they are being reduced. A formula that counts the number of rows in the dynamic tab in order to determine the table set first would be preferable, but for my own purposes, setting lookup range to a static quantity such as 2000 would suffice.
I have tried using various formulas that utilize INDEX and MATCH, however, I cannot seem to find a solution that properly hashes the table array in the released & added tab and it looks to me as if the AGGREGATE function may be required to do so.
Sheet1:
| Part Code | Available quantity |
|-----------|--------------------|
| ABC123 | Should be 35 |
| XYZ321 | Should be 21 |
| EFG456 | Should be 25 |
Sheet2:
| Part Code | Released / Added |
|-----------|------------------|
| ABC123 | 55 |
| XYZ321 | 28 |
| ABC123 | -12 |
| EFG456 | 35 |
| EFG456 | -10 |
| XYZ321 | -7 |
| ABC123 | -8 |
Right, you seem to benefit from using SUMIF()
The formula used in B2 translates to:
=SUMIF(Sheet2!$A$2:$A$8,Sheet1!A2,Sheet2!$B$2:$B$8)

Generate a master table based on a unique identifier with columns for the data from each table/worksheet

I am currently working in Excel but I am willing to consider solutions in (free) database software. The data I have is organized by time period, one worksheet for each period. The worksheets all have the same column structure.
Here is an example of the data from the 2017-Q1 worksheet:
| Time Period | Product ID | Prod. Category ID | Product Scale |
| 2017 - Q1 | 0012345678 | 012345 | 3 |
| 2017 - Q1 | 0023456789 | 012345 | 1 |
| 2017 - Q1 | 0033333588 | 022235 | 3 |
| 2017 - Q1 | 0123333333 | 022235 | 1 |
Here is an example of the data from the 2017-Q2 worksheet:
| Time Period | Product ID | Prod. Category ID | Product Scale |
| 2017 - Q2 | 0012345678 | 012345 | 5 |
| 2017 - Q2 | 0033333588 | 022235 | 7 |
| 2017 - Q2 | 0123333333 | 025444 | 5 |
| 2017 - Q2 | 0145555578 | 025444 | 1 |
The things that I am particularly focused on are:
Product ID 0023456789 does not appear in 2017-Q2, so the corresponding column in the master worksheet should show something (it could be blank, it could be NULL, it could be N/A, it doesn't matter as long as it is always the same.) The same thing needs to happen when a Product ID first appears in a later time period, the time periods before that should have the same N/A (or NULL, or blank, or whatever) This means that in order to form the master worksheet, I need to make a consolidated list of all of the Product IDs from all of the time periods, since not all of the Product IDs are present in every time period.
Product ID 0123333333 changed Prod. Category ID, so I need to always pull at least the Prod. Category ID column and the Product Scale column for each time period, just to be sure if it has changed or not.
I want to generate a master worksheet as follows (abbreviating column names just for space constraints here in this website):
| Product ID | 2017-Q1PCID | 2017-Q1 PS | 2017-Q2 PCID | 2017-Q2 PS |
| 0012345678 | 012345 | 3 | 012345 | 5 |
| 0023456789 | 012345 | 1 | N/A | N/A |
| 0033333588 | 022235 | 3 | 022235 | 7 |
| 0123333333 | 022235 | 1 | 025444 | 5 |
| 0145555578 | N/A | N/A | 025444 | 1 |
I have 12 different time period worksheets, so the master worksheet would need to first make a master list of unique Product IDs and then have 24 additional columns, because there are 2 columns for each time period (one column for the Product Category ID and another for the Product Scale ID, for that time period.) Furthermore, I should mention that each time period worksheet has 8,000-12,000 Product ID records, so ideally the solution should be able to handle thousands of rows fairly quickly.
TLDR : use index() match() .
Assuming the table 1 is located at "Sheet1" and the "Product ID" in the last table is located in cell A1, put this in B2 :
=IFERROR(INDEX('2017-Q1'!C:C,MATCH(A2,'2017-Q1'!B:B,0)),"")
and in C2 :
=IFERROR(INDEX('2017-Q1'!D:D,MATCH(A2,'2017-Q1'!B:B,0)),"")
and drag downwards.
Idea : 'load' the answer if the Product ID is matched in the source sheet, and put blank if no Product ID match is found.
Hope it helps. (:

Excel Pivot Advanced How to incorporate multiple columns

I have a table that looks like this -
Key | X | Y | Z
| Date | Date | Date
1a | 01-Jan-16 | 15-Jan-16 |
2a | | 01-Feb-16 |
3a | | | 01-Mar-16
I need to do a pivot on this table, to get a resulting table that looks like this -
Key | Type | Date
1a | X | 01-Jan-16
1a | Y | 15-Jan-16
2a | Y | 01-Feb-16
3a | Z | 01-Mar-16
I've tried multiple versions of Pivot, but they don't get me all the data within these 3 columns. What might the best solution be?
Delete the row with Date. Insert copies of your Key column immediately to the left of your Y and Z columns. Add two adjacent columns at a time using Multiple consolidation ranges, drill-down on the Grand Total intercept and filter the Value column to delete rows with blank cells. Label as appropriate.

DAX Cumulative Total With Date Filters

I am trying to calculate a running total where orders are only valid during a certain date range. Each order has a value, a start date and an end date. I want to calculate the cumulative sum of the order's values only during the dates between an order's start date and end date.
I've read over this article on cumulative totals and have an equation for the running total but I can't figure out how to filter the equation so that it filter's out an order once the date table is past the order's End Date. The current measure I have is Cumulative Value:=CALCULATE(SUM(Orders[Vaue]), FILTER(ALL('Date'), [Date] <= MAX([Date]))) and I want to add a filter that filters out any orders with an end date past the current date row, similar to this Filter('Order', 'Orders'[Order_End_Date] < 'Date'[Date]). When I try to add this filter though I get an error since 'Date'[Date] is not used in any aggregation.
Below is the data model that I am using and a link to the Excel File with the data model.
The sample Data:
+-----------+
| Date |
+-----------+
| 1/1/2015 |
| 1/2/2015 |
| 1/3/2015 |
| 1/4/2015 |
| 1/5/2015 |
| 1/6/2015 |
| 1/7/2015 |
| 1/8/2015 |
| 1/9/2015 |
| 1/10/2015 |
+-----------+
+----------+------+------------------+----------------+
| Order_Id | Vaue | Order_Start_Date | Order_End_Date |
+----------+------+------------------+----------------+
| 1 | 1 | 1/1/2015 | 1/3/2015 |
| 2 | 3 | 1/2/2015 | |
| 3 | 6 | 1/3/2015 | 1/7/2015 |
| 4 | 7 | 1/5/2015 | |
+----------+------+------------------+----------------+
And the output of the current measure I have and what the correct measure's output should be.
+-----------+-----------------+--------------------------+
| Date | Current Measure | Desired Measure's Output |
+-----------+-----------------+--------------------------+
| 1/1/2015 | 1 | 1 |
| 1/2/2015 | 4 | 4 |
| 1/3/2015 | 10 | 9 |
| 1/4/2015 | 10 | 9 |
| 1/5/2015 | 17 | 16 |
| 1/6/2015 | 17 | 16 |
| 1/7/2015 | 17 | 10 |
| 1/8/2015 | 17 | 10 |
| 1/9/2015 | 17 | 10 |
| 1/10/2015 | 17 | 10 |
+-----------+-----------------+--------------------------+
Cumulative Value2:=CALCULATE(
SUM(Orders[Vaue])
,FILTER(
VALUES(Orders[Order_Start_Date])
,Orders[Order_Start_Date] <= MAX('Date'[Date])
)
,FILTER(
VALUES(Orders[Order_End_Date])
,ISBLANK(Orders[Order_End_Date])
|| Orders[Order_End_Date] >= MAX('Date'[Date])
)
)
Model Diagram (note I took out your date relation - for the limited use case you've provided, it only makes things more complicated):
Note: I will refer to function arguments positionally, with the first argument represented by (1).
So, what we're doing is similar to what you were trying. We've got two FILTER()s, each as an argument to our CALCULATE(). CALCULATE() combines its arguments (2)-(n) in a logical and.
The first FILTER() does essentially what you were already doing, except we are filtering the distinct values of the [Order_Start_Date], comparing them against the current filter context of the pivot table.
The second FILTER() loops over the distinct values of [Order_End_Date], checking two conditions combined in a logical or. We must handle the case of a BLANK [Order_End_Date]. This BLANK is normally implicitly converted to 0 == 1899-12-30, which is less than any date we're considering. In the case of a BLANK, we get a true value from ISBLANK() and the row is returned as a part of FILTER()'s resultset. The other test is simply checking that [Order_End_Date] is greater than the current filter context date in the pivot.
What you are looking for is often called the "event in progress" problem. Here are some posts that will help you to solve your problem.
a solid summary of the problem
a special case
guess this will help on first sight
if you can't get enough - read the complete white paper
I hope this helps.
-Tom

Resources