Consulting

Results 1 to 5 of 5

Thread: Solved: Pivot Tables from External Databases - Average formula incorrect

  1. #1
    Administrator
    2nd VP-Knowledge Base
    VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location

    Solved: Pivot Tables from External Databases - Average formula incorrect

    Hey guys,

    I have created a pivot table using the database at my job as an external reference (SQL Server 2000 database). I edited the query in MS Query to add fields that (to my knowledge) cannot be obtained otherwise. The specific field I'm having a problem with used the DateDIFF function in MS Query to display the amount of days between two dates. The formula itself works fine in the SQL statement, what I'm having a problem with is the Average formula that the Pivot table uses in the Data Items section of the pivot table of those days.

    I've provided a workbook that is an example of what my pivot table kinda looks like (using the worksheet's data). The difference is the 'Days Late' column is the field where I used the DateDIFF formula in my actual workbook.

    Here's a simplified version of what the SQL statement looks like:
    SELECT Tabel1.Item1, Table2.Item2, DateDIFF(d,Table1.Date1,Table1.Date2) AS 'Days Late'
    FROM MyDataBase.dbo.Table1 Table1, MyDataBase.dbo.Table2 Table2
    WHERE Table1.ItemID = Table2.ItemID AND (Table1.Date2>='1/1/2007' And Table1.Date2<='12/31/2007')
    The data returned that I'm concerned with is this:

    Days Late
    10
    0
    0

    The workbook I provided shows a correct use of the Average function (3.33 being the average), although my actual workbook says the average is 5.00...what gives?

    If I need to be more clear please say so. This is pretty new for me (pivot tables using external databases...and pivot tables too), so I'm not sure if I left out important information.

    Many thanks in advance,
    Joseph

    EDIT: Sorry, forgot the workbook. Here it is.




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  2. #2
    VBAX Mentor CBrine's Avatar
    Joined
    Jun 2004
    Location
    Toronto, Canada
    Posts
    387
    Location
    Malik,
    Sounds to me like you may be getting a Null in one of the return values? In access, I would surround the offending field with NZ() to eliminate it. I'm not sure if that will work in SQL, but if the Datediff works, then it might as well. Can't test without the data.

    nz(DateDIFF(d,Table1.Date1,Table1.Date2))

    Maybe?
    Cal
    The most difficult errors to resolve are the one's you know you didn't make.


  3. #3
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Cal,

    NZ(variable, return_value_if_null) function doesn't work (it is not a found function), but IsNull(variable, return_value_if_null) did work, but the number still turned out to be the same.

    I found the problem. After looking at the data in MS Query, I noticed that the item that was 10 days late was inputted twice in the database, with exactly the same information. So technically, the data would be:

    Item ----- Days Late
    Item1 ----- 10
    Item1 ----- 10
    Item2 ----- 0
    Item3 ----- 0

    Making the average correctly 5.00.

    It's funny because when you use the Average of Days Late in the Data area of the Pivot Table, it shows Item1 once, and the average of it ((10+10)/2)=10 when there is supposed to be unique Items in the database. Just a silly mistake I guess (from whoever entered the data twice).

    Thanks anyway Cal!




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  4. #4
    VBAX Mentor CBrine's Avatar
    Joined
    Jun 2004
    Location
    Toronto, Canada
    Posts
    387
    Location
    Malik,
    The surprise would have been if I actually was able to help you with anything that you weren't able to figure out on your own.

    :-)
    The most difficult errors to resolve are the one's you know you didn't make.


  5. #5
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location


    That wouldn't be a surprise to me




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •