Sum of columns

I have a datatable with five six columns,

i want to find the sum of each column separately, in the end i need to have 6 sum values. whats the best approach for this? any particular activity in ui path to find sum>?

Current,First,Second,Third,Fourth,Fifth
228.29,
106.32,
10882.99,
50160,

the above is the data table

Hi @shreyaank

You could use this Activity pack:
image

It contains an activity that does that:

You can also search our forum for summing up a column that involves some coding.

1 Like

Hi,

Thank you, I have a a query, suppose i want the sum of first column in my datatable,

Aggregate column should be the name of the column for which i want the sum right?

Group by columns- what values to be entered here??

Yes, it looks like for simple sum you would type in the columnName as string.

As far as the GroupByColumns function goes and other options, I hope the thread of the custom activity will be of help:

1 Like

It just says Groupby columns- It allows more columns . Separated by comma(,) symbol.

I just want the sum of one column at once. What value to be inputted in that case

@balupad14 can you please help? I just want the sum of each column separately, my dt has 6 columns

I was clearly not paying attention. Please use this activity instead:
image

I suppose you will need 1 activity per column.

it is taking the values , but there is error thrown in each, Ive pasted the error below

“message”: “Sum : 50160 is invalid Column”,
“level”: “Error”,
“logType”: “Default”,

@shreyaank

Let us take you are having a datatable dta. with Six Columns.
Create one Array variable of type string.
Let us take it as

Arr Sum= (From p In (From q In dta.Columns.Cast(Of System.Data.DataColumn
                                                             Select q.ColumnName.ToString)
               Select Convert.ToString((dta.Select().Select(Function(x) x(Convert.ToInt32(p.ToString)))).Sum)).ToArray

Now the first Element of the array contains the Sum of First Column, Second Element Contains the Sum of Second Column Like that.

I have not tested the code :slightly_smiling_face:

Regards,
Mahesh

1 Like

Hi Mahesh,

Can you please share the XAML? p and q to be declared? ColumnName should be my datatable column name right?

No You just use the query in Assign Activity.

Regards,
Mahesh

Sum array = (From p In (From q In CustomisedCurrentDT.Columns.Cast(Of System.Data.DataColumn
Select q.ColumnName.ToString)
Select Convert.ToString((CustomisedCurrentDT.Select().Select(Function(x) x(Convert.ToInt32(p.ToString)))).Sum)).ToArray

so will i get the sum of all columns??

Yes You will get the Sum of First Column in Zero Index , 2nd Column Sum in 1st Index and So On

Regards,
Mahesh

(From p In (From q In CustomisedCurrentDT.Columns.Cast(Of System.Data.DataColumn
Select q.ColumnName.ToString)
Select Convert.ToString((CustomisedCurrentDT.Select().Select(Function(x) x(Convert.ToInt32(p.ToString)))).Sum)).ToArray

Im getting error, end of expression expected)

Try with this

Arr Sum= (From p In (From q In dta.Columns.Cast(Of System.Data.DataColumn
                                                             Select Convert.ToString(q.ColumnName))
               Select Convert.ToString((dta.Select().Select(Function(x) Convert.ToInt32(x(p.ToString)))).Sum)).ToArray

Now this will Work

Regards,
Mahesh

HI @shreyaank,

try below code.
Convert.ToString(dt.Compute("Sum(Current)", string.Empty))

Regards,
Arivu

Hi Arivu,

getting error- invalid use of aggregate function Sum() and Type:String

(From p In (From q In CustomisedCurrentDT.Columns.Cast(Of System.Data.DataColumn Select Convert.ToString(q.ColumnName))
Select Convert.ToString((CustomisedCurrentDT.Select().Select(Function(x) Convert.ToInt32(x(p.ToString)))).Sum)).ToArray

same error again- end of expression )expected

Hi @shreyaank,

Try this code

Convert.ToString(dt.AsEnumerable().Sum(Function(row) Convert.ToDouble(row("Current"))))

Regards,
Arivu

Hi Arivu,

getting the below error

{
“message”: “Assign : Object cannot be cast from DBNull to other types.”,
“level”: “Error”,
"