# How Do You Group Pivot Table Fields?

**URL:** <https://forums.powershell.org/t/how-do-you-group-pivot-table-fields/16714>\
**Category:** PowerShell Help\
**Created:** [June 2, 2021, 1:26pm UTC](https://forums.powershell.org/t/how-do-you-group-pivot-table-fields/16714 "2021-06-02T13:26:01Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jason\_Cannon](https://avatars.discourse-cdn.com/v4/letter/j/848f3c/32.png) [@Jason\_Cannon](https://forums.powershell.org/u/Jason_Cannon)\
**Post date:** [June 2, 2021, 1:26pm UTC](https://forums.powershell.org/t/how-do-you-group-pivot-table-fields/16714/1 "2021-06-02T13:26:01Z")

</div>

I am working on a PowerShell script to create pivot tables in Excel, for the first time. I have a field that has dates in it and I want to group on that field in the pivot table on the Days and the Months, but I cannot find how to do that through PowerShell.

```auto
#### Pivot Table
$xlPivotTableVersion15 = 5 # Excel 2013
$xlPivotTableVersion12 = 3 # Excel 2007
$xlPivotTableVersion10 = 1
$xlSum = -4157
$xlAverage = -4106
$xlCount = -4112
$xlRight = -4152
$xlDescending = 2
$xlDatabase = 1 # this just means local sheet data
$xlHidden = 0
$xlRowField = 1
$xlColumnField = 2
$xlPageField = 3
$xlDataField = 4    
$xlDirection = [Microsoft.Office.Interop.Excel.XLDirection]

$range1 = $ExcelWorksheet.Range("C1")
$range1 = $ExcelWorksheet.Range($range1,$range1.End($xlDirection::xlDown))
$range2 = $ExcelWorksheet.Range("M1")
$range2 = $ExcelWorksheet.Range($range2,$range2.End($xlDirection::xlDown))
$selection = $ExcelWorksheet.Range($range1, $range2)

$PivotTable = $ExcelWorkbook.PivotCaches().Create($xlDatabase, $selection, $xlPivotTableVersion15)
$PivotTable.CreatePivotTable("R1C15","Tables1") | Out-Null 

[void]$ExcelWorksheet.Select()
$ExcelWorksheet.Cells.Item(1,15).Select()
$ExcelWorkbook.ShowPivotTableFieldList = $true 

$PivotFields = $ExcelWorksheet.PivotTables("Tables1").PivotFields("ASSIGNEDEMPID")
$PivotFields.Orientation = $xlRowField
#$PivotFields.Orientation = $xlDataField
$PivotFields.Position = 1

$PivotFields = $ExcelWorksheet.PivotTables("Tables1").PivotFields("CREATEDTIME")
$PivotFields.Orientation = $xlRowField
#$PivotFields.Orientation = $xlDataField

$PivotFields = $ExcelWorksheet.PivotTables("Tables1").PivotFields("TELNO")
$PivotFields.Orientation = $xlRowField
$PivotFields.Orientation = $xlDataField

#### Pivot Table - End

```

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [June 3, 2021, 3:28pm UTC](https://forums.powershell.org/t/how-do-you-group-pivot-table-fields/16714/2 "2021-06-03T15:28:22Z")

</div>

Excel is not fun to code for. It uses an old .COM object and as you can tell you have to hard-code variable references that are defined in VBA. Best advice is to record a macro in Excel and do what you are attempting manually in Excel. Review the VBA code and try to replicate that in Powershell.

---

<div class="post-metadata">

**Author:** ![Jason\_Cannon](https://avatars.discourse-cdn.com/v4/letter/j/848f3c/32.png) [@Jason\_Cannon](https://forums.powershell.org/u/Jason_Cannon)\
**Post date:** [June 3, 2021, 7:01pm UTC](https://forums.powershell.org/t/how-do-you-group-pivot-table-fields/16714/3 "2021-06-03T19:01:58Z")

</div>

Okay. I will give that a try. I am not the best at reading VBA code so sure to have more questions.

Thanks.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 3, 2021, 8:08pm UTC](https://forums.powershell.org/t/how-do-you-group-pivot-table-fields/16714/4 "2021-06-03T20:08:18Z")

</div>

I don’t have experiences with it and I even don’t know if it has the needed function but I think it’s worth trying. You could take a look at the module from [Doug Finke](https://github.com/dfinke)  
[ImportExcel](https://www.powershellgallery.com/packages/ImportExcel/). I’ve heard it’s lacking proper documentation but not functions. 😉

---

<div class="post-metadata">

**Author:** ![Jason\_Cannon](https://avatars.discourse-cdn.com/v4/letter/j/848f3c/32.png) [@Jason\_Cannon](https://forums.powershell.org/u/Jason_Cannon)\
**Post date:** [June 4, 2021, 11:11am UTC](https://forums.powershell.org/t/how-do-you-group-pivot-table-fields/16714/5 "2021-06-04T11:11:25Z")

</div>

Thanks. I have checked that out, briefly, and I couldn’t find out how to do what I am looking for.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 4, 2021, 11:55am UTC](https://forums.powershell.org/t/how-do-you-group-pivot-table-fields/16714/6 "2021-06-04T11:55:09Z")

</div>

Hmmm … that’s a pity. I’m sorry.

If you haven’t done yet you still can try other forums like [StackOverflow](https://stackoverflow.com/questions/tagged/powershell) or [Reddit](https://www.reddit.com/search/?q=powershell) or [Microsoft](https://answers.microsoft.com/en-us/).

---

<div class="post-metadata">

**Author:** ![Jason\_Cannon](https://avatars.discourse-cdn.com/v4/letter/j/848f3c/32.png) [@Jason\_Cannon](https://forums.powershell.org/u/Jason_Cannon)\
**Post date:** [June 4, 2021, 12:06pm UTC](https://forums.powershell.org/t/how-do-you-group-pivot-table-fields/16714/7 "2021-06-04T12:06:26Z")

</div>

With Rob’s advice (thanks), and help from this page:

> <https://stackoverflow.com/questions/26063633/converting-pivot-code-from-vba-to-powershell>

I was able to get the grouping to work.

This works:

> $PivotGroupRange = $Excel.Range(“O3”)  
> $periods = @($false, $false, $false, $true, $false, $false, $false)  
> $PivotGroupRange.Group($true, $true, [Type]::Missing, $periods)

Seems you specify the cell you want to group, that’s the “O3”, and then set the grouping with the $false’s and $true’s. The Day is the fourth “period” set to $true.

Thanks all.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 4, 2021, 12:23pm UTC](https://forums.powershell.org/t/how-do-you-group-pivot-table-fields/16714/8 "2021-06-04T12:23:46Z")

</div>

Cool.

Thanks for sharing. 👍🏽

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:24pm UTC](https://forums.powershell.org/t/how-do-you-group-pivot-table-fields/16714/9 "2024-05-16T20:24:20Z")

</div>

Closed on May 16, 2024, 8:24pm UTC


