Showing posts with label SSRS Report Development and SQL. Show all posts
Showing posts with label SSRS Report Development and SQL. Show all posts

Thursday, August 7, 2014

Summing Percentages

Here is another write-up on summing percentages. Example 1 is the only example that produces a valid result. Unless the sum of all the numerators is being used as the denominator for all the percentages to be summed it is necessary to calculate group percentages the same way as the individual percentages are being calculated using the sum of the numerators divided by the sum of the denominators. There are additional issues calculating percentages from reports with options to filter the data as then the calculations need to be based on the filtered data rendering any data summed in the view useless.

Example 1: Using the sum of all the numerators as the denominator for all the percentages to be summed yields a valid result.
Numerator Numerator (Running Total) Denominator Denominator (Running Total) Percentage Running Sum of % Running Sum of % divided by the number of percentages summed Running Percentage
1 1 210 210 0.48% 0.48% 0.48% 0.48%
2 3 210 420 0.95% 1.43% 0.71% 0.71%
3 6 210 630 1.43% 2.86% 0.95% 0.95%
4 10 210 840 1.90% 4.76% 1.19% 1.19%
5 15 210 1050 2.38% 7.14% 1.43% 1.43%
6 21 210 1260 2.86% 10.00% 1.67% 1.67%
7 28 210 1470 3.33% 13.33% 1.90% 1.90%
8 36 210 1680 3.81% 17.14% 2.14% 2.14%
9 45 210 1890 4.29% 21.43% 2.38% 2.38%
10 55 210 2100 4.76% 26.19% 2.62% 2.62%
11 66 210 2310 5.24% 31.43% 2.86% 2.86%
12 78 210 2520 5.71% 37.14% 3.10% 3.10%
13 91 210 2730 6.19% 43.33% 3.33% 3.33%
14 105 210 2940 6.67% 50.00% 3.57% 3.57%
15 120 210 3150 7.14% 57.14% 3.81% 3.81%
16 136 210 3360 7.62% 64.76% 4.05% 4.05%
17 153 210 3570 8.10% 72.86% 4.29% 4.29%
18 171 210 3780 8.57% 81.43% 4.52% 4.52%
19 190 210 3990 9.05% 90.48% 4.76% 4.76%
20 210 210 4200 9.52% 100.00% 5.00% 5.00%
Example 2: Using 100 as the denominator for all the percentages to be summed does not yield a valid result.
Numerator Numerator (Running Total) Denominator Denominator (Running Total) Percentage Running Sum of % Running Sum of % divided by the number of percentages summed Running Percentage
1 1 100 100 1.00% 1.00% 1.00% 1.00%
2 3 100 200 2.00% 3.00% 1.50% 1.50%
3 6 100 300 3.00% 6.00% 2.00% 2.00%
4 10 100 400 4.00% 10.00% 2.50% 2.50%
5 15 100 500 5.00% 15.00% 3.00% 3.00%
6 21 100 600 6.00% 21.00% 3.50% 3.50%
7 28 100 700 7.00% 28.00% 4.00% 4.00%
8 36 100 800 8.00% 36.00% 4.50% 4.50%
9 45 100 900 9.00% 45.00% 5.00% 5.00%
10 55 100 1000 10.00% 55.00% 5.50% 5.50%
11 66 100 1100 11.00% 66.00% 6.00% 6.00%
12 78 100 1200 12.00% 78.00% 6.50% 6.50%
13 91 100 1300 13.00% 91.00% 7.00% 7.00%
14 105 100 1400 14.00% 105.00% 7.50% 7.50%
15 120 100 1500 15.00% 120.00% 8.00% 8.00%
16 136 100 1600 16.00% 136.00% 8.50% 8.50%
17 153 100 1700 17.00% 153.00% 9.00% 9.00%
18 171 100 1800 18.00% 171.00% 9.50% 9.50%
19 190 100 1900 19.00% 190.00% 10.00% 10.00%
20 210 100 2000 20.00% 210.00% 10.50% 10.50%
Example 3: summing percentages calculated with different denominators does not yield meaningful data.
Numerator Numerator (Running Total) Denominator Denominator (Running Total) Percentage Running Sum of % Running Sum of % divided by the number of percentages summed Running Percentage
1 1 10 10 10.00% 10.00% 10.00% 10.00%
2 3 25 35 8.00% 18.00% 9.00% 8.57%
3 6 12 47 25.00% 43.00% 14.33% 12.77%
4 10 15 62 26.67% 69.67% 17.42% 16.13%
5 15 17 79 29.41% 99.08% 19.82% 18.99%
6 21 88 167 6.82% 105.90% 17.65% 12.57%
7 28 398 565 1.76% 107.66% 15.38% 4.96%
8 36 49 614 16.33% 123.98% 15.50% 5.86%
9 45 50 664 18.00% 141.98% 15.78% 6.78%
10 55 66 730 15.15% 157.13% 15.71% 7.53%
11 66 5 735 220.00% 377.13% 34.28% 8.98%
12 78 75 810 16.00% 393.13% 32.76% 9.63%
13 91 49 859 26.53% 419.66% 32.28% 10.59%
14 105 30 889 46.67% 466.33% 33.31% 11.81%
15 120 31 920 48.39% 514.72% 34.31% 13.04%
16 136 32 952 50.00% 564.72% 35.29% 14.29%
17 153 33 985 51.52% 616.23% 36.25% 15.53%
18 171 34 1019 52.94% 669.17% 37.18% 16.78%
19 190 35 1054 54.29% 723.46% 38.08% 18.03%
20 210 36 1090 55.56% 779.02% 38.95% 19.27%

Friday, November 30, 2012

SSRS - Centralized Formatting

Centralized formatting should really be done via .net DLLs but in many organizations short and long term maintenance of reporting is more often than not ignored and the idea of providing development software to build DLLs to people who merely do reporting discounted.  When this is the case SSRS report developers have a second option of using code in the reports to replace the functionality the .net DLL would have provided.  The formatting will not be centralized as it would in a DLL however it is possible to use search and replace on report RDLs to modify the code thereby simplifying the process of making the same changes to hundreds of reports. This makes using values defined in code the second best choice.  Make sure to make backups of the RDLs first as it is very possible to do something to many reports that will require reverting to the original version. In this situation it is advisable to use code something like show below to format your report titles, tables, fonts, etc.  This code goes in the code section of your report. Examples of calling the code are at the end of this article.  There are two  SQLServerCentral.com articles on creating DLLs for this purpose. This first is SSRS - Custom Code with External Assemblies and the second is Centralising Reporting Services Stylesheets. You can use these links along with the code below to create you own custom centralized formatting DLLs.  This Microsoft link on security may also help you get them working.

Public Const NumberFormat = "###,###,###,###,##0.00"
Public Const PercentageFormat = "##0.00 %"   
 
Function BackgroundColor(ByVal Style As String) As String         
Select Case UCase(Style)
Case "MAINTITLE":Return "White"            
CASE "SUBTITLE1":Return "White"            
CASE "SUBTITLE2":Return "White"            
CASE "SUBTITLE3":Return "White"
Case "DATEGROUP":return "White"
Case "TABLEHEADER":Return "CornflowerBlue"            
CASE "TABLEFOOTER" :Return "CornflowerBlue"
Case "GROUP1HEADER":Return "#8fb3f3"            
CASE "GROUP1FOOTER":Return "#8fb3f3"
Case "GROUP2HEADER":Return "#c7d9f9"            
CASE "GROUP2FOOTER":Return "#c7d9f9"
Case "SUBTOTAL":Return "#8fb3f3"
Case "GRANDTOTAL":Return "White"
Case "DETAIL": Return "White"            
CASE "PAGEFOOTER": Return "White"            
CASE "REPORTFOOTER": Return "White"            
CASE Else: Return "White"        
End Select
End Function
 
Function FontColor(ByVal BackGroundStyle As String) As String
Select CASE UCase(BackGroundStyle )  
CASE "PURPLE":Return "Black"  
CASE "DARKBLUE":Return "Black"  
CASE "WHITE":Return "Black"  
CASE "LIGHTSTEELBLUE":Return "Black"  
CASE "LIGHTGREY":Return "Black"  
CASE "LightGrey":Return "Black"  
CASE "#6e9eca":Return "Black"  
CASE "#e0e0e0":Return "Black"  
CASE Else: Return "Black" 
End Select
 
End Function

Function FontFamily(ByVal Style As String) As String
Select Case UCase(Style)            
CASE "MAINTITLE":Return "Arial"            
CASE "SUBTITLE1":Return "Arial"            
CASE "SUBTITLE2":Return "Arial"            
CASE "SUBTITLE3":Return "Arial"            
CASE "TABLEHEADER":Return "Arial Narrow"            
CASE "TABLEFOOTER" :Return "Arial Narrow"            
CASE "GROUP1HEADER":Return "Arial Narrow"            
CASE "GROUP1FOOTER":Return "Arial Narrow"            
CASE "GROUP2HEADER":Return "Arial Narrow"            
CASE "GROUP2FOOTER":Return "Arial Narrow"             
CASE "DATEGROUP":return "Arial Narrow"            
CASE "SUBTOTAL":Return "Arial Narrow"            
CASE "GRANDTOTAL":Return "Arial Narrow"            
CASE "DETAIL": Return "Arial Narrow"            
CASE "PAGEFOOTER": Return "Arial Narrow"            
CASE "REPORTFOOTER": Return "Arial Narrow"            
CASE Else: Return "Arial Narrow"        
End Select
 
End Function

Function FontSize(ByVal Style As String) As String
Select Case UCase(Style)            
CASE "MAINTITLE":Return "12pt"            
CASE "SUBTITLE1":Return "12pt"            
CASE "SUBTITLE2":Return "12pt"            
CASE "SUBTITLE3":Return "10pt"            
CASE "TABLEHEADER":Return "8pt"            
CASE "TABLEFOOTER" :Return "8pt"            
CASE "GROUP1HEADER":Return "8pt"            
CASE "GROUP1FOOTER":Return "8pt"            
CASE "GROUP2HEADER":Return "8pt"            
CASE "GROUP2FOOTER":Return "8pt"            
CASE "DATEGROUP":return "8pt"            
CASE "SUBTOTAL":Return "8pt"            
CASE "GRANDTOTAL":Return "8pt"            
CASE "DETAIL": Return "8pt"            
CASE "PAGEFOOTER": Return "8pt"            
CASE "REPORTFOOTER": Return "8pt"            
CASE Else: Return "8pt"        
End Select
End Function
 
Function FontWeight(ByVal Style As String) As String
Select  CASE UCase(Style)            
CASE "MAINTITLE":Return "Bold"            
CASE "SUBTITLE1":Return "Bold"            
CASE "SUBTITLE2":Return "Bold"            
CASE "SUBTITLE3":Return "Bold"            
CASE "TABLEHEADER":Return "Bold"            
CASE "TABLEFOOTER" :Return "Bold"            
CASE "GROUP1HEADER":Return "Normal"            
CASE "GROUP1FOOTER":Return "8pt"            
CASE "GROUP2HEADER":Return "8pt"            
CASE "GROUP2FOOTER":Return "Bold"             
CASE "DATEGROUP":return "Bold"            
CASE "SUBTOTAL":Return "Bold"            
CASE "GRANDTOTAL":Return "Bold"            
CASE "DETAIL": Return "Normal"            
CASE "PAGEFOOTER": Return "Normal"            
CASE "REPORTFOOTER": Return "Normal"            
CASE Else: Return "Normal"        
End Select
 
End Function

Function FontStyle(ByVal Style As String) As String 
 Select CASE UCase(Style)            
CASE "MAINTITLE":Return "Normal"            
CASE "SUBTITLE1":Return "Normal"            
CASE "SUBTITLE2":Return "Normal"            
CASE "SUBTITLE3":Return "Normal"            
CASE "TABLEHEADER":Return "Normal"            
CASE "TABLEFOOTER" :Return "Normal"            
CASE "GROUP1HEADER":Return "Normal"            
CASE "GROUP1FOOTER":Return "Normal"            
CASE "GROUP2HEADER":Return "Normal"            
CASE "GROUP2FOOTER":Return "Normal"             
CASE "DATEGROUP":return "Normal"            
CASE "SUBTOTAL":Return "Normal"            
CASE "GRANDTOTAL":Return "Normal"            
CASE "DETAIL": Return "Normal"            
CASE "PAGEFOOTER": Return "Normal"            
CASE "REPORTFOOTER": Return "Normal"            
CASE Else: Return "Normal"        
End Select
 
End Function 
  
----------------------------------------------------------------------------

Examples of using this code in your report.

<Color>=code.FontColor(code.BackgroundColor("SUBTITLE1"))</Color> <BackgroundColor>=code.BackgroundColor("SUBTITLE1")</BackgroundColor> <FontStyle>=code.FontStyle("SUBTITLE1")</FontStyle> <FontFamily>=code.FontFamily("SUBTITLE1")</FontFamily> <FontSize>=code.FontSize("SUBTITLE1")</FontSize> <FontWeight>=code.FontWeight("SUBTITLE1")</FontWeight> 
<Color>=code.FontColor(code.BackgroundColor("SUBTITLE2"))</Color> <BackgroundColor>=code.BackgroundColor("SUBTITLE2")</BackgroundColor> <FontStyle>=code.FontStyle("SUBTITLE2")</FontStyle> <FontFamily>=code.FontFamily("SUBTITLE2")</FontFamily> <FontSize>=code.FontSize("SUBTITLE2")</FontSize> <FontWeight>=code.FontWeight("SUBTITLE2")</FontWeight> 
  


<FontStyle>=code.FontStyle("GROUP1FOOTER")</FontStyle>
<FontFamily>=code.FontFamily("GROUP1FOOTER")</FontFamily>
<FontSize>=code.FontSize("GROUP1FOOTER")</FontSize>
<FontWeight>=code.FontWeight("GROUP1FOOTER")</FontWeight>
<Format>=code.NumberFormat </Format>

Tuesday, October 30, 2012

SSRS - Centralized Formatting

Centralized formatting should really be done via .net DLLs but in many organizations short and long term maintenance of reporting is more often than not ignored and the idea of providing development software to build DLLs to people who merely do reporting discounted.  When this is the case SSRS report developers have a second option of using code in the reports to replace the functionality the .net DLL would have provided.  The formatting will not be centralized as it would in a DLL however it is possible to use search and replace on report RDLs to modify the code thereby simplifying the process of making the same changes to hundreds of reports. This makes using values defined in code the second best choice.  Make sure to make backups of the RDLs first as it is very possible to do something to many reports that will require reverting to the original version. In this situation it is advisable to use code something like show below to format your report titles, tables, fonts, etc.  This code goes in the code section of your report. Examples of calling the code are at the end of this article.  There are two  SQLServerCentral.com articles on creating DLLs for this purpose. This first is SSRS - Custom Code with External Assemblies and the second is Centralising Reporting Services Stylesheets. You can use these links along with the code below to create you own custom centralized formatting DLLs.  This Microsoft link on security may also help you get them working.

Public Const NumberFormat = "###,###,###,###,##0.00" 
Public Const PercentageFormat = "##0.00 %"    

  
Function BackgroundColor(ByVal Style As String) As String          
Select Case UCase(Style) 
            Case "MAINTITLE":Return "White"             Case "SUBTITLE1":Return "White"             Case "SUBTITLE2":Return "White"             Case "SUBTITLE3":Return "White" 
            Case "DATEGROUP":return "White" 
            Case "TABLEHEADER":Return "CornflowerBlue"             Case "TABLEFOOTER" :Return "CornflowerBlue" 
            Case "GROUP1HEADER":Return "#8fb3f3"             Case "GROUP1FOOTER":Return "#8fb3f3" 
            Case "GROUP2HEADER":Return "#c7d9f9"             Case "GROUP2FOOTER":Return "#c7d9f9" 
             Case "SUBTOTAL":Return "#8fb3f3" 
            Case "GRANDTOTAL":Return "White" 
            Case "DETAIL": Return "White"             Case "PAGEFOOTER": Return "White"             Case "REPORTFOOTER": Return "White"             Case Else: Return "White"         End Select End Function 
  
Function FontColor(ByVal BackGroundStyle As String) As String 
Select Case UCase(BackGroundStyle )   Case "PURPLE":Return "Black"   Case "DARKBLUE":Return "Black"   Case "WHITE":Return "Black"   Case "LIGHTSTEELBLUE":Return "Black"   Case "LIGHTGREY":Return "Black"   Case "LightGrey":Return "Black"   Case "#6e9eca":Return "Black"   Case "#e0e0e0":Return "Black"   Case Else:Return "Black"  End Select End Function 
Function FontFamily(ByVal Style As String) As String 
        Select Case UCase(Style)             Case "MAINTITLE":Return "Arial"             Case "SUBTITLE1":Return "Arial"             Case "SUBTITLE2":Return "Arial"             Case "SUBTITLE3":Return "Arial"             Case "TABLEHEADER":Return "Arial Narrow"             Case "TABLEFOOTER" :Return "Arial Narrow"             Case "GROUP1HEADER":Return "Arial Narrow"             Case "GROUP1FOOTER":Return "Arial Narrow"             Case "GROUP2HEADER":Return "Arial Narrow"             Case "GROUP2FOOTER":Return "Arial Narrow"              Case "DATEGROUP":return "Arial Narrow"             Case "SUBTOTAL":Return "Arial Narrow"             Case "GRANDTOTAL":Return "Arial Narrow"             Case "DETAIL": Return "Arial Narrow"             Case "PAGEFOOTER": Return "Arial Narrow"             Case "REPORTFOOTER": Return "Arial Narrow"             Case Else: Return "Arial Narrow"         End Select End Function 
Function FontSize(ByVal Style As String) As String 
        Select Case UCase(Style)             Case "MAINTITLE":Return "12pt"             Case "SUBTITLE1":Return "12pt"             Case "SUBTITLE2":Return "12pt"             Case "SUBTITLE3":Return "10pt"             Case "TABLEHEADER":Return "8pt"             Case "TABLEFOOTER" :Return "8pt"             Case "GROUP1HEADER":Return "8pt"             Case "GROUP1FOOTER":Return "8pt"             Case "GROUP2HEADER":Return "8pt"             Case "GROUP2FOOTER":Return "8pt"             Case "DATEGROUP":return "8pt"             Case "SUBTOTAL":Return "8pt"             Case "GRANDTOTAL":Return "8pt"             Case "DETAIL": Return "8pt"             Case "PAGEFOOTER": Return "8pt"             Case "REPORTFOOTER": Return "8pt"             Case Else: Return "8pt"         End Select End Function 
 Function FontWeight(ByVal Style As String) As String 
        Select Case UCase(Style)             Case "MAINTITLE":Return "Bold"             Case "SUBTITLE1":Return "Bold"             Case "SUBTITLE2":Return "Bold"             Case "SUBTITLE3":Return "Bold"             Case "TABLEHEADER":Return "Bold"             Case "TABLEFOOTER" :Return "Bold"             Case "GROUP1HEADER":Return "Normal"             Case "GROUP1FOOTER":Return "8pt"             Case "GROUP2HEADER":Return "8pt"             Case "GROUP2FOOTER":Return "Bold"              Case "DATEGROUP":return "Bold"             Case "SUBTOTAL":Return "Bold"             Case "GRANDTOTAL":Return "Bold"             Case "DETAIL": Return "Normal"             Case "PAGEFOOTER": Return "Normal"             Case "REPORTFOOTER": Return "Normal"             Case Else: Return "Normal"         End Select End Function 
 Function FontStyle(ByVal Style As String) As String 
        Select Case UCase(Style)             Case "MAINTITLE":Return "Normal"             Case "SUBTITLE1":Return "Normal"             Case "SUBTITLE2":Return "Normal"             Case "SUBTITLE3":Return "Normal"             Case "TABLEHEADER":Return "Normal"             Case "TABLEFOOTER" :Return "Normal"             Case "GROUP1HEADER":Return "Normal"             Case "GROUP1FOOTER":Return "Normal"             Case "GROUP2HEADER":Return "Normal"             Case "GROUP2FOOTER":Return "Normal"              Case "DATEGROUP":return "Normal"             Case "SUBTOTAL":Return "Normal"             Case "GRANDTOTAL":Return "Normal"             Case "DETAIL": Return "Normal"             Case "PAGEFOOTER": Return "Normal"             Case "REPORTFOOTER": Return "Normal"             Case Else: Return "Normal"         End Select End Function 
  
----------------------------------------------------------------------------

Examples of using this code in your report.

<Color>=code.FontColor(code.BackgroundColor("SUBTITLE1"))</Color> <BackgroundColor>=code.BackgroundColor("SUBTITLE1")</BackgroundColor> <FontStyle>=code.FontStyle("SUBTITLE1")</FontStyle> <FontFamily>=code.FontFamily("SUBTITLE1")</FontFamily> <FontSize>=code.FontSize("SUBTITLE1")</FontSize> <FontWeight>=code.FontWeight("SUBTITLE1")</FontWeight> 

<Color>=code.FontColor(code.BackgroundColor("SUBTITLE2"))</Color> <BackgroundColor>=code.BackgroundColor("SUBTITLE2")</BackgroundColor> <FontStyle>=code.FontStyle("SUBTITLE2")</FontStyle> <FontFamily>=code.FontFamily("SUBTITLE2")</FontFamily> <FontSize>=code.FontSize("SUBTITLE2")</FontSize> <FontWeight>=code.FontWeight("SUBTITLE2")</FontWeight> 
  


<FontStyle>=code.FontStyle("GROUP1FOOTER")</FontStyle>

<FontFamily>=code.FontFamily("GROUP1FOOTER")</FontFamily>
<FontSize>=code.FontSize("GROUP1FOOTER")</FontSize>
<FontWeight>=code.FontWeight("GROUP1FOOTER")</FontWeight>
<Format>=code.NumberFormat </Format>