Summarizing excel

Hello guys i have a excel.

Here i want to take first data from Final column and ie, “MOT_2024_ON” and “Approval date”
column ie "6/30/2024.

Now i want to calculate the total “GROSS PREMIUM” for the repeating “MOT_2024_ON” and “Approval date” value and code is done given below

Now it should go to the second row but the second row is same means “FINAL” and “APPROVAL DATE” is same as that of first row so it should skip

Then it should go to the 3rd row and “ECAR_2024_PR” and “6/30/2024” and should calculate the total Gross Premium Value.

and finally it should write the data in a new sheet given below like a summary

image

in general it is a groupBy Case and is aggregating the gross premium

Hi @Gokul_Murali ,

Complete Workflow

  1. Read the Excel Data:
Excel Application Scope - ExcelFilePath
    Read Range - "Sheet1" - Output: dt
  1. Process the Data:
Assign - dict = New Dictionary(Of String, Double)
For Each row In dt.Rows
    Assign - key = row("FINAL").ToString + "_" + row("Approval date").ToString
    If dict.ContainsKey(key)
        Assign - dict(key) = dict(key) + Convert.ToDouble(row("GROSS PREMIUM"))
    Else
        Assign - dict(key) = Convert.ToDouble(row("GROSS PREMIUM"))
    End If
Next
  1. Prepare Summary Data:
Build Data Table - summaryDataTable
    Columns: {"FINAL", "Approval Date", "Total Gross Premium"}

For Each kvp In dict
    Assign - parts = kvp.Key.Split("_"c)
    Assign - final = parts(0)
    Assign - approvalDate = parts(1)
    Add Data Row - ArrayRow: {final, approvalDate, kvp.Value} - DataTable: summaryDataTable
Next
  1. Write the Summary Data to a New Sheet:
Excel Application Scope - ExcelFilePath
    Write Range - "Sheet2" - summaryDataTable

This process will read the data, calculate the total “GROSS PREMIUM” for each unique combination of “FINAL” and “Approval Date”, and write the summarized data to a new sheet in the Excel file.

@sandyarpa767

Hi Sandya,

Iam getting an error like this,

@sandyarpa767

Can you help me out on this,

The error “Option Strict On disallows late binding” occurs because Option Strict enforces early binding, meaning that all variable types must be known at compile time. In your case, row("FINAL") and row("APPROVAL DATE") are likely being treated as Object types, which Option Strict does not allow to be used directly without explicit casting

Updated UiPath Workflow:

  1. Assign Activities:
  • Add separate Assign activities to cast the values before combining them.
  1. Assign Values:
  • finalValue = row("FINAL").ToString()
  • approvalDateValue = row("APPROVAL DATE").ToString()
  1. Combine Values:
  • Key = finalValue & "_" & approvalDateValue

Find some starter help:

Build DataTable and configure the three output columns

  • FINAL, APPROVAL DATE, GROSS PREMIUM
    • out: dtResult

Assign Activity:
dtResult =

(From d in dtDataTableVar.AsEnumerable
Group d by k1=d("FINAL").toString.Trim into grp=Group
Let fs = grp.Sum(Function (g) CDbl(g("GROSS PREMIUM").toString.Trim))
Let ra = new Object(){k1, g.First()("APPROVAL DATE"), fs}
Select r = dtResult.Rows.Add(ra)).CopyToDataTable

feel free to adapt conversions and extending the grouping to also the Approval Date

@sandyarpa767

Now error in this assign activity.

am i given the argument type Correctly


e

Kindly note that we shared with you a more compact alternate

when working with the dictionary then adapt the type argument

when dictionary is of datatype: Dictionary(Of String, Double)
then type argument is a KeyValuePair of String, Double

@ppr

The error “Option Strict On disallows late binding” occurs because Option Strict requires that the types be known at compile time. In this case, kvp.key is being treated as an Object, and Option Strict does not allow implicit conversions. To resolve this, you need to explicitly cast kvp.key to a String.

@sandyarpa767

What should i change in the code, can you mention that

no was not mentioned as key value pair was highlighted:
grafik

@ppr

What should be the datatype for Parts now i have given as string.

That one is done

Parts since u are splitting please use as array/collection datatype

@sandyarpa767

as 2 different approaches were shared with you, so decide on which one you want to implement first. Then be strict on this line and have an understanding on what is aimed.

we answered with:

he error message indicates that key is not a member of System.Collections.Generic.Dictionary(Of String, Double). This suggests that your dictionary’s keys and values are not being accessed correctly in your code.

To resolve this, you need to properly access the key-value pairs in your dictionary. Here’s a detailed explanation of how to do this correctly:

Correct Approach for Accessing Dictionary Elements

  1. Ensure Proper Typing:
  • Your dictionary dict should be of type Dictionary(Of String, Double).
  • In your For Each loop, the variable kvp should be of type KeyValuePair(Of String, Double).
  1. Accessing Key and Value:
  • Use kvp.Key to access the key.
  • Use kvp.Value to access the value.
  1. String Operations:
  • Ensure that any operations on kvp.Key or kvp.Value are compatible with their

Dictionary value

image

For each argument type is also given as Key value pair

@sandyarpa767

Sharing my test sample

Test.zip (4.3 KB)