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.
 
Great, Jerry.

Please check and post both the results you expect and those you receive.

I get these results

Type Jan Feb Mar Apr
Metal 100 300 500 500
Plastic 300 400 400 400


You have it working so the VBA is really for info. Fazza
Code:
Sub CreateRefreshableQueryTable()

  ' "sumproduct and unique fields" thread at
  ' http://www.mrexcel.com/forum/showthread.php?p=1483747

  'Code to create the query table.
  '
  'Only need be run once. After it is created, the table can be refreshed like a pivot
  'table - data, refresh when the active cell is within the table. Or the exclamation
  'point icon on the toolbar. (When updating be sure the named ranges are up to date
  'with any changes to the source data! This could be easily done with VBA, BTW.)

  'Assumes named ranges for the data tables, not dynamic.
  'Names are "MainData" and "MonthsList"
  'Referenced table header names are.
  ' In MainData - "Conduit", "Type", "Weight", "Added", "Removed"
  ' In MonthsList - "TheMonth"

  Dim strConn As String
  Dim strSQL As String
  Dim wksNew As Worksheet

  strConn = Join$(Array("ODBC;DSN=Excel Files;DBQ=", _
      ActiveWorkbook.FullName, ";DefaultDir=", ActiveWorkbook.Path, _
      ";DriverID=790;MaxBufferSize=2048;PageTimeout=5;"), vbNullString)

  strSQL = Join$(Array( _
      "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"), vbCr)
  
  Set wksNew = Worksheets.Add

  With wksNew
    With .QueryTables.Add(Connection:=strConn, Destination:=.Range("A1"), Sql:=strSQL)
      .Refresh BackgroundQuery:=False
    End With
  End With
  Set wksNew = Nothing
  
End Sub
 
Upvote 0

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Another question, can the result set be placed into the same spreadsheet the tables are in?

Yes, for sure. It is as you said - change the source data and then refresh the table. It is as simple as that. Can only suggest you try again...
 
Upvote 0
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?

Jerry,

There must be an error/misunderstanding in my interpretation of the logic/rules.

So, it will be a matter of understanding the correct rules and modifying the SQL to suit.

Can you please review the requirement, and the sample data and results, and even the SQL and results it generates, to understand where the misunderstanding has occured? I'll also start from the beginning to see if I can see where I've gone wrong.

regards, Fazza
 
Upvote 0
Jerry,

I don't understand the March metal result.

My understanding is to include original worksheet rows
row 3 ABC999 weight 100
row 5 ABC1001 weight 200
row 11 HIJ3000 weight 200

These total 500. Your first post has value 300 for March metal.

This sort of difference from your data is what I was referring to at the start of my first post, BTW.

Can you please check and advise?

Regards, Fazza
 
Upvote 0
You're right, I had been changing some of the data to experiment, and I thought I had it back the way it was originally, obviously not... Your query works great and is accounting for removal dates. It's also adding only the unique conduit values, in case there are duplicates, which is correct.

Thanks for your help, this is going to save me a lot of work and time. Now I just have to figure out how the SQL works in case I want to use it in other applications as well (I'm gonna need a lot of coffee).
 
Upvote 0

Forum statistics

Threads
1,223,927
Messages
6,175,439
Members
452,641
Latest member
Arcaila

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