Excel

Sunday, December 18, 2011

SSRS : New Line Expression

SSRS : New Line Expression

       If you want to display the text in a new line at the text box, the following expression can be used.

="Line 1 " + VBCRLF + "Line 2"

Ex : =Fields!AddressLine1 + VBCRLF + Fields!AddressLine2 + VBCRLF + Fields!AddressLine3

OP:
 Address1
 Address2
 Address3


Note: If VBCRLF is used first or last part the expression, it has no impact on result. If you want a line in the first or last, then use one more extra VBCRLF.

Friday, December 16, 2011

SSIS- Pivot transformation


The following article neatly explains how to use Pivot Transformation in SSIS


This tutorial provides a complete pivot sample on AdventureWorks sample SQL Server database with SSIS 2008...
Assume you want to find order quantities for each product by year.
you may use this query to get the result:
 select
    Product.ProductID,
    Product.Name,
    year(SalesOrderHeader.OrderDate) as OrderYear,
    sum(SalesOrderDetail.OrderQty) as OrderQuantity
from Sales.SalesOrderDetail
inner join Production.Product
on SalesOrderDetail.ProductID=Product.ProductID
inner join Sales.SalesOrderHeader
on SalesOrderDetail.SalesOrderID=SalesOrderHeader.SalesOrderID
group by Product.ProductID,Product.Name,year(SalesOrderHeader.OrderDate)
order by Product.Name,year(SalesOrderHeader.OrderDate)
and this is result of query above:

Now , create a new SSIS package
Add a Data Flow Task
in dataflow tab :
Add a OLE DB Source and connect it to AdventureWorks database in your SQL Server
write the query in SQL command Text of oledb data source,
then add a PIVOT Transformation after OLE DB Data Source

in Pivot transformation advanced editor , go to input columns tab and select all input columns

then go to input and output properties tab, select pivot default input, under input columns you will find all columns which you selected in previous tab.
the only property you must set for each input column is the PivotUsage.
this describe the values you can use in PivotUsage property:

So , in this example , PivotUsage property for each input column will be as below:
ProductID           0
Name                 1
OrderYear           2
OrderQuantity     3
Notice that you must have at least one input column with PivotUsage 2, one input column with PivotUsage 3,one input column with PivotUsage 0 OR 1 at least in your pivot transformation.

After setting input columns, go to Pivot Default output , and under output columns add these columns:
ID, Name , 2001 ,2002 , 2003 , 2004
ID and Name columns will show the exact values form ProductID and Name input columns.
So, set SourceColumn property of ID output column to LineAgeID of ProductID input column.
Note: this is critical to use the lineageID value of input columns not the ID value. if you use ID value you will face error.
but how can you find lineageID of input columns?
simply select ProductID under input columns and see the lineageID value there.
Note: the lineageID is different on each machine, so you can not use exact lineage numbers used in this example.you must use your lineageID values.
this is lineageID of ProductID input column:
and this is the SourceColumn property in ID output column:

leave the PivotKeyValue of ID and Name output column as empty.
Do above steps for Name output column with Name input column too
Now it's Pivot columns turn, select 2001 under output columns
this columns will show the results for the year 2001, so enter 2001 in PivotKeyValue property.
and set SourceColumn with lineageID of OrderQuantity input column.
In fact you must set SourceColumn property with the lineageID of the input column which has the PivotUsage 3 value.
in this example , lineageID of OrderQuantity is 69 (Remember you must use your own lineageIDs)

So, we set SourceColumn with 69 in 2001 output column:

Now , do the same for 2002,2003,2004 columns.the only difference is that you must set PivotKeyValue to 2002,2003,2004 in these columns. butlineageID will be the same in all 2001 - 2004 columns.
Pivot configuration finished now.
Just add a destination, and fill the result of Pivot in destination.
I used a RecodeSet Destination with a Grid Data Viewer to show the result,
you can add Grid dataviewer by right click on green arrow between pivot transformation and  your destination, then select data viewers, then add Grid.
this is the full desing of my DataFlow Task:

and this is the final Result:

 Courtesy : Reza Rad (www.rad.pasfu.com)

Wednesday, December 14, 2011

Creating Time Dimension table & Data population

Script to create a simple Time Dimension Table using SQL script and populate data :

IF EXISTS (SELECT name FROM sysobjects WHERE name = 'TimeDimension' AND type = 'U')
DROP TABLE TimeDimension
GO
CREATE TABLE TimeDimension
(
[DateID] numeric(8,0) PRIMARY KEY CLUSTERED,
[Date] SMALLDATETIME, [Day] VARCHAR(10),
week varchar(10), [Month] VARCHAR(10),
[Quarter] varchar(10),
[Year] SMALLINT,
DayOfMonth SMALLINT,
DayOfYear smallint,
WeekOfYear SMALLINT,
MonthOfYear SMALLINT,
QuarterOfYear smallint,
[FinYear] nvarchar(10),
[FinMonthOfYear] smallint,
[FinWeekOfYear] smallint,
[FinWeek] varchar(10),
[FinQuarterOfYear] Smallint
)
GO DECLARE @CurrDate SMALLDATETIME

SET @CurrDate = '01/01/2008'
WHILE @CurrDate < getdate()+360
BEGIN
INSERT TimeDimension
([DateID],[Date], [Day], [Month], [Year], DayOfMonth, WeekOfYear, MonthOfYear, [Quarter],

DayOfYear,week,Quarterofyear)
VALUES
(
CONVERT(Varchar(10),@CurrDate,112), --Date-As-ID @CurrDate,-- theDate
DATENAME(dw, @CurrDate), -- theDay
DATENAME(month, @CurrDate), -- theMonth
YEAR(@CurrDate), -- theYear
DAY(@CurrDate), -- DayOfMonth
DATENAME(wk, @CurrDate), -- WeekOfYear
MONTH(@CurrDate), -- MonthOfYear
'Quarter' + DATENAME(quarter,@CurrDate), -- Quarter
datename(dy,@CurrDate), --Day of Year
'Week ' + Datename(wk,@CurrDate), --Week
DATENAME(quarter,@CurrDate)) --Quarterof year
SET @CurrDate = DATEADD(day, 1, @CurrDate)
END

The above script will create a TimeDimension table and it will populate dimensional data from 01/01/2008 upto 360 days from run date.

Wednesday, December 7, 2011

SSRS Custom code to convert the Integer values into “Days:HH:MM” format


To Convert the Integer values into “Days:HH:MM” format:

Write the following VB.Net code in Custom Code area (Right click on report -> Report Properties -> Code)


Function MinToDDHHMM(iValues  as integer) as String
        Dim mDay,  iHour As Integer
        Dim mMinute As Integer
 If mValue >= (24 * 60) Then
            mDay = mValue \ (24 * 60)
            mValue = mValue Mod (24 * 60)
 End If
 If mValue >= 60 Then
            mHour = mValue \ 60
            mValue = mValue Mod 60
  End If
mMinute = mValue
MinToDDHHMM = IIF(mDay>9,iDays, "0" & mDay) &  ":" & IIF(mHour>9,mHour, "0"&mHour) & ":" &
                                                        IIF(mMinute>9 ,mMinute,"0" &mMinute)
End Function

Now, use the following value expression “ =Code.MinToDDHHMM(Fields!TotalMinutes.Value) in the report area.

(Considered that the field “TotalMinutes” contains the minute value to be converted into Days:HH:MM format.)

Result : You will get the minute value “1505” as “01:01:05”

SSRS Custom Code to Convert minutes into HH:MM format



To Convert the Integer values into “HH:MM” format in SSRS:

Write the following VB.Net code in Custom Code area (Right click on report -> Report Properties -> Code)

Function ToHHMM(Minutes As Integer) As String
Dim RetValue As String
Dim min As Integer
RetValue = "0000:00"
If Minutes < 0 Then Minutes = Minutes * -1
RetValue = Fix(Minutes / 60)
min = Minutes Mod 60
If min < 10 Then
RetValue = RetValue + ":0" + CStr(min)
Else
RetValue = RetValue + ":" + CStr(min)
End If
ToHHMM = RetValue
End Function

Now, use the following value expression “ =Code.ToHHMM(Fields!TotalMinutes.Value) in the report area.

(Considered that the field “TotalMinutes” contains the minute value to be converted into HH:MM format.)

Result : You will get the minute value “185” as “03:05”

Saturday, December 3, 2011

SSIS - Check the availability of Data Source


When the tables in the DW are truncated and populated on regular basis, most of the time the truncate script will run first and then the data population from Source to destination will happen. What if the Data Source is not available in mid of the process or at any circumstances? 
 The truncation will occur and the destination will be empty, to avoid this we can check the availability of the source before initiating the truncation or any other tasks. 
To do so, we can use the following steps:

1.       Create variable in the Package (Package level scope) to which will be used to flag that the source is available or not.
2.       Create a script task with the following script in (it will check whether the source is available or not & update the variable)

                Script task:

Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime

Public Sub Main()

        Dim ConMgr As ConnectionManager = Dts.Connections("<SourceConn>")
        Dim ConStr As String = ConMgr.ConnectionString

        Try
            ConMgr.AcquireConnection(Nothing)
            Dts.Events.FireInformation(1, "", "Connection acquired Successfully on" + ConMgr.Name, "", 0, False)
            Dts.Events.FireInformation(2, "", "Connection String=" + ConStr, "", 0, False)
            Dts.Variables("SourceAvailability").Value = "y"
            Dts.TaskResult = ScriptResults.Success

        Catch ex As Exception
            Dts.Events.FireInformation(1, "", "Connection Failed on" + ConMgr.Name, "", 0, False)
            Dts.Events.FireInformation(2, "", "Connection String=" + ConStr, "", 0, False)
            Dts.Variables("SourceAvailability").Value = "n"
            Dts.TaskResult = ScriptResults.Success
        End Try
      End Sub

End Class

3.       Use the precedence Constraint to write the expression as shown in fig . (Double click on the green arrow to go the Precedence constraint Editor) 


Ie, This precedence constraint will let the execution to flow to the next tasks only if the source is available.

Courtesy: got idea from http://consultingblogs.emc.com .


Monday, November 14, 2011

SQL : To calculate the Time difference between the consecutive incidents(Time between incidents)


            If you want to calcualte the time difference between the first record and the next record - we can use the following SQL Query.

EX : Data

          In the above sample to calculate the Time difference between the First incident's closed Time and the next incident's Start Time (we can call it as a time between incidents)

SQL :



WITH CTE
                  as (

SELECT IncidentNumber, IncidentOpenDate, IncidentClosedDate,
               ( SELECT MAX(T1.IncidentClosedDate)
      FROM Trn_Incidents T1
      WHERE T1.IncidentClosedDate < T2.IncidentOpenDate
    ) AS Previous_IncidentEndTime
 FROM Trn_Incidents T2
                    )

SELECT IncidentNumber, IncidentOpenDate, IncidentClosedDate ,  DATEDIFF(MI,Previous_IncidentEndTime,IncidentOpenDate) AS IdleTime
FROM CTE

The Result would be :


Now we got the idle time between the incident sequence..!!



Monday, September 26, 2011

SQL server : Usage of "in" clause in "If" condition


                    I had a situation to pass multi-value parameter into SSRS data set and execute a query if the multi value parameter contains a value -'ABC' else execute another one query  (to put it in simple form) ..So i tried using the following syntax:


If @Param1 in ('ABC')  
Select  a,b from Table1


else 
select b,c from Table1
End



 we got surprised when the above syntax didn't work as expected..Then we identified the trick over the SQL syntax :

The IN clause should be in reverse order when it has been used in IF Conditions:


If @Param1 'ABC' in (@Param1)


Select  a,b from Table1


else 
select b,c from Table1
End

Then it started working fine..!