Hi! I would like to extract the maximum and the minimum value from one excel but there is no column name for it. Then write the max and min value with the company name on another sheet. Can someone guide me accordingly? Thank you.
Hi @Krystal ,
Could you try unchecking Add Headers and use these two snippets of code?
Min->
Dt.AsEnumerable().Min(Function(m) Convert.ToDouble(m(1).ToString.Trim))
Max →
Dt.AsEnumerable().Max(Function(m) Convert.ToDouble(m(1).ToString.Trim))
Kind Regards,
Ashwin A.K
Hi
Welcome to UiPath forum
I would like to share you a tutorial where we have all functions related to datatable
Hope it would be useful for you
We have included a expression to know the max and min value of a column
Get Maximum value of a column in datatable
Use a assign activity like this
int_max = dt.AsEnumerable().Max(Function(row) cint(row(columnindex)))
(Output - int32 type)
Get Minimum value of a column in datatable
Use a assign activity like this
int_min = dt.AsEnumerable().Min(Function(row) cint(row(columnindex)))
(Output - int32 type)
**Where column index starts from 0 for first column and 1 for second column and it goes in that order
If you don’t have ColumnName we can make use of column index**
For more references
Cheers @Krystal
to combine it:
arrOrderedValues | Int32( ) or Double( ) if needed
=
Dt.AsEnumerable().Select(Function(x) Convert.ToDouble(x(1).toString.Trim)).Orderby(Function (x) x).toArray
we get the min by: arrOrderedValues.First()
we get the max by: arrOrderedValues.Last()
I can the value successfully but how can I write the value and the related company name to another excel sheet?
Fine
I do have another approach since you want the corresponding row as well
- Let’s say you have the datatable named dt
Now use a Build datatable activity and create two columns one with first columnname Name and another ColumnName as value and get the output as Finaldt
Then use a clear datatable activity and pass Finaldt as input
-
Use sort datatable activity and pass dt as input and mention order as ascending to get the minimum value and index as 1 and Get the output as same variable dt
-
Now use a Add Datarow activity and pass Datarow as dt.Rows(0) and in datatable mention as Finaldt
-
Again use a sort datatable activity and pass dt as input and order as descending and index as 1 and get the output as dt
-
Finally use another add Datarow activity where mention Datarow as dt.Rows(0) and datatable as Finaldt
-
This Finaldt will now have both column values with min and max value
Use write range activity and enable add headers and mention the sheetname you want
Cheers @Krystal
Hi @Krystal ,
For the Input data provided, the Output Should be the Min and Max Value rows.
So In short, the Output should contain only two rows.
For this You can follow the Below Steps.
- You have the Datatable read using
Read Range Workbook
Activity, let’s say having the datatable variable namedDT
.(Without Add Headers) - You can then create a Clone of this
DT
, with variable saycloneDT
. Clone can be achieved as below using Assign Activity :
cloneDT = DT.Clone
- Next, We can use two Add Data Row Activity to add the min and max value row. Finding the min and max value rows can be done as below :
DT.AsEnumerable.OrderBy(Function(x)Cdbl(x(1).ToString)).First.ItemArray //Row having Minimum Value
DT.AsEnumerable.OrderBy(Function(x)Cdbl(x(1).ToString)).Last.ItemArray //Row having Maximum value
Using the Add Data Row
Activity twice :
Fine instead of Datarow let’s use ArrayRow
Mention like this in both add Datarow activity
In ArrayRow property
{dt.Rows(0)(0).ToString, dt.Rows(0)(1).ToString}
And in datatable mention as Finaldt
Remove what is mentioned in Datarow
You are almost done
Cheers @Krystal
Thanks for your help!
Thanks a lot.
Glad it got resolved @Krystal
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.