CallConnectRate.psm1
|
Set-StrictMode -Version Latest function ConvertTo-Pct([double]$num, [double]$den) { if ($den -le 0) { return 0.0 } [math]::Round($num / $den * 100, 1, [System.MidpointRounding]::AwayFromZero) } function New-CallRow([string]$Name, [long]$d, [long]$c, [long]$v, [long]$m) { $perMeeting = $null if ($m -gt 0) { $perMeeting = [math]::Round($d / $m, 1, [System.MidpointRounding]::AwayFromZero) } [pscustomobject]@{ Rep = $Name Dials = $d Connects = $c Conversations = $v Meetings = $m ConnectRate = ConvertTo-Pct $c $d ConversationRate = ConvertTo-Pct $v $c MeetingRate = ConvertTo-Pct $m $v DialsPerMeeting = $perMeeting } } function Get-CallConnectRate { <# .SYNOPSIS Cold calling funnel per rep and in total: connect rate, conversation rate, meeting rate and dials per meeting. .DESCRIPTION Reads a CSV with the columns rep, dials, connects, conversations, meetings. Rows for the same rep are added together. Connect rate = connects / dials. Conversation rate = conversations / connects. Meeting rate = meetings / conversations. Dials per meeting = dials / meetings (empty when a rep has no meetings). Rates are percentages with one decimal. A row must narrow: dials >= connects >= conversations >= meetings, otherwise the function throws. The last output row is TOTAL. .EXAMPLE Get-CallConnectRate -Path sample.csv #> [CmdletBinding()] param([Parameter(Mandatory)][string]$Path) if (-not (Test-Path -LiteralPath $Path)) { throw "file not found: $Path" } $rows = @(Import-Csv -LiteralPath $Path) if ($rows.Count -eq 0) { throw 'no data rows' } foreach ($need in 'rep', 'dials', 'connects', 'conversations', 'meetings') { if (-not ($rows[0].PSObject.Properties.Name -contains $need)) { throw "missing column: $need" } } $order = New-Object System.Collections.Generic.List[string] $sum = @{} $line = 1 foreach ($r in $rows) { $line++ $name = "$($r.rep)".Trim() if (-not $name) { throw "line ${line}: rep is empty" } $n = @{} foreach ($f in 'dials', 'connects', 'conversations', 'meetings') { $x = [long]0 if (-not [long]::TryParse("$($r.$f)".Trim(), [ref]$x) -or $x -lt 0) { throw "line ${line}: $f must be a whole number, zero or more (rep $name)" } $n[$f] = $x } if ($n.dials -lt $n.connects -or $n.connects -lt $n.conversations -or $n.conversations -lt $n.meetings) { throw "line ${line}: counts must narrow (dials >= connects >= conversations >= meetings) for rep $name" } if (-not $sum.ContainsKey($name)) { $sum[$name] = @{ d = [long]0; c = [long]0; v = [long]0; m = [long]0 }; $order.Add($name) } $sum[$name].d += $n.dials $sum[$name].c += $n.connects $sum[$name].v += $n.conversations $sum[$name].m += $n.meetings } $td = [long]0; $tc = [long]0; $tv = [long]0; $tm = [long]0 foreach ($name in $order) { $s = $sum[$name] New-CallRow $name $s.d $s.c $s.v $s.m $td += $s.d; $tc += $s.c; $tv += $s.v; $tm += $s.m } New-CallRow 'TOTAL' $td $tc $tv $tm } Export-ModuleMember -Function Get-CallConnectRate |