How to get + value

Hi Team, Please find below table FYR,

Date Code DT Final Date Note
7/26/2021 Mango T+3 7/31/2021 Should exclude Sat and Sunday
7/26/2021 Cucum T+4 7/31/2021 Should exclude only Sat
7/26/2021 Mango T+5 8/1/2021 Should exclude Sat and Sunday
7/26/2021 PP T+6 8/2/2021 Should exclude Sat and Sunday
7/26/2021 TT T+7 8/5/2021 Should exclude Sat and Sunday
7/26/2021 Cucum T+8 8/6/2021 Should exclude Sat and Sunday
7/26/2021 PP T+9 8/7/2021 Should exclude Sat and Sunday
7/26/2021 TT T+10 8/8/2021 Should exclude Sat and Sunday

Here in the above table based on Date column and Code column i need to add Finaldate values calculating from DT column,

see here in the Date column it is 7/26/2021 i have to calculate the final date by considering T+ value in DT column, if it T+3 i have to increase the date 3 times but the challenge here is i have to exclude sat only if it cucum from code column and exclude sat and sunday date if it is other than cucum…how do i achive this?

Date T T+1 T+2
7/26/2024 7/26/2024 7/29/2024 7/30/2024

see this is the above example, here i have excluded 27th and 28th bcz it is sat and sunday

Are you using C# or VB.NET?

We are using VB.net, please help me to achieve this, do i have to loop through and check using condition? according to date i have to increment checking T + values, so here i have to extract number from T + 3 right? then i have to check if the date is sat or sunday i have exclude and increment it accordingly

What does your source DataTable look like? This?

Date Code DT
7/26/2021 Mango T+3
7/26/2021 Cucum T+4
7/26/2021 Mango T+5
7/26/2021 PP T+6
7/26/2021 TT T+7
7/26/2021 Cucum T+8
7/26/2021 PP T+9
7/26/2021 TT T+10

Do you want to programatically add the Final Date column based on your requirements (extract the number from the DT column and add that amount of days to the date in the Date column excluding Saturday if Code = “Cucum”, otherwise excluding both Saturday and Sunday?

Yes exactly, you are right

I want to do this in studio itself

I have built your table using a Build Data Table activity:

image

Create an Invoke Code activity with VBNet for the language. Add an argument to the activity as such, replacing YourTable with the name of your DataTable variable

Add the following code to the Invoke Code activity:

' Add the Final Date column to the DataTable
table.Columns.Add("Final Date", GetType(DateTime))

' Loop through each row to calculate the Final Date
For Each row As DataRow In table.Rows
    Dim startDate As DateTime = Convert.ToDateTime(row("Date")).Date ' Ensure we are working only with the date part
    Dim code As String = row("Code").ToString()
    Dim daysToAdd As Integer = Integer.Parse(row("DT").ToString().Substring(2))

    Dim finalDate As DateTime = startDate
    Dim daysAdded As Integer = 0

    ' Calculate the final date by skipping the specified days
    While daysAdded < daysToAdd
        finalDate = finalDate.AddDays(1)

        If code = "Cucum" Then
            ' Exclude Saturdays
            If finalDate.DayOfWeek <> DayOfWeek.Saturday Then
                daysAdded += 1
            End If
        Else
            ' Exclude Saturdays and Sundays
            If finalDate.DayOfWeek <> DayOfWeek.Saturday AndAlso finalDate.DayOfWeek <> DayOfWeek.Sunday Then
                daysAdded += 1
            End If
        End If
    End While

    ' Set the calculated final date to the Final Date column
    row("Final Date") = finalDate.Date ' Ensure only the date part is set
Next

' Display the results
For Each row As DataRow In table.Rows
    Console.WriteLine($"{row("Date"):d} - {row("Code")} - {row("DT")} - {row("Final Date"):d}")
Next

If you don’t want to print the results, remove this section of code:

' Display the results
For Each row As DataRow In table.Rows
    Console.WriteLine($"{row("Date"):d} - {row("Code")} - {row("DT")} - {row("Final Date"):d}")
Next

Tim

Hi Timothy, Thanks for your reply, what if i have already Final Date in the column,


do i have to remove this part?

Assuming that the Final Date column is empty:

' Loop through each row to calculate the Final Date
For Each row As DataRow In table.Rows
    Dim startDate As DateTime = Convert.ToDateTime(row("Date")).Date ' Ensure we are working only with the date part
    Dim code As String = row("Code").ToString()
    Dim daysToAdd As Integer = Integer.Parse(row("DT").ToString().Substring(2))

    Dim finalDate As DateTime = startDate
    Dim daysAdded As Integer = 0

    ' Calculate the final date by skipping the specified days
    While daysAdded < daysToAdd
        finalDate = finalDate.AddDays(1)

        If code = "Cucum" Then
            ' Exclude Saturdays
            If finalDate.DayOfWeek <> DayOfWeek.Saturday Then
                daysAdded += 1
            End If
        Else
            ' Exclude Saturdays and Sundays
            If finalDate.DayOfWeek <> DayOfWeek.Saturday AndAlso finalDate.DayOfWeek <> DayOfWeek.Sunday Then
                daysAdded += 1
            End If
        End If
    End While

    ' Set the calculated final date to the Final Date column
    row("Final Date") = finalDate.Date ' Ensure only the date part is set
Next

' Display the results
For Each row As DataRow In table.Rows
    Console.WriteLine($"{row("Date"):d} - {row("Code")} - {row("DT")} - {row("Final Date"):d}")
Next

Thank you so much it is working fine

Hi small doubt,

What if NA is there in DT colum, it should write NA if is NA in Final Date column

Do you mean to say that you want Final Date to be “NA” if DT is “NA”?

yea right, i have few NA in original file so

Hi @bhanu.priya2

You can do it simply with LINQ Expression, check the below steps,
→ Use the Read range workbook activity to read the excel and store in a datatable called dt.
→ Then use the assign activity to write the below expression,

- Assign -> dt = (From row In dt.AsEnumerable() 
                  Let DateCol = row("Date").ToString() 
                  Let Code = row("Code").ToString() 
                  Let DTValue = row("DT").ToString()
                  Let AddorSubDays = System.Text.RegularExpressions.Regex.Match(DTValue.ToString,"\d+").Value
                  Let FinalDate = DateTime.ParseExact(DateCol, "M/d/yyyy", System.Globalization.CultureInfo.InvariantCulture).AddDays(+CInt(AddorSubDays)).ToString
                  Let FinalizeDate = If(CDate(FinalDate).DayOfWeek.ToString.Equals("Saturday"),CDate(FinalDate).AddDays(+2).ToString("M/d/yyyy"),(If(CDate(FinalDate).DayOfWeek.ToString.Equals("Sunday"),CDate(FinalDate).AddDays(+1).ToString("M/d/yyyy"),CDate(FinalDate).ToString("M/d/yyyy"))))
                  Let Note = If(Code.ToString().Trim.ToLower.Equals("cucum"),"Should exclude only Sat","Should exclude Sat And Sunday") 
                  Select dt.Clone().Rows.add({DateCol, Code, DTValue, FinalizeDate, Note}) 
                          ).CopyToDataTable()

→ Then use the Write range workbook activity to write the dt to the Same excel sheet.

Check the below workflow for better understanding,
Sequence5.xaml (10.0 KB)

Check the below output file, Sheet1 has Input data and Sheet2 has Output data,
DummyData.xlsx (11.3 KB)

Hope it helps!!

could you please provide this solution? i have few values which is NA so if DT is NA i want my final date to be also NA

i have few values NA in DT column, it is throwing an error if NA is der in DT column

If the NA is there in DT column then how many days has to add to the Date column value… @bhanu.priya2

If it is T+3 then we add 3 days what about NA

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.