Hi ,
I am using excel file which contains columns city and TxAmount value . I need to pivot the City wise to sum up the TxAmount value. How can achieve this?
Hi @aadhyadas
You can use the Create Pivot Table activity to create the Pivot based on your requirement.
Create Pivot Table activity is the Modern Excel activity, you have to use this activity inside the Use excel file.
To know more about Create Pivot Table activity check the below one,
Hope it helps!!
Hi @aadhyadas ,
Detailed Steps:
Step 1: Read Data from Excel
- Drag an
Excel Application Scopeactivity onto the workflow.
- Set the
FilePathto your Excel file (e.g.,"Input.xlsx").
- Inside the
Excel Application Scope, drag aRead Rangeactivity.
- Set the
SheetNameto the name of the sheet you want to read. - Set
Outputto aDataTablevariable, e.g.,dtInput.
Step 2: Process Data with LINQ
- Add an
Assignactivity to pivot the data.
- Create a new
DataTablevariable, e.g.,dtPivoted. - Use LINQ to group and sum the data. In UiPath, you can use an
Assignactivity to store the results of the LINQ query in aDictionary<string, Decimal>variable.Here’s the LINQ expression you can use:
dtInput.AsEnumerable().
GroupBy(Function(row) row.Field(Of String)("City")).
Select(Function(g) New With {
.State = g.Key,
.TotalCCInvoice = g.Sum(Function(row) row.Field(Of Decimal)("TxAmount"))
}).ToDictionary(Function(x) x.State, Function(x) x.Txamount)
- Convert Dictionary to DataTable:
- Use
Assignactivities to convert the dictionary to aDataTableif needed.Here’s a sample way to convert the dictionary to aDataTable:
dtPivoted = New DataTable()
dtPivoted.Columns.Add("City", GetType(String))
dtPivoted.Columns.Add("TotalTxAmount", GetType(Decimal))
For Each kvp In dictPivotResult
dtPivoted.Rows.Add(kvp.Key, kvp.Value)
Next
Step 3: Write Pivoted Data to Excel
- Drag another
Excel Application Scopeactivity onto the workflow to save the results.
- Set the
FilePathto the location where you want to save the output file.
- Inside the
Excel Application Scope, use theWrite Rangeactivity.
- Set the
DataTableproperty todtPivoted. - Specify the sheet name and range if needed.
Regards
Sandy
it shows unable to convert “” to double
Make sure u dont have null values in the columns
1 Like
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.