Pivot Table Sum on Value Range

rudypoochris

New Member
Joined
Dec 27, 2013
Messages
3
Hi Guys,

This one is probably pretty tricky. I have a data management issue that I have been able to resolve using SUMIFs and a custom table, but not in pivot form. Since the data source is a SQL database, I would prefer to resolve the issue in pivot form so that future values chart correctly. The data is formatted as so:

JobDay StartDay EndSizeProject
11/1/20141/10/201410A
21/3/20143/12/201513B
31/6/20142/10/201450C
41/18/20142/10/201440A
51/7/20143/1/201435B
62/16/20144/2/201423C
71/29/20142/28/201470D
83/1/20143/18/20142E
91/4/20142/7/201412G
102/9/20143/9/2014300F

<colgroup><col><col><col><col><col></colgroup><tbody>
</tbody>

From that data I want to see the total size of projects active at the start of the week for the past year. This would essentially mean summing the size of the projects if the week start date lies between the day start and end. This is what I ended up with:

YearMonthWeekWeekday StartActive Size
2014111/1/1410
2014121/8/14120
2014131/15/14110
2014141/22/14150
2014151/29/14220
2014262/5/14220
2014272/12/14418
2014282/19/14441
2014292/26/14441
20143103/5/14338
20143113/12/1438
20143123/19/1436
20143133/26/1436
20144144/2/1436
20144154/9/1413
20144164/16/1413
20144174/23/1413
20144184/30/1413

<colgroup><col><col span="2"><col><col></colgroup><tbody>
</tbody>

This is the associated pivot chart:



This would be "OK" except that I would also like to see the size breakdown by project, like this:



This is where I would need the data to be handled purely in a pivot so that when a new project is added, the pivot table will automatically make the summing column for it rather than my manual creation of columns for the data.

I have uploaded a link to a work book with the examples of the data and how I made the charts above. You can see that to make the second chart I had to manually create the columns for each project:

Download PivotExample.xlsx from Sendspace.com - send big files the easy way

Thanks for the help.
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
hi & welcome

I didn't download the sample file, btw.

I started with the posted first table of data, and named it "ProjectData". Then the first four columns of the second table: named it "WeeksData". Saved the file.

From a new file, ALT-D-P to start the pivot table wizard, external data choice at the first step & followed the wizard. At the end took the option to edit in MS Query and changed the SQL to be as below. Proceed to end.

hth. regards

Code:
SELECT W.[Year], W.[Month], W.[Week], P.Size, P.Project
FROM ProjectData P, WeeksData W
WHERE P.[Day Start] <= W.[Weekday Start] AND P.[Day End] >= W.[Weekday Start]
 
Upvote 0
hi & welcome

I didn't download the sample file, btw.

I started with the posted first table of data, and named it "ProjectData". Then the first four columns of the second table: named it "WeeksData". Saved the file.

From a new file, ALT-D-P to start the pivot table wizard, external data choice at the first step & followed the wizard. At the end took the option to edit in MS Query and changed the SQL to be as below. Proceed to end.

hth. regards

Code:
SELECT W.[Year], W.[Month], W.[Week], P.Size, P.Project
FROM ProjectData P, WeeksData W
WHERE P.[Day Start] <= W.[Weekday Start] AND P.[Day End] >= W.[Weekday Start]

Hmm. That didn't work for me. I wasn't able to bring both files into the pivot table. Additionally the data set will be coming from a SQL connection so saving the data into an Excel file will not be a viable method of doing this. I can though link directly to the SQL database using the query I made to obtain the data in the first place. Unfortunately in the ALT D-P menu it doesn't seem that that is an option.

Do you have any alternate ways that would work with active SQL connections? I could also modify the query itself theorehtically (I know you can do some pivoting in SQL), but I am not sure how.

The ProjectData portion is from a SQL server, the WeeksData was just a blank table I built in Excel.
 
Upvote 0
OK. I had the data in one Excel file not two. If you were to use two files, include the full file path in addition to the table name. Something like `C:\path\filename.xlsx`.ProjectData

Another thought, if you have the data on two separate worksheets, instead of the defined name approach I originally described there might be benefits of using the alternative approach of referring to the sheet names (if the headers are in row 1 and the data immediately below). Syntax is then FROM `full file path`.[sheet name$]

However if the data is coming from a database, just connect straight to it: exactly how that is done for your database I can't advise. Suggest you google.

I'm only familiar with MS Access. If it proves difficult to connect the pivot table to the database suggest you use VBA and ADO and try http://www.connectionstrings.com/ for the connection string. If you have a recordset, then you can set the pivotcache's recordset to that same recordset and quickly create a pivot table: VBA help on recordsets refers, IIRC.

For the WeeksData table there are some choices. Maybe easiest to make/have that table in the database. Also the SQL I gave could be there too. Then the pivot table can simply refresh from that query, or be created on the fly. This would keep the info in the database and the Excel file would be only the pivot table. If having the table in the database doesn't suit, either make it (in Excel most likely) via VBA or, without VBA, have it in a file (probably the same Excel file as the pivot table would be simplest).

regards
 
Upvote 0

Forum statistics

Threads
1,216,322
Messages
6,130,046
Members
449,554
Latest member
andyspa

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top