How to do a Pivot Table using a Linq Query in UiPath?

Hello Good People,

Am trying to use a linq query to do a pivot table but am getting this error:

Assign: Input array is longer than the number of columns in this table.

This the resource I’ve been trying to follow:

Below is the linq query am trying to use:

(From row In dtBandPushing.AsEnumerable
Group row By Key = New With{
	Key.TERMINAL = row.item("TERMINAL"),
} Into grp = Group
Select dtBandPushing.LoadDataRow(New Object(){

Kindly advise on what I could doing wrong.

Thank you.

Kakooza Allan Klaus

Hi @Kakooza-Allan-Klaus

  • Create a new Data Table Variable - dtBandPushingGrouped

  • Assign : dtBandPushingGrouped = dtBandPushing.Clone

  • Add new data column (object type) to dtBandPushingGrouped using the Add Data Column Activity

  • Assign
    dtBandPushingGouped =

    From row In dtBandPushing.AsEnumerable
    Group row By
    k1 = row("TERMINAL"),
    k2 = row("COMMISSION_EARNED")
    Into grp = Group
    Select dtBandPushingGrouped.Rows.Add({k1, k2, grp.Count()})

Hope it works!

1 Like

Why am I adding this column and what values should I populate there:


That’s the error I get when I try to write range

Hi @Kakooza-Allan-Klaus ,

Kindly share the input excel file and the expected output,
to provide you with a linq query to perform pivot

1 Like

Input_File.xlsx (21.3 KB)

Expected_Output.xlsx (35.6 KB)

Those are the files that you’ve requested for.


Maybe to add more context to this @kumar.varun2’s linq query worked but I don’t know why he appends another column to the DataTable and on writing the data to an excel file I get that error of Failed setting the current sheet to: Export in workbook

Hi @Kakooza-Allan-Klaus

Try this approach



	From row In dt_Data.AsEnumerable
	Group row By k = row("TERMINAL")
	Into grp=Group
	Select dt_Data.LoadDataRow({k, grp.Sum(Function(gr) CDbl(gr("COMMISSION_EARNED")))}, True)

Xaml for reference

PivotTableUsingLINQ.xaml (5.4 KB)

Thanks a lot @kumar.varun2,

This resolved my issue.

And the Failed Setting error was resolved when I used Workbook activities instead of the Excel Application Scope Activities.

And also do you’ve any resource that you could recommend that would help me improve my skill with linq queries.

Hi @Kakooza-Allan-Klaus

The master of LINQ (@ppr ) has created a full catalogue with links to various resources. Please check out.

1 Like

Thanks @kumar.varun2,

I highly appreciate man

@ppr has actually helped resolve a number of bugs here.

Thanks @kumar.varun2 for the solution :clap:


This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.