2

I have an excel sheet with many tabs. Say one is called wsMain and the other is called wsDate.

In my data flow transformation I am able to successfully load the data from wsMain to my table.

Now I have to update this transformation where I have to fetch the maximum date from the worksheet wsDate and only load data from wsMain where the date is less than on equal to the maximum date in wsDate (that is the only column available).

So for I have figured out that I need to create a new Excel connection manager to read the data from wsDate and I have used the Aggregate transformatioin to get the maximum date.

Now the question is how do I use this date to restrict the rows coming from wsMain?

I understand from the link below that you can store the value in a variable but what do I do next?: SSIS set result set from data flow to variable

I have tried using a merge join but not sure if I am doing it right.

Here is what it looks like now:

https://imgur.com/dAXsGpm

Unbound
  • 177
  • 2
  • 14

3 Answers3

1

I could not achieve the above but would be interested to know if that is possible. As a work around I have created a separate dataflow where I have stored the valued in a variable and then used the variable in the conditional split to filter the required rows:

enter image description here

Here is a step by step guide I followed to write the variable: https://www.proteanit.com/2008/12/11/ssis-writing-to-a-package-variable-in-a-dataflow/

Unbound
  • 177
  • 2
  • 14
1

You can obtain the maximum value of the wsDate column first, this use this as a filter to avoid introducing unnecessary records into the data flow which which would be discarded by the Conditional Split. An overview of this process is below. I'd also recommend confirming the data types for all columns involved.

  • Create an SSIS DateTime variable and name this something descriptive such as MaxDate.

  • Create a Data Flow Task before the current one with an Excel Source component. Use the SQL command option for the Data Access Mode and enter a SQL statement to return the max value of the wsDate column. In the following example ExcelSource is the name of the sheet that you're pulling from. I'd suggested confirming the query with the Preview button on the Excel Source as well.

  • Add a Script Component (not Task) after the Excel Source. Add the MaxDate variable in the ReadWriteVariables field on the main page of the Script Component. On the Inputs and Outputs pane add the output column from the Excel Source as an Input Column with the ReadOnly usage Type. Example C# code for this is below. Note that variables can only be written to in the PostExecute method. The Input0_ProcessInputRow method is called once for each row that passes through, however there will only be the single row in this case. On the following code MaxExcelDate is the name of the output column from the Excel Source.

  • On the Excel Source component in the Data Flow Task where the records are imported from Excel, change the Data Access Mode to SQL command and enter a SQL statement to return records that have a date less than or equal to the maximum wsDate value. This is the last example and the ? is a placeholder for the parameter. After entering this SQL, click the Parameters button and select Parameter0 for the Parameters field, the MaxDate variable for Variables field, and a direction of Input. The Conditional Split can then be removed since these records will now be filtered out.

Excel MAX wsDate SELECT:

SELECT MAX(wsDate) AS MaxExcelDate FROM ExcelSource

C# Script Component:

DateTime maxDate;
public override void PostExecute()
{
    base.PostExecute();
    Variables.MaxDate = maxDate;
}
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
    maxDate = Row.MaxExcelDate;
}

Excel Command with Date Filter:

SELECT 
    Column1,
    Column2,
    Column3
FROM ExcelSheet 
WHERE DateColumn <= ?
userfl89
  • 4,610
  • 1
  • 9
  • 17
1

Yes, it is possible. In the data flow, you will need to determine the max date, which you already have. Next, you will need to MERGE JOIN the two data flows on the date column. From there, you will feed it into a CONDITIONAL SPLIT and split where the date columns match [i.e., !ISNULL()] versus do not match [i.e., ISNULL()]. In your case, you only want the matches. The non-matches will be disregarded.

Note: if you use an INNER JOIN on the MERGE JOIN where there is only one date (i.e., MaxDate) to join on, then this will take care of the row filtering for you. You will not need a CONDITIONAL SPLIT.

Welcome to ETL.

Update

It is a real pain that SSIS's MERGE JOINs only perform joins on EQUAL operations as opposed to LESS THAN and GREATER THAN operations. You will need to separate the data flows.

  1. Use a script component to scan the excel file for the MAX Date and assign that value to a package variable in SSIS. Alternatively, you can have a dates table in SQL Server and then use an Execute SQL Command in SSIS to retrieve the MAX Date from the table and assign that value to a package variable
  2. Modify your existing data flow to remove the reading of the Excel date file completely. Then add a DERIVED COLUMN transformation and add a new column that is mapped to the package variable in SSIS that stores the MAX date. You can name the Derived Column Name 'MaxDate'
  3. Add a conditional split transformation with the following CONDITION logic: [AsOfDt] <= [MaxDate]
  4. Set the Output Name to Insert Records

Note: The CONDITIONAL SPLIT creates a new output data flow with restricted/filtered rows. It does not create a new column within the existing data flow. Think of this as a transposition of data flow output from column modification to row modification. Only those rows that match the condition will be sent to the output that you desire. I assume you only want to Insert these records, so I named it that. You can choose whatever naming convention you prefer

Note 2: Sorry for not making the Update my original answer - I haven't used the AGGREGATE transformation before so I was not aware that it restricts row output as opposed to reading a value in the data flow and then assigning it to a variable. That would be a terrific transformation for Microsoft to add to SSIS. It appears that the ROWCOUNT and SCRIPT COMPONENT transformations are the only ones that have the ability to set a package variable value within the data flow.

J Weezy
  • 3,507
  • 3
  • 32
  • 88
  • The dates allowed needs to be less than or equal to the maximum date not equal to then how would you do that? – Unbound Apr 16 '19 at 09:59
  • @Unbound Can you merge the data flows together? If yes, then use a CONDITIONAL SPLIT. – J Weezy Apr 16 '19 at 15:02
  • Sorry, I do not see how that makes sense. Once you join the max date with every record there is no point in splitting. – Unbound Apr 17 '19 at 14:53
  • @Unbound I have updated my answer. I really wish Microsoft would revisit the existing Common transformations and beef them up. That would greatly reduce the complexity of packages and simplify development, making developers lives much easier while enhancing adoption of a great tool that is free. – J Weezy Apr 17 '19 at 15:52
  • it still does not make sense. It looks like what you have described is what I have done as a workaround using another data flow (also I have posted my workaround which was the first answer to this post). Your solution still does not answer how you can achieve this together in a single data flow. PLEASE read the original question and the answers posted by myself and others first. – Unbound Apr 18 '19 at 12:04
  • @Unbound Please read my update: I confirmed that in this case it is not possible due to the limitations of how the MERGE JOIN joins the data flows: it only mergers where keys are equal to each other, not less than or greater than. This results in NULL values for dates that do not match the max date, which means it is not possible to perform comparisons. My apologies for the confusion - I thought it was possible. You will need to find another way to join data flows together before you can perform comparisons on dates. – J Weezy Apr 18 '19 at 14:15