Search code examples
matrixreporting-servicesgroup-byssrs-2008subtraction

Subtract Group Columns in Matrix table in SSRS


I have a matrix table grouped by WBS and Acc at row level. Then i have a column group "Table" with Plan & Actuals. What i want to get is (Plan)-(Actuals)

I used the below expression in a outer group column

=SUM(IIF(Fields!Table.Value = "Plan", (Fields!Total.Value),0))-SUM(IIF(Fields!Table.Value = "Actuals", (Fields!Total.Value),0))

The total.value is the total of plan and actuals individually.

I am getting an error in the calculated column (Expression)

The Matrix TableThe Matrix Table

The Dataset and Outputs (expected and current)


Solution

  • If you only ever have two fixed values in your Table column then you should be able to do this without the need for custom code.

    Something like this...

    =SUM(IIF(Fields!Table.Value = "Plan", Fields!Total.Value,0))
     - 
     SUM(IIF(Fields!Table.Value = "Actuals", Fields!Total.Value,0))
    

    UPDATE for clarity

    I have created a report based on your sample data and the above works as expected.

    I created a dataset using the following query

    DECLARE @t TABLE (WBSNumbers varchar(20), [Table] varchar(20), Account Varchar(20), Total float)
    
    INSERT INTO @t 
    SELECT 'xxx', 'Plan', 'Capital', 96875 UNION ALL
    SELECT 'xxx', 'Plan', 'Expense', 40625 UNION ALL
    SELECT 'xxx', 'Actuals', 'Capital', 229949 UNION ALL
    SELECT 'xxx', 'Actuals', 'Expense', 2848 UNION ALL
    SELECT 'yyy', 'Actuals', 'Expense', 0 
    
    SELECT * FROM @t
    

    I added a matrix with row groups for WBSNumbers and Account and a column group for Table.

    I added a new column outside the column group with the expression stated above

    I added an extra row for the headers but this was not really required - just to make it match the expected output more closely.

    The final design looks like this.

    enter image description here

    The final output looked like this...

    enter image description here

    Other than the fact that the sample data does not match the supplied expected output for ABS yyyy this works as expected.