Add column to GroupNumeric/Date in PivotTables.

This commit is contained in:
James O'Neill
2022-07-14 17:43:55 +01:00
parent 5b4857d7a0
commit 5ab9d6f23f
9 changed files with 110 additions and 2 deletions

View File

@@ -0,0 +1,13 @@
try {Import-Module $PSScriptRoot\..\..\ImportExcel.psd1} catch {throw ; return}
#Get rid of pre-exisiting sheet
$xlSourcefile = "$env:TEMP\ImportExcelExample.xlsx"
Write-Verbose -Verbose -Message "Save location: $xlSourcefile"
Remove-Item $xlSourcefile -ErrorAction Ignore
$PivotTableDefinition = New-PivotTableDefinition -Activate -PivotTableName Points `
-PivotRows Driver -PivotColumns Date -PivotData @{Points = "SUM"} -GroupDateColumn Date -GroupDatePart Years, Months
Import-Csv "$PSScriptRoot\First10Races.csv" |
Select-Object Race, @{n = "Date"; e = {[datetime]::ParseExact($_.date, "dd/MM/yyyy", (Get-Culture))}}, FinishPosition, Driver, GridPosition, Team, Points |
Export-Excel $xlSourcefile -Show -AutoSize -PivotTableDefinition $PivotTableDefinition

View File

@@ -0,0 +1,13 @@
try {Import-Module $PSScriptRoot\..\..\ImportExcel.psd1} catch {throw ; return}
#Get rid of pre-exisiting sheet
$xlSourcefile = "$env:TEMP\ImportExcelExample.xlsx"
Write-Verbose -Verbose -Message "Save location: $xlSourcefile"
Remove-Item $xlSourcefile -ErrorAction Ignore
$PivotTableDefinition = New-PivotTableDefinition -Activate -PivotTableName Places `
-PivotRows Driver -PivotColumns FinishPosition -PivotData @{Date = "Count"} -GroupNumericColumn FinishPosition -GroupNumericMin 1 -GroupNumericMax 25 -GroupNumericInterval 3
Import-Csv "$PSScriptRoot\First10Races.csv" |
Select-Object Race, @{n = "Date"; e = {[datetime]::ParseExact($_.date, "dd/MM/yyyy", (Get-Culture))}}, FinishPosition, Driver, GridPosition, Team, Points |
Export-Excel $xlSourcefile -Show -AutoSize -PivotTableDefinition $PivotTableDefinition

View File

@@ -18,8 +18,10 @@
[String]$PivotTotals = "Both",
[Switch]$NoTotalsInPivot,
[String]$GroupDateRow,
[String]$GroupDateColumn,
[OfficeOpenXml.Table.PivotTable.eDateGroupBy[]]$GroupDatePart,
[String]$GroupNumericRow,
[String]$GroupNumericColumn,
[double]$GroupNumericMin = 0 ,
[double]$GroupNumericMax = [Double]::MaxValue ,
[double]$GroupNumericInterval = 100 ,
@@ -139,11 +141,21 @@
if (-not $r ) {Write-Warning -Message "Could not find a Row field named '$GroupNumericRow'; no numeric grouping will be done."}
else {$r.AddNumericGrouping($GroupNumericMin, $GroupNumericMax, $GroupNumericInterval)}
}
elseif ($GroupNumericColumn) {
$c = $pivotTable.ColumnFields.Where( {$_.name -eq $GroupNumericColumn })
if (-not $c ) {Write-Warning -Message "Could not find a Column field named '$GroupNumericColumn'; no numeric grouping will be done."}
else {$c.AddNumericGrouping($GroupNumericMin, $GroupNumericMax, $GroupNumericInterval)}
}
if ($GroupDateRow -and $PSBoundParameters.ContainsKey("GroupDatePart")) {
$r = $pivotTable.RowFields.Where( {$_.name -eq $GroupDateRow })
if (-not $r ) {Write-Warning -Message "Could not find a Row field named '$GroupDateRow'; no date grouping will be done."}
else {$r.AddDateGrouping($GroupDatePart)}
}
elseif ($GroupDateColumn -and $PSBoundParameters.ContainsKey("GroupDatePart")) {
$c = $pivotTable.ColumnFields.Where( {$_.name -eq $GroupDateColumn })
if (-not $c ) {Write-Warning -Message "Could not find a Column field named '$GroupDateColumn'; no date grouping will be done."}
else {$c.AddDateGrouping($GroupDatePart)}
}
}
catch {Write-Warning -Message "Failed adding PivotTable '$pivotTableName': $_"}
}

View File

@@ -16,8 +16,10 @@ function New-PivotTableDefinition {
[String]$PivotTotals = "Both",
[Switch]$NoTotalsInPivot,
[String]$GroupDateRow,
[String]$GroupDateColumn,
[OfficeOpenXml.Table.PivotTable.eDateGroupBy[]]$GroupDatePart,
[String]$GroupNumericRow,
[String]$GroupNumericColumn,
[double]$GroupNumericMin = 0 ,
[double]$GroupNumericMax = [Double]::MaxValue ,
[double]$GroupNumericInterval = 100 ,

View File

@@ -65,6 +65,11 @@ Plus, wiring the [PowerShell ScriptAnalyzer Excel report](https://github.com/dfi
![](.gitbook/assets/ScriptAnalyzerReport.png)
## What's new for 7.1.4
- Added GroupNumericColumn and GroupDateColumn to New-PivotTableDefinition and Add-PivotTable.
## What's new 7.1.3
- Changed to `ProviderPath`. Thanks [Trevor Walker](https://github.com/sporkabob)

View File

@@ -331,6 +331,22 @@ Accept pipeline input: False
Accept wildcard characters: False
```
### -GroupDateColumn
The name of a Column field which should be grouped by parts of the date/time \(ignored if GroupDateRow is not specified\)
```yaml
Type: String
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### -GroupDatePart
The Part\(s\) of the date to use in the grouping \(ignored if GroupDateRow is not specified\)
@@ -364,6 +380,22 @@ Accept pipeline input: False
Accept wildcard characters: False
```
### -GroupNumericColumn
The name of a Column field which should be grouped by Number \(e.g. 0-99, 100-199, 200-299 \)
```yaml
Type: String
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### -GroupNumericMin
The starting point for grouping

View File

@@ -225,6 +225,22 @@ Accept pipeline input: False
Accept wildcard characters: False
```
### -GroupDateColumn
The name of a column field which should be grouped by parts of the date/time \(ignored if GroupDatePart is not specified\)
```yaml
Type: String
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### -GroupDatePart
The Part\(s\) of the date to use in the grouping \(ignored if GroupDateRow is not specified\)
@@ -234,7 +250,6 @@ Type: eDateGroupBy[]
Parameter Sets: (All)
Aliases:
Accepted values: Years, Quarters, Months, Days, Hours, Minutes, Seconds
Required: False
Position: Named
Default value: None
@@ -250,7 +265,6 @@ The name of a row field which should be grouped by Number \(e.g 0-99, 100-199, 2
Type: String
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: None
@@ -258,6 +272,23 @@ Accept pipeline input: False
Accept wildcard characters: False
```
### -GroupNumericColumn
The name of a column field which should be grouped by Number \(e.g 0-99, 100-199, 200-299 \)
```yaml
Type: String
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### -GroupNumericMin
The starting point for grouping