sumproduct and unique fields

jerry12302

Active Member
Joined
Apr 18, 2005
Messages
456
Office Version
  1. 2010
Platform
  1. Windows
I have a sample table below to illustrate my problem, the numbers in red are what I'm looking for a formula to calculate, probably a sumproduct, although I'm open to any suggestions.

Weight through conduits are to be summed only once if they are repeated in any particular month. (DEF1000 is in building 2 and 5 in March, but its weight should only be summed once) I hope this makes sense.
systemsfile.xls
ABCDEFGHIJKL
1Weight(countconduitsonlyonceifduplicated)
2BuildingConduitTypeWeightAddedRemovedJan-07Feb-07Mar-07Apr-07
3BLDG1ABC999Metal100Dec-06Metal100300300100
4BLDG2DEF1000Plastic100Mar-07Jan-08Plastic300400400300
5BLDG3ABC1001Metal200Mar-07
6BLDG4ABC1002Plastic200Apr-06Detailforillustrationpurposes:
7BLDG5DEF1000Plastic100Sep-06Apr-07MetalABC999ABC999ABC999ABC999
8BLDG6ABC1000Plastic100Jun-07HIJ3000HIJ3000
9BLDG7HIJ3000Metal200Feb-07Mar-07
10BLDG8ABC5000Plastic100Feb-07PlasticABC1002ABC1002ABC1002ABC1002
11BLDG9HIJ3000Metal200Feb-07DEF1000DEF1000DEF1000
12ABC5000ABC5000ABC5000
Sheet3


Any suggestions would be appreciated.
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
So for PLASTIC'S in Feb-07 try something like


=SUMPRODUCT(--(COUNTIF(OFFSET(B2:B10,,,ROW(B2:B10)-ROW(B2)+1,1),B2:B10)=1),--(C2:C10="plastic"),--(TEXT(E2:E10,"mm/yy")="02/07"),D2:D10)


HTH
 
Upvote 0
Jerry,

I have a different answer from what you posted - so can you check the result is correct?

Approach: give the source data table "A2:F11" a defined name, I used "MainData". Add another table with header "TheMonth" and dates listed under it: 1 Jan 07, 1 Feb 07, 1 Mar 07m 1 Apr 07. Give this entire table (header & 4 data items) a defined name, I used MonthsList.

Save the file. Via menu data, import external data, new database query select Excel files, then browse & select your Excel file, follow the wizard, select at least one field from the tables, proceed until you get a choice to finish but don't finish yet. Select the option to go into MS Query. When that starts hit the SQL button and copy the SQL from below to fully replace what you see. Enter this (acknowledging any message you get and continue). You should see the result set within MS Query. Now hit the open door icon & put the results set into a worksheet. This is now a refreshable query table.

HTH, Fazza

Code:
TRANSFORM Sum(A.Weight)
SELECT A.Type
FROM (SELECT DISTINCT MD.Conduit, MD.Type, MD.Weight, ML.TheMonth
FROM MainData MD, MonthsList ML
WHERE MD.Added <= ML.TheMonth AND (MD.Removed Is Null OR MD.Removed>ML.TheMonth)) A
GROUP BY A.Type
PIVOT A.TheMonth
 
Upvote 0
PS The query table, or similar, database-type approach will be scalable. So, it will work quickly with 10 records or 60,000 records. A formulaic solution will struggle if you have large data sets though might be OK if you don't have much data.
 
Upvote 0
Thanks Face, I'll try that.

Thanks Fazza, I followed your steps and made it as far as "follow the wizard, select at least one field from the tables". By select I assume you mean select the fields and press > to get the fields in the window to the right labeled "Columns in your query". That worked, but when I hit Next I got the error message:

"The Query Wizard can not continue because it can not join the tables in your query. You must join the tables manually in Microsoft Query by dragging the fields to join between the tables."

I've never messed with this before, any suggestions? I'm stumped.
 
Upvote 0
"The Query Wizard can not continue because it can not join the tables in your query. You must join the tables manually in Microsoft Query by dragging the fields to join between the tables."

I've never messed with this before, any suggestions? I'm stumped.

Yes, Jerry. Per my earlier post, select at least one field from the tables, proceed until you get a choice to finish but don't finish yet. Select the option to go into MS Query. When that starts hit the SQL button and copy the SQL from below to fully replace what you see. Enter this (acknowledging any message you get and continue). You should see the result set within MS Query. Now hit the open door icon & put the results set into a worksheet. This is now a refreshable query table.

That is, just acknowledge the message you got and continue.
 
Upvote 0
Fazza, I finally stumbled into MSQuery, I hit the SQL button and replaced the code there with the code you gave me. Then I pressed OK and got this message:

"SQL query can't be represented graphically. Continue anyway?"

When I press OK I get the message:

"Could not add the table '(SELECT'."

Any ideas? Thanks for your help, sorry for being dense on this, this SQL stuff is all new to me.
 
Upvote 0
Hi, Jerry.

It is critical that you have your table names and field names EXACTLY the same as I have used. That is, one table called MainData (headers & data all in the named range, not a dynamic named range) with headers "Conduit", "Type", "Weight", "Added" & "Removed" and one table called "MonthsList" (headers & data all in the named range, not a dynamic named range) with header "TheMonth". If you have: different names; or spaces added in names; or table named ranges that do not correctly cover the headers & data - it won't work. (Of course, the SQL can be changed to suit different names if you like.)

Suggest you try again. If it fails again, please post the SQL you are using. So, copy & paste it into your thread as 'code' like below.

I'll separately post again - in the next hour or so - with some VBA to create the table.

regards, Fazza

Code:
TRANSFORM Sum(A.Weight)
SELECT A.Type
FROM (SELECT DISTINCT MD.Conduit, MD.Type, MD.Weight, ML.TheMonth
FROM MainData MD, MonthsList ML
WHERE MD.Added <= ML.TheMonth AND (MD.Removed Is Null OR MD.Removed>ML.TheMonth)) A
GROUP BY A.Type
PIVOT A.TheMonth
 
Upvote 0
Hey! I got it, boy, the devil is in the details. I placed the result set into the spreadsheet. The only problem I see is that it is not accounting for the removed field. Metal for March, for example, should be 200 less because 200 was removed in March. Is there a way to account for this?

Thank you so much for getting me this far.
 
Upvote 0
Another question, can the result set be placed into the same spreadsheet the tables are in? I tried that, and I tried changing some source data and refreshing the data, but the numbers didn't change. Am I doing it wrong?
 
Upvote 0

Forum statistics

Threads
1,223,924
Messages
6,175,417
Members
452,640
Latest member
steveridge

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