Concatenate Rows (PowerPivot)

  • Hi,

    Today I'm facing a very complicated challenge , I have a connection trough PowerPivot to an Excel file and I'm trying to concatenate the rows into one single column , here is how my data looks :

    Sales Order Identifier..........Billing Document Identifier................Billing Document Calendar Date

    75210241...............................859889179......................................5/9/2013

    75210241...............................9647889180....................................5/11/2013

    81106197...............................9653785136...................................5/12/2013

    The code for the above is this one :

    SELECT DISTINCT ['Direct_VPA invoice numbers$'].[Sales Order Identifier],['Direct_VPA invoice numbers$'].[Billing Document Identifier],

    ['Direct_VPA invoice numbers$'].[Billing Document Calendar Date]

    FROM ['Direct_VPA invoice numbers$']

    Basically an SO might contain different Billing Documents , if that's the case ,I'll need to concatenate that SO into one single row

    I'm trying to change my data to look like this:

    Sales Order Identifier...........Billing Document Identifier..............Billing Document Calendar Date

    75210241......................... 859889179 , 9647889180..................5/9/2013, 5/11/2013

    81106197................................9653785136...................................5/12/2013

    So far I've tried some variations of the next code but I keep getting an error :

    SELECT DISTINCT

    ['Direct_VPA invoice numbers$'].[Sales Order Identifier]

    , STUFF((

    SELECT N', ' + CAST(['Direct_VPA invoice numbers$'].[Billing Document Identifier] AS VARCHAR(255))

    FROM ['Direct_VPA invoice numbers$'] f2

    WHERE f1.['Direct_VPA invoice numbers$'].[Sales Order Identifier] = f2.['Direct_VPA invoice numbers$'].[Sales Order Identifier]

    FOR XML PATH (''), TYPE), 1, 2, '') AS FileNameString

    FROM ['Direct_VPA invoice numbers$'] f1

    Error "IErrorInfo.GetDescription failed with E_FAIL(0x80004005)."

    Any light here would be much appreciated

  • Please see the following article for one pretty easy method.

    http://www.sqlservercentral.com/articles/comma+separated+list/71700/

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply