How to sum 2 column considering each date

Hi Team,

Final Date A B
1/29/2021 150000 11.32
1/29/2021 150000 0
2/2/2021 200000 9803.92
2/2/2021 10000 32289.63
2/4/2021 100000 7867
2/4/2021 100000 3921.57
Above is the sample data, here i want to Total A and B for each “Date” and save

i need output like this
Final Date A B
1/29/2021 300000 11.32
2/2/2021 210000 42093.55
2/4/2021 200000 11788.57

Hi @bhanu.priya2

Try this

(From row In DT.AsEnumerable()
                   Group row By finalDate = row.Field(Of DateTime)("Final Date") Into Group
                   Let totalA = Group.Sum(Function(r) r.Field(Of Double)("A"))
                   Let totalB = Group.Sum(Function(r) r.Field(Of Double)("B"))
                   Select DT.Clone.LoadDataRow(New Object() {finalDate, totalA, totalB}, False)
                  ).CopyToDataTable()

Input:

image

Output:

image

Regards,

it is giving me this error

@bhanu.priya2

Try this

(From row In DT.AsEnumerable()
               Group row By finalDate = row.Field(Of DateTime)("Final Date") Into Group
               Let totalA = Group.Sum(Function(r) Convert.ToDouble(r.Field(Of String)("A")))
               Let totalB = Group.Sum(Function(r) Convert.ToDouble(r.Field(Of String)("B")))
               Select DT.Clone.LoadDataRow(New Object() {finalDate, totalA, totalB}, False)
              ).CopyToDataTable()

Regards,

it is giving me this error

@bhanu.priya2

Check your input datatable is passing as null, Can you try to run in debug mode

Regards,

Sample.xlsx (157.0 KB)

This is my input data

@bhanu.priya2

Try this

Sequence13.xaml (16.2 KB)

Output:

Sample (1).xlsx (133.9 KB)

Regards,

This worked thank you

1 Like

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