Compare 2 column values, if both the values are equal, paste that value in third column, if there is a minimum value, paste the min value in third column

  1. There are 2 columns
  2. Compare both the columns, if both values are equal, paste it in third column
  3. If any value is lesser, paste the minimum value in third column

Note: Values are numeric. Attaching the file for reference
Compare_SampleFile.xlsx (9.8 KB)

@ayushi_jain3

For Each row As DataRow In dt1.AsEnumerable
If CInt(row(0).ToString).Equals(CInt(row(1).ToString)) Then
row(2)=“equal”
Else
If CInt(row(0).ToString)<(CInt(row(1).ToString)) Then
row(2)=row(0).ToString
Else
row(2)=row(1).ToString
End If
End If
Next

use this in invoke code	activity

there should not be any empty values in a row

cheers

there are rows where some columns are empty. what do i do in that case?

Hii @ayushi_jain3

Use this Linq Query

DT1.Asenumerable.Intersect(DT2.Asenumerable,system.DataRowComparer.Default) .CopyToDataTable

Cheers…!

Hey @ayushi_jain3 ,

I have replicated your use case and below is the xaml file for your reference
Compare Dt.zip (11.3 KB)

below is the output screenshot
image

Below is the linq

(From row In dt.AsEnumerable
Let column1Value = If(row("Column1") IsNot DBNull.Value AndAlso Not String.IsNullOrEmpty(row("Column1").ToString), CInt(row("Column1")), 0)
Let column2Value = If(row("Column2") IsNot DBNull.Value AndAlso Not String.IsNullOrEmpty(row("Column2").ToString), CInt(row("Column2")), 0)
Let a = Math.Min(column1Value, column2Value)
Select dt.Clone.Rows.Add(column1Value.ToString, column2Value.ToString, a.ToString)).CopyToDataTable

Hope it helps you out!

1 Like

column 12 and 13 are returning 0, whereas it should return the value 2 & 83 respectively. Could you please modify the query?

Based on your original post it looks like the solution above meets your criteria, were you wanting to count 0 as null?

@ayushi_jain3

For Each row As DataRow In dt1.AsEnumerable
Dim value1 As String = row(0).ToString()
Dim value2 As String = row(1).ToString()

If String.IsNullOrEmpty(value1) And Not String.IsNullOrEmpty(value2) Then
    row(2) = value2
ElseIf Not String.IsNullOrEmpty(value1) And String.IsNullOrEmpty(value2) Then
    row(2) = value1
ElseIf Not String.IsNullOrEmpty(value1) And Not String.IsNullOrEmpty(value2) Then
    If CInt(value1) = CInt(value2) Then
        row(2) = "equal"
    ElseIf CInt(value1) < CInt(value2) Then
        row(2) = value1
    Else
        row(2) = value2
    End If
Else
    row(2) = "Both Empty or Null"
End If

Next

hope this helps

1 Like

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