anjana.k
(Anjana K)
October 7, 2022, 8:34am
1
I have a datatable mentioned as below
SSN Amount
1 10
1 20
2 10
3 9
I need to find the Max value of “Amount” column with respect to each distinct SSN and output the row to a datatable
expected output
SSN Amount
1 20
2 10
3 9
Hi @anjana.k ,
The case seems to be of Group By and Selecting the Max value out of the Grouped values.
For a Starter, Check the below links on Grouping :
This HowTo introduces on the different options for grouping data from a datatable in order to process the grouped data.
Introduction
Grouping data and processing the grouped data is a common scenario e.g. when the grouped data is to aggregate like summing up, find maximum, get the average or concatening items.
Lets have a look on following data:
[grafik]
A possible scenario could be:
Create a report containing following information:
the region code
the sum of CaseCount per RegionCode
t…
@anjana.k
Welcome to UiPath community
Please refer the below link for your answer
MaxValDT.xaml (7.4 KB) I need to get max value in a column based on Group by of two columns in datatable
From the Source Datatable, I need to get max values of column “Col 3” (found as string in SourceDT) based on Group by columns “Col 1” and “Col 2” in a new datatable. And “Col 4” is misc data.
Please help me to solve this.
What I have:
Source Datatable:
[image]
What I need:
In a New Datatable (DT1):
[image]
Since dealing with datatable with large data, I am trying in LinQ Query instea…
@anjana.k ,
Preparing of the Output datatable would be required, Here since the datatable schema is the same, we can take the clone of the Input datatable.
We can then use the below Expression :
(From d In dtData.AsEnumerable
Group d By k=d("SSN").toString.Trim Into grp=Group
Let value = grp.Max(Function (x) CDbl(x("Amount").toString))
Let ra = New Object(){k, value}
Select dtData_New.Rows.Add(ra)).CopyToDataTable
Here, dtData
is your Input datatable, and dtData_New
is your Output datatable which should be the clone of the input datatable.
Yoichi
(Yoichi)
October 7, 2022, 8:43am
5
Hi,
FYI, another approach:
dt = dt.AsEnumerable.GroupBy(Function(r) r("SSN").ToString).Select(Function(g) g.OrderBy(Function(r) Int32.Parse(r("amount").ToString)).Last()).CopyToDataTable()
Sample20221007-4.zip (2.8 KB)
Regards,
Gokul001
(Gokul Balaji)
October 7, 2022, 8:46am
6
Hi @anjana.k
Check out the Workflow
HigestValue.xaml (7.6 KB)
Output
Regards
Gokul
anjana.k
(Anjana K)
October 7, 2022, 9:02am
7
Hi @Gokul001 ,
I have n number of columns in my real table, and i want all the columns to be present in the output table too
Gokul001
(Gokul Balaji)
October 7, 2022, 9:07am
8
Hi @anjana.k
You can try grp.First()(Column Index)
Regards
Gokul
@anjana.k ,
Have you tried out @Yoichi 's Workflow/Suggestion provided above ?
Let us know if you are facing any difficulties.
1 Like
anjana.k
(Anjana K)
October 7, 2022, 9:14am
10
Hi @Yoichi ,
When i tried giving decimal values , it is throwing an error , also if any empty cells are found then also the bot throws error
@anjana.k Please find the below code
DT.AsEnumerable.GroupBy(Function(r) r(“SSN”).ToString).Select(Function(g) g.OrderBy(Function(r) Double.Parse(“0”&r(“amount”).ToString)).Last()).CopyToDataTable()
1 Like
Yoichi
(Yoichi)
October 7, 2022, 9:18am
12
Hi,
How about the following?
dt = dt.AsEnumerable.GroupBy(Function(r) r("SSN").ToString).Select(Function(g) g.OrderBy(Function(r) if(Double.TryParse(r("amount").ToString,New Double) ,Double.Parse(r("amount").ToString),Double.MinValue)).Last()).CopyToDataTable()
Sample20221007-4v2.zip (2.8 KB)
Regards,
system
(system)
Closed
October 10, 2022, 9:20am
14
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.