• Is this what you are looking for? I didn't find any cube related information in your question, so I did this in straight SQL..

    DECLARE @Input TABLE

    (

    pn VARCHAR(10),

    prcode VARCHAR(10),

    dos DATE,

    Expected DECIMAL(10,2),

    Payment DECIMAL(10,2)

    )

    INSERT INTO @Input VALUES('MS0000003', 'PT001', '1/2/2013', 29.72, 0), ('MS0000003', 'PT001', '1/21/2013', 57.1, 0)

    ,('MS0000003', 'PT002', '6/7/2013', 26.69, 0),

    ('MS0000003', 'PT002', '6/7/2013', 89.16, 0),

    ('MS0000003', 'PT002', '6/6/2013', 57.1, 0),

    ('MS0000003', 'PT002', '6/7/2013', 12.28, 0),

    ('MS0000003', 'PT002', '6/7/2013', 26.69, 26.69),

    ('MS0000003', 'PT002', '6/7/2013', 89.16, 77.16),

    ('MS0000003', 'PT002', '6/17/2013', 57.1, 57.1)

    ;WITH CTE AS

    (

    SELECT pn, prcode, MAX(dos) AS Max_dos, MIN(dos) AS min_dos, SUM(expected) as expected, SUM(payment) AS payment

    FROM @Input

    GROUP BY pn, prcode

    )

    SELECT a.pn, a.prcode, Max_dos, DATEDIFF(d, min_dos, max_dos) AS differnce, a.Expected, a.Payment

    FROM CTE a

    Good Luck 🙂 .. Visit www.sqlsaga.com for more t-sql code snippets and BI related how to articles.