Find Second Maximum value From DataTable in UiPath

Hi Welcome to another post of Codehunters.

In this post we will try to solve a very common problem of finding the second largest number from a column, this is also the most asked interview question in many interviews.

For this we have a excel sheet with a Column of Age and we want to find 2nd Highest value from this, you can in fact find 2nd highest 3rd highest or any rank value from list using this method.

Launch UiPath an open a Blank Process, Now Drag and Drop the read range activity to read the excel sheet and hold the value in a data table. Select the File. The Sheet Name in our case is Sheet1, The range is all means blank. The Output we will hold in a new Variable dtExcelData you can press Ctrl + K to create a new Variable. It should look like below Screenshot\

So we have the Excel data in the datatable. Now Next thing we want to do is to sort the datatable so lets drag and drop the Sort Datatable Activity Here First is the Column Name that we want to Sort so the column name is dtExcelData.Columns(“Age”) Next is the DataTable name Our Datatable is DtExcelData.

Next two we can keep blank as we are not using Index Column Name, the Output of this will be another dataTable we will call it as dtSortedData. And Type will be Ascending. Check Below screenshot for Sort DataTable Activity.

Now Drag an Assign activity and Create a Variable called SecondLargest and assign it to below expression


Here the -2 is the Second Highest, if you want 3rd Highest write -3 and so on and so forth.

The complete code should look like below screenshot

Now Run this process and see the result. The Output is same as in Our Excel it is the Second Highest now lets change the value of Assign Activity’s expression to -3 to get the third highest and run again.

So this is how you can get the second maximum number from a list in UiPath. T

Hope this helps and I will see you in the next video. Till then Code With Confidence.

you're currently offline