Skip to content

Instantly share code, notes, and snippets.

@cdhunt
Last active July 24, 2016 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdhunt/6632be21a0864b3029ba01801b21cfde to your computer and use it in GitHub Desktop.
Save cdhunt/6632be21a0864b3029ba01801b21cfde to your computer and use it in GitHub Desktop.
Add-Type -Path C:\Users\chris.hunt\Downloads\PivotData\lib\NReco.PivotData.dll
$CSV = gc "C:\Users\chris.hunt\Downloads\PivotData\NReco.PivotData.Examples.CsvDemo\TechCrunchcontinentalUSA.csv" | ConvertFrom-Csv
$aggregators=new-object Collections.ArrayList
$aggregators.Add([NReco.PivotData.SumAggregatorFactory]::new('raisedAmt')) | Out-Null
$aggregators.Add([NReco.PivotData.AverageAggregatorFactory]::new('raisedAmt')) | Out-Null
$aggregators.Add([NReco.PivotData.MaxAggregatorFactory]::new('raisedAmt')) | Out-Null
$caf=New-Object NReco.PivotData.CompositeAggregatorFactory $aggregators
$pd=New-Object NReco.PivotData.PivotData (echo company raisedCurrency),$caf
$data=[Collections.Generic.List[System.Collections.Generic.Dictionary[string,object]]]::new()
$fields = $csv | gm -MemberType Properties | Select -ExpandProperty Name
foreach ($row in $csv) {
foreach ($field in $fields) {
$record = [System.Collections.Generic.Dictionary[string,object]]::new()
$record[$field] = $row.$field
$data.Add($record) | Out-Null
}
}
$pd.ProcessData($data)
$sq = [NReco.PivotData.SliceQuery]::new($pd)
$sq.Dimension('company')
$byCompanyPivotData = $sq.Execute()
$byCompanyPivotData | foreach {
'Company "{0}": ${1:0.#}M' -f $_.Key, $_.Value[0]
}
$byCompanyPivotData = $pd.Slice((echo company), $false)
$byCompanyPivotData.GetDimensionKeys('company')[0] | foreach {
'Company "{0}": ${1:0.#}M' -f $_, $byCompanyPivotData[$_].Value[0]
}
$CSV = gc "C:\Users\chris.hunt\Downloads\PivotData\NReco.PivotData.Examples.CsvDemo\TechCrunchcontinentalUSA.csv" | ConvertFrom-Csv
$groups = $CSV | Group-Object company
$data = foreach ($group in $groups) {
$aggregates = $group.group | Measure-Object -Property raisedAmt -Sum -Average -Maximum
[PSCustomObject]@{Company=$group.Name
AvgRaisedAmt=$aggregates.Average.ToString("C")
TotalRaisedAmt=$aggregates.Sum.ToString("C")
MaxRaisedAmt=$aggregates.Maximum.ToString("C")
Rounds=$aggregates.Count}
}
$data | ft -AutoSize
<# Possible DSL structure
PSelect {
Field: company,
Field: raisedAmt -as AvgRaisedAmt -Average -Unit Currency
Field: raisedAmt -as TotalRaisedAmt -Sum -Unit Currency
Field: raisedAmt -as MaxRaisedAmt -Maximum -Unit Currency
Field: raisedAmt -as Rounds -Count
GroupBy: company
From -CSV "C:\Users\chris.hunt\Downloads\PivotData\NReco.PivotData.Examples.CsvDemo\TechCrunchcontinentalUSA.csv"
}
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment