Can I create a pivot table from two separate worksheets?

scottbass

New Member
Joined
Sep 3, 2012
Messages
46
Hi,

Say I have these sheets:

Claims:

CaseIdCustomerIDClaimNumClaimLineAmountApplicable *
1A11100Y
1A12100N
1A13100Y
1A21100N
1A22100Y
2B11200Y
2B21200Y
2B22200N

<tbody>
</tbody>

Payments:

CaseIdCustomerIDRemitterDateCategory1Category2Category3Total
1AJohn15-Apr-11$50$50
1AMary20-Apr-11$50$50
1ABob30-Apr-11$50$50$100
2BScott01-Jul-13$40$20$10$70

<tbody>
</tbody>

Money:

What I want in this sheet is a complex pivot table, or perhaps a complex VLOOKUP or array formula (???), with the data from these two separate worksheets. I'm not sure if I can do this in Excel or not? If I can do it in Excel, it would be cool as the end user gets immediate feedback/recalculations.

Here is what I need:

CaseIdCustomerNumberTotalAmountApplicableAmountAgreedAmount **WriteOffPaidAmountOutstandingAmount
1A$500$300$200$100$200$0
2B$600$400$200$200$70$130

<tbody>
</tbody>

Definitions:

TotalAmount: The total of all claim lines in the Claims worksheet for a given CaseId
ApplicableAmount: The total of all claim lines in Claims for a given CaseId where Applicable = "Y". * Note the Applicable column is hand coded based on an analyst's investigation.
AgreedAmount: The amount that is agreed is owed based on legal discussions. ** Note that this column is hand coded by the analyst based on those legal discussions.
WriteOff: The difference between the ApplicableAmount and the AgreedAmount.
PaidAmount: The total of all payments for a given CaseId, where the Total column is the sum of payments in three different categories.
OutstandingAmount: The difference between the AgreedAmount and the PaidAmount.

I think (???) I could get close to this with a Pivot table for an individual worksheet. But, I'm unsure if a Pivot table can be created to combine the data from these two worksheets as shown above?

If you KNOW this can't be done, can you let me know asap so I can start working on other alternatives? Which will be to export the Excel data to a database, run a summary program on the two separate tables, join the data, and re-export back into Excel. The downside is this will not give the end user immediate feedback, as this ETL process would run overnight. Also, the derivations would need to be done by the summary process, i.e. there could be no formulas in this worksheet, which would be a bummer.

Thanks for any help you can provide. Much appreciated.

Regards,
Scott
 
This is the final query I want to execute:


Code:
SELECT
a.caseid As CaseId,
a.analyst As Analyst,
a.lastname As LastName,
a.firstname As FirstName,
a.totalamt As Total,
b.applicamt As Applicable,
c.agreedamt As Agreed,
(b.applicamt - c.agreedamt) As WriteOff,
d.paidamt As Paid,
(c.agreedamt = d.paidamt) As Outstanding
FROM
(
(
(SELECT caseid, analyst, lastname, firstname, SUM(BenefitPaid) As totalamt FROM [Claims$] GROUP BY caseid, analyst, lastname, firstname) a
LEFT JOIN
(SELECT caseid, SUM(BenefitPaid) As applicamt FROM [Claims$] WHERE Applicable='Y' GROUP BY caseid) b
ON
a.caseid=b.caseid
)
LEFT JOIN
(SELECT caseid, SUM(AgreedAmount) As agreedamt FROM [Money$] GROUP BY caseid) c
ON
a.caseid=c.caseid
)
LEFT JOIN
(SELECT caseid, SUM(Total) As paidamt FROM [Payments$] GROUP BY caseid) d
ON
a.caseid=d.caseid


This was working for me once, but I breathed on it wrong and am now getting the error "Could not add the table "(" ". Well, thanks MS for the helpful error message.


THIS IS VALID SQL! Perhaps there is a data issue on this new workbook (I cut and pasted my real data to this test workbook), but the error message sucks.


Re: your last post...I'm now trying to create the queries "manually" via the query wizard (ALT-D-D-N), then use VBA to refresh. This will require much less VBA than the ADO/recordset approach.

OK, payments are entered directly in the spreadsheet. I had expected like the Claims data it would come from another application/database.
Nope, cheques in the mail :)

So doing the two steps so that the AgreedAmount is simply retained without the mucking around required with an alternative approach. I still am unsure if you're clear on that approach?
Nope, not clear, although essentially I think it means breaking up the query into multiple parts. While breaking up the query into two parts may be the way to go, my 3 table (4 alias) outer join is valid SQL and should work. However, given the lack of standard SQL support in MS query, perhaps that is the best approach. It appears that anything past two LEFT JOINS is "cumbersome", requiring parentheses not needed in ANSI-compliant SQL.

Though I see you have the same result as you describe Money sheet refreshes have blanks in the AgreedAmount for new records and other values stay there since 'the Money worksheet itself forms part of the query'. It sounds like you're doing the refresh a little like I'm thinking except with a recordset to re-generate the entire Money data each time, hence you query Money when making that recordset. Whereas I am keeping the exisiting Money data and using the extra scratch table to retain the pre-existing AgreedAmount values.
Nope...I Googled "Excel Query Table", and got that hit from Microsoft with the ADO/recordset approach (not a query table). I'm now trying to get the query wizard to embed a query table (or two using your approach) onto a scratch worksheet, then refresh Money. Ideally, Money would be the table output from the second embedded query table, rather than copying cells from the scratch workbook (this is different from what I previously stated).

1) Using the query wizard you first create a data source. I've never done that, and I've been doing these queries for ~10 years. ALT-D-D-N and then Excel (or whatever) files & follow wizard.
When I say "query wizard" that's what I mean...ALT-D-D-N, first define a data source, then define the query. For my query, I pick any table (worksheet), edit the query manually, click the SQL icon, then paste in my query. And it appears that I could create many queries from the single data source (which is pointing to my workbook).

2) I just make each query from scratch, and like I just wrote, I don't use data sources. Here are three basic ways to do these things: one, create them manually (no VBA) once, refresh manually as required; two, create them manually & use VBA to refresh (probably just the connection once you ensure there are no hard coded file paths in the SQL. Easy enough too to just put the SQL in each time); three, create them via VBA (to be sure they are what you are expecting, & not changed by users) & refresh using VBA.
Sorry for being dense...using ALT-D-D-N, the first thing you do is create the data source ???!!! But perhaps when you say "...I don't use data sources", you mean you override those settings via VBA? And yes, I'm looking to do #2: create them manually and use VBA to refresh.

Note the code I gave last time to debug.print the .connection and .sql is for Excel 2003 & earlier. For later versions the query table object reference has changed.
Yep, Googling now. I have to use a list object...This bit I should be able to get via Google.

A beer'd be nice. Thanks, Scott. If you're over in Perth I can shout you a couple
I spent about a year in Perth on a consulting gig (not Excel lol). Lived in Subiaco near Lake Monger. And I'll do the shouting (at least the first three!)
(y)
 
Upvote 0

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
In the SQL near the end of the select, is : (c.agreedamt = d.paidamt) as Outstanding

You may want that. Or you may want : (c.agreedamt - d.paidamt) as Outstanding

This step about defining a data source. Well yes I do use a data source but not the ones that you define & save via a long-winded process. Just select Excel files and continue.

When you have some time, these may be of interest

Querying External Data in Excel
which leads to Querying External Data in Excel

Daily Dose of Excel » Blog Archive » Create a QueryTable from an Excel Source in VBA

The image at the bottom of page 2 selects data sources - this is the screen I never seem to see.
http://fleet-maintenance.com/public_downloads/EXCEL_DATA_FROM_SQLSERVER.pdf

maybe it is just because I select Excel files at an earlier choice, not <new data="" source=""> like you see (not used) at
Daily Dose of Excel » Blog Archive » Parameters in Excel external data queries

this looks a really long way to get there - not what I do
Import External Data</new>
 
Upvote 0

Forum statistics

Threads
1,214,909
Messages
6,122,189
Members
449,072
Latest member
DW Draft

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