Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

sql server - how to subtract adjacent columns in an ssrs matrix

My SSRS MatrixI have an ssrs matrix which looks like the one below :

               Month(Columns)
   Product(Rows)  Sales(Data)

The output looks something like this :

                       June  July  August  Sept  Oct  
          ABC          34     34     23     22    67
          DEF          33     21     32     22    14

I want an output that looks like this :

                       June  July  June-July  Aug  July-Aug  Sept  Aug-Sept  Oct  Sept-Oct
              ABC        34   34      0        23     11      22      1       67     45
              DEF        33   21      12       32     11      22      10      14     8

I tried doing something like this :

                Month(Columns) Change
   Product(Rows)  Sales(Data)   Expression

The expression looks something like this :

=Sum(IIF(Fields!MONTH.Value=Fields!MONTH.Value,Fields!Products.Value,Nothing))-
Sum(IIF(Fields!MONTH.Value=Fields!MONTH.Value - 1,Fields!Products.Value,Nothing))

But it doesnt work . I want to see the output as shown above . Please let me know.



Hey Sam ,

With the solution you mentioned :

I see an output like this :

                       June   Garbage    July  July-June  Aug  Aug-Jul  
              ABC        34     xx        34      0        23     11    
              DEF        33     xx        21      12       32     11    

Is there a way we can remove the column with the garbage values ?

Hey Sam , I tried your code. Now I have a big white space all along the column. Is there a way I can hide the wide space too ? enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you are grouping your columns by month then you don't need to use the SumIif

You can use a expression such as =Sum(Fields!Products.Value) to get the sum of all products in that particular month. If you want to see the difference between the current month and the previous month then if you enter the below expression in a cell within the month column group...

=Iif(Previous(Fields!MONTH.Value) = Nothing, 0, 
Sum(Fields!Products.Value) - Previous(Sum(Fields!Products.Value)))

You need the null check in this instance as the first month will return nothing for previous.

If you have overlapping row and column row groups (which I believe you do) then you won't be able to use Previous as it isn't supported :-(


I think that the only solution is to use some custom code.

There is a link here

Public Shared previous as Integer
Public Shared current as Integer
  Public Shared Function GetCurrent(Item as Integer) as Integer
     previous=current
     current=Item
     return current
  End Function

  Public Shared Function GetPrevious()
     return previous
  End Function

Then your usage would be something like

=Code.GetCurrent(Sum(Fields!Products.Value)) - Code.GetPrevious()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...