DCSIMG
Recursive Sum for a Group in a Matrix in SSRS - Ella Maschiach's BI Blog

Ella Maschiach's BI Blog

BI, Olap Technologies, Tools, System Analysis and Design

News

About Ella Maschiach

Business Inteligence

Recursive Sum for a Group in a Matrix in SSRS

Recursive Sum for a Group in a Matrix in SSRS

I wrote a post not too long ago about creating Sum for a Group with Recursion in SSRS. A reader of that post dropped me a line as she was having trouble recreating the same behavior in a matrix (the post itself had demonstrations in it only for a table). So I decided to look into how a recursive sum acts in a matrix, and boy what a learning experience that turned out to be...
I created a report with a matrix which connected to Adventure Works DW. I defined the employees as the group on the rows (the recursive group with the parent - child relationship), and the product line as the group on the columns. I added styling to the padding and headers in SSRS as I have demonstrated in previous posts.
Now, the thing was that if I used the simple recursive sum I used for my table, I would get the recursive sum for the parent employee, but it would be the same for all the product lines. Obviously, this was not what I was looking for.At this point, I would really like to thank Boris Kogan who introduced me to the InScope function in SSRS, and helped me play around with it. The function helps you see where you are in respect to a dataset, a group, or a data region. Using the InScope function in a matrix can help us manipulate the sum that’s created for each part of the matrix (the inner cells, the total for the columns, the total for the rows or the total for the matrix). In a brilliant post by Jorg Klein, you get a very elegant and powerful solution to how to break your matrix into all of its parts. Using Mr. Klein’s very smart expression, I wrote:

=Iif(InScope("matrix1_ProductLine"), 
   
Iif(InScope("matrix1_FullName"), 
      
sum(Fields!SalesAmount.Value), 
      
sum(Fields!SalesAmount.Value,"matrix1_ProductLine")),
     Iif(InScope("matrix1_FullName"), 
        
sum(Fields!SalesAmount.Value,"matrix1_FullName",recursive), 
         sum(Fields!SalesAmount.Value)))  

Which basically means that in the matrix the following happens (see picture with correlating numbers):


  1. In the cells, we get for each employee the sales he made for that product line. If he’s the boss, we still get only the sales he made (and not his sales with his workers sales)
  2. The subtotal for the row group is as usual for the scope of product line.
  3. The recursive sum appears in the subtotal of the column group for the employee.
  4. The total for the matrix is as usual (a total of the data in the dataset).
Which gives us:

   

But you might still ask “What about a recursive sum in the cells?” Well, I do admit my solution isn’t full… I did try to apply:
sum(Fields!SalesAmount.Value,"matrix1_FullName",recursive) 
For the sum in the cell but I have to admit it didn’t work. What I got was the total for each employee for all of his sales (including his children, if he’s a parent). That’s all his sales - not just for that product line. I even tried adding another iif clause where I defined that only level zero (of the parent) will get the recursive sum and the rest will get the usual sum. That also didn’t help.
Thing is, that the recursive sum actually needs to run on two levels of scope for it to appear correctly. We need the recursive sum for both groups we have in the matrix – both the employee and the product line, because we’re actually trying to sum the sales of an employee and all his workers for a specific product line (not just their sales in general). In the recursive sum though, you can only give one scope for it to run on – either the employee or the product line. You cannot define it on two scopes. So in the solution I’m proposing, you only see the recursive sum in the column group subtotal.
I would also add that though in my previous post about recursive sum I gave you the solution for Reporting Services 2005 and Reporting Services 2008, this time I’m giving you the solution just for SSRS 2005 because my VPC crashed, when my PC crashed…. :(

I’ll try to get you the solution for SSRS 2008 in an upcoming post.
In the meanwhile, I’m attaching my report for your viewing pleasure: Sum Recursive Matrix
I hope this helped you create the recursive sum you need for a group in your matrix.

Comments

wing said:

hi Ella:

  can you give a detailed sample to me?I am a fresher to the SSRS that used it to display the parent-child dimensions in matrix.this problem has already puzzled me for many days.thx.

from wing

# April 23, 2009 5:50 PM

Ella Maschiach said:

Hi Wing,

I have already attached the detailed file to the blog post (look at the last paragraph “Sum Recursive Matrix”).

Hope that helps,

Ella

# April 26, 2009 9:39 AM

Thato said:

Hi Ella

I am having difficulties understanding this InScope function. I tried copying your code above and using it for navigation on matrix totals but I'm not winning everytime I click on the total it randomly select a cell and pass it as a parameter which then give me incorrect info.

may you please give an example of how to do this

many thanks

# May 27, 2009 2:18 PM

Ella Maschiach said:

Hi Thato,

The formula is not meant for navigation but as an expression in a cell inside the matrix itself. Try to build an SSRS project in your BIDS and then download the RDL file I put at the end of the post, and attach it to the project.

Look at the file and that should explain it the best way.

All the best,

Ella

# May 27, 2009 3:48 PM

Sri said:

Exporting the report to Excel does not keep the visual hierarchy. Any ideas / suggestions to keep the + , -?

Thanks for this article.

# May 28, 2009 7:04 AM

Ella Maschiach said:

# May 31, 2009 2:02 PM

Ella Maschiach's BI Blog said:

I read a few months back a post by Robert Bruckner saying that the InScope function isn't relevant

# October 23, 2009 12:25 PM

Juan Ignacio said:

Hi Ella,

I'm having a problem with a Tablix in SSRS 2008.

I have:

On rows: 2 levels of hierarchy.

On columns: 2 levels of hierarchy.

Everything is with subtotals. It's something like this:

                 Year

                   M1     M2    M3   Totalyear

Category C1

    Subcategory S1

            S2

                S3

                C1Total

Category C2

  Subcategory   S1

                S2

                S3

                C2Total

Overall Total

What i want to do, is to calculate a percent over the parent group, i.e:

in (S1,M1), i want ((S1,M1) / (C1Total, M1))

the same way, what i want in (C1Total, M1) is to calculate ((C1Total, M1) / (Overall Total, M1)).

So far, i've been able to calculate percent over the parent group, but the problem is that it calculates it over the total of the tablix, so it gives me back the same value for each month.

I've been reading about this and it sais that you can't calculate an aggregation function with 2 scopes (category and month, in this case).

I need to find another way to calculate what i want.

The data comes from a MOLAP cube in analysis services, so an alternative is to precalculate it there, but i haven't been able to do it.

Any help is welcome.

Thx to anyone who answers me. (i'm sorry about my english, but is not my native languaje).

Juan Ignacio

# November 23, 2009 8:28 PM

Ella Maschiach said:

Hi Juan,

Indeed you are right and you cannot calculate an aggregation function with 2 scopes. If you’re willing to use a scope in MDX, you would need something along the lines of:

Create Member CurrentCube.[Measures].[Ratio S1M1 to C1M1]

As IIf

   (

      IsEmpty

      (

         [Measures].[Sales], [Category]

      ),

      Null,

      (

        [Measures].[Sales], [Subcategory]

      )

      /

(

      [Measures].[Sales], [Category]

)

   ),

Format_String = "Percent" ;  

Assuming your regular measure is called [Measures].[Sales Ratio] what you would need to do in MDX is also add a scope to replace the ratio calculated with the one you created yourself:

Scope

(

   [Subcategory].Members,

   [Date].[Fiscal].[Fiscal Months].Members,

   [Measures].[Sales Ratio]

) ;  

This = ([Measures].[Ratio S1M1 to C1M1]) ;

End Scope ;

Hope that helped,

Ella

# November 29, 2009 11:33 AM

Ravi said:

Hi,

I am able to create "A Recursive Hierarchy Group". My problem is the data appears in this way

- A         8000

   A1.1    5000

   A1.2    3000

- B         17000  

   B1.1    8000

   B1.2    9000

but I want the report to show the data in the below format

   A1.1    5000

   A1.2    3000

- A         8000

   B1.1    8000

   B1.2    9000

- B         17000  

Please let me know if this is possible in Recursive hiearacial Groups ?

Thanks

# December 1, 2009 11:32 AM

Ella Maschiach said:

Hi Ravi,

I regret to say that as far as I know that’s not possible.

All the best,

Ella

# December 8, 2009 9:06 AM

Ravi said:

Hi Ela,

Thanks for your answer,Please let me know if you come accross this.

Bye

Ravi

# December 10, 2009 4:12 PM

Juan Ignacio said:

Hi ella, thanks for your answer, it works just fine! :)

# December 22, 2009 10:03 PM

Ella Maschiach said:

Glad I could help :)

# December 23, 2009 9:10 AM

Ella Maschiach's BI Blog said:

It's been three years that me and the blog have been together (truthfully, the date was on the 15

# May 29, 2010 9:19 AM

Rohit Kumar said:

Hi,

i have a small query in SSRS, which is quite big for me right now. Actually i hav to calculate the Progressive total in a Table. The situration is like:

Intial(Opening Bal) : 2000

S.No   Cost   Progressive_Cost

1      100         2100 (Opening Bal +  Current Cost)

2      200         2300( Previous Progressive_Cost+cost)

3      300         2600(2300 + 300)

Kindly help me, How can i achieve that.I am using SSRS right now.I have also tried Previous() Function but thats not giving the desired Result

# August 20, 2010 1:28 PM

Ella Maschiach said:

Hi Rohit,

First of all the opening balance should be also part of your dimension. It should look like:

S.No   Cost   Progressive_Cost
0              2000       2000
1              100         2100
2              200         2300
3              300         2600

This way you can achieve progressive cost with the SSRS function of RunningValue. The function is located under Sommon Functions > Aggregate. For use of the function, please look at the following:
http://msdn.microsoft.com/en-us/library/ms159136.aspx
http://blog.stevienova.com/2007/07/02/ssrs-runningvalue-to-get-cumulative-totals/

All the best,
Ella

# August 23, 2010 4:01 PM

djaif said:

does the sum recursive work in the sort expression in rs 2005? I get error.

# October 20, 2010 4:21 PM

Ella Maschiach said:

Hi Djaif,

I don’t think that SSRS can compute the recursive subtotal (whose value is dependant per cell) and sort on it, all before rendering the report. If you want to sort on this expression, you’ll have to calculate all the values before hand in the dataset itself of the report.

All the best,

Ella

# October 24, 2010 4:11 PM

daniele said:

with rs2000 I have sortexpression on the group witch contained a sum with recursive parameter

IIF((Parameters!SortDir.Value="Desc" AND Parameters!Sort.Value="Revenue"),Sum(FIELDS!RevTotal.Value, "table3_CustomerGroup", Recursive),0)

So rs did all the job sorting in recursive way every level in the hierarchy :

Parent1 1000

Parent 2 1500

Parent 1-2 700

Enfant1 300

Enfant2 400

Parent2-2 800

Enfant1 200

Enfant2 600

But in rs 2005 this expression results error!! Without the parameter recursive there is no error but the sort does not work on parents

I tried to implement it using the interactive sort new feature in rs2005. There is no error for the sort expression with recursive parameter but the sort does not work properly.

Clients families are grouped sing a key CustomerId, ParentCustomerId.

# November 3, 2010 3:11 PM

Ella Maschiach said:

Thank you Daniele for the added information on Djaif's question!

# November 6, 2010 6:16 PM

daniele said:

no its not add information!!! I have exactly the same problem as Djaif. I dont know how to make it work using just features of RS2005!!!?

# November 8, 2010 5:03 PM

ida said:

hi,

thanks for your solution!

i have also 1 problem about sum over colums.

   column1    column2

    50          30

    100         60

sum col1: 150   sum col1:90

Grand Total = sum col1 + sum col2

i try to calculate this Grand total but RS 2008R2 don't want to summe the values. Please can you help me?

Ida  

# November 26, 2010 1:42 PM

Ella Maschiach said:

Daniele - as I have told Djaif before, I’m afraid I cannot assit you on the matter.

Ida – try creating another column which has an expression to sum column 1 + 2.

All the best,

Ella

# December 1, 2010 2:56 PM

Devi said:

Hi Ella,

Sorry if it is a repeat, from  the figure, What is the process or expression to add column “ “T  and Column “ “S”  Assuming it is matrix report and the columns are displayed based on the selection in the parameters. Say for example your param A (You selected “R,M,S,T”) every time your selection changes, but you always need to sum the last 2 columns.

Any suggestions are highly appreciated.

Thanks,

Devi

# January 12, 2011 2:30 AM

Ella Maschiach said:

Daniele and Djaif – please look at the following link:

technet.microsoft.com/.../dd255193.aspx

Follow the steps described for "To sort values in a group, including the details group, for a Tablix". You should plant your expression in the sort expression for the group and that should work well enough.

Devi – just to make sure, are you asking how to sum the last two columns, never mind which 2 columns were chosen by the user?

# January 15, 2011 11:47 PM

Subbu said:

Hi Ella,

I have an issue with SSRS 2008(not sure if its an issue). Our requirement is to have a recursive hierarchical report which i was able to crack through. However we also have a column grouping and when the recursive sum (rolling up sub total) is used, the values are being doubled.

            Month1    Month2    Month3

Level 1         3         2         1

Level 2        2         0         0

 Level 3       1         2         1

As per the above, I need to display hierarchical information month wise along with the subtotals rolling up. However recursive SUM double up the values.

Is there a way out to display accordingly. All the help is much appreciated. Thanks.

# November 9, 2011 4:26 PM

jennette said:

hi, thanks for the post. it really helped me a lot. just a quick question, i tried the solution below but it seems it's only showing 2 levels? I have a data that drill down upto 5 levels. please help. thanks

# January 11, 2012 6:40 PM

Ella Maschiach said:

Hi Jennette,

The report can drill just as deep as the Parent – Child (or any recursive hierarchy for that matter) can go. But you can only define one parent and one child. If what you have isn’t a recursive hierarchy but rather a basic hierarchy with 5 levels, use the usual report grouping.

All the best,

Ella

# January 12, 2012 5:25 PM

Nuno said:

Hi,

I'm new in the SSRS, and i'm having a problem creating a measure to do a apparently easy SUM with a condition!

Can someone help me?

What i have is:

[Dm Plano Contas].[Snc Cod].[1]

...

[Dm Plano Contas].[Snc Cod].[23]

[Dm Plano Contas].[Snc Cod].[231]

[Dm Plano Contas].[Snc Cod].[2311]

[Dm Plano Contas].[Snc Cod].[2312]

...

And what i want is to SUM all the childrens of the [Dm Plano Contas].[Snc Cod].[23] where the [Measures].[Saldo] is smaller than zero.

# April 19, 2012 2:24 PM

Ella Maschiach said:

Hi Nuno,

From the example it looks like what you want is to check that measure for all the descendants of that member as the key is either 3 or 4 letters long (which I guess means different levels). Using MDX functions you can write something along the lines of:

Sum(YTD(), iif(([Measures].[Saldo], [Dm Plano Contas].[Snc Cod].currentmember) <0, [Measures].[Saldo], 0)

Create that sum as a new measure in your cube and use that as a measure for the recursion in SSRS I described in the post.

I would show in the report with just the relevant members as I don't see the logic in this measure for any member that isn't under [Dm Plano Contas].[Snc Cod].[32].

Please also look into the children, descendants and members MDX functions if they're at all relevant for you.

Hope that helped,
Ella

# April 23, 2012 3:27 PM

sarvam said:

how to do the average of average in a matrix .

# October 4, 2012 1:23 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: