SignalLift.psm1

Set-StrictMode -Version Latest

function Get-SignalLift {
<#
.SYNOPSIS
Compare meeting rates for accounts with no signal, 1 signal and 2 or more signals.
.DESCRIPTION
The CSV needs a header row with the columns account, signals (a non-negative whole number: how many buying signals the
account showed before outreach, from your own records) and meeting (yes or no, case-insensitive). Header matching is
case-insensitive. Each account goes into one group; the edges are fixed: 'no signal' (0), '1 signal' (1) and
'2 or more signals' (2 or more). Per group: accounts, meetings and meeting rate (meetings / accounts, 1 decimal,
'n/a' when the group has no accounts). Lift = meeting rate of '1 signal' or of '2 or more signals' divided by the meeting
rate of 'no signal', computed from the unrounded rates and shown to 2 decimals; 'n/a' when the group is empty or the
'no signal' rate is 0 or undefined.
The numbers describe your file. They are not proof that signals cause meetings.
Returns one object with Accounts, Meetings, Groups (Group, Accounts, Meetings, MeetingRate, MeetingRateText, Lift,
LiftText) and Report (the text report). Throws on a missing file, a missing column, no data rows, a bad signals or
meeting value (the data row number is named) or a duplicate account (named). Data rows are counted after the header,
starting at 1.
.EXAMPLE
Get-SignalLift -Path sample.csv
#>

    [CmdletBinding()]
    param([Parameter(Mandatory)][string]$Path)

    if (-not (Test-Path -LiteralPath $Path -PathType Leaf)) { throw "file not found: $Path" }
    $inv = [System.Globalization.CultureInfo]::InvariantCulture
    $rows = @(Import-Csv -LiteralPath $Path)
    if ($rows.Count -eq 0) { throw 'CSV has no data rows' }
    $names = @{}
    foreach ($p in $rows[0].PSObject.Properties.Name) { $names[$p.Trim().ToLowerInvariant()] = $p }
    foreach ($need in 'account', 'signals', 'meeting') {
        if (-not $names.ContainsKey($need)) { throw "missing column: $need" }
    }
    $get = {
        param($row, $col)
        $v = $row.PSObject.Properties[$names[$col]].Value
        if ($null -eq $v) { '' } else { ([string]$v).Trim() }
    }

    $labels = 'no signal', '1 signal', '2 or more signals'
    $acc = @(0, 0, 0)
    $mtg = @(0, 0, 0)
    $seen = New-Object 'System.Collections.Generic.HashSet[string]'
    $n = 0
    foreach ($r in $rows) {
        $n++
        $a = & $get $r 'account'
        if ($a -eq '') { throw "row ${n}: empty account" }
        if (-not $seen.Add($a.ToLowerInvariant())) { throw "row ${n}: duplicate account '$a'" }
        $sText = & $get $r 'signals'
        $s = [long]0
        if ($sText -notmatch '^[0-9]{1,15}$' -or -not [long]::TryParse($sText, [ref]$s)) {
            throw "row ${n}: bad signals '$sText' (expected a non-negative whole number)"
        }
        $mText = (& $get $r 'meeting').ToLowerInvariant()
        if ($mText -ne 'yes' -and $mText -ne 'no') { throw "row ${n}: bad meeting '$mText' (expected yes or no)" }
        $g = if ($s -eq 0) { 0 } elseif ($s -eq 1) { 1 } else { 2 }
        $acc[$g]++
        if ($mText -eq 'yes') { $mtg[$g]++ }
    }

    $pct = { param($v) if ($null -eq $v) { 'n/a' } else { ([math]::Round(100.0 * $v, 1, [System.MidpointRounding]::AwayFromZero)).ToString('0.0', $inv) + '%' } }
    $rate = @($null, $null, $null)
    for ($g = 0; $g -lt 3; $g++) { if ($acc[$g] -gt 0) { $rate[$g] = [double]$mtg[$g] / $acc[$g] } }

    $groups = New-Object System.Collections.Generic.List[object]
    for ($g = 0; $g -lt 3; $g++) {
        $lift = $null
        if ($g -gt 0 -and $null -ne $rate[$g] -and $null -ne $rate[0] -and $rate[0] -gt 0) {
            $lift = [math]::Round($rate[$g] / $rate[0], 2, [System.MidpointRounding]::AwayFromZero)
        }
        $liftText = if ($g -eq 0) { 'baseline' } elseif ($null -eq $lift) { 'n/a' } else { $lift.ToString('0.00', $inv) }
        $rv = $null
        if ($null -ne $rate[$g]) { $rv = [math]::Round(100.0 * $rate[$g], 1, [System.MidpointRounding]::AwayFromZero) }
        $groups.Add([pscustomobject]@{
            Group = $labels[$g]; Accounts = $acc[$g]; Meetings = $mtg[$g]
            MeetingRate = $rv; MeetingRateText = (& $pct $rate[$g]); Lift = $lift; LiftText = $liftText
        })
    }

    $totalAcc = $acc[0] + $acc[1] + $acc[2]
    $totalMtg = $mtg[0] + $mtg[1] + $mtg[2]
    $lines = New-Object System.Collections.Generic.List[string]
    $lines.Add("Accounts: $totalAcc")
    $lines.Add("Meetings: $totalMtg")
    $lines.Add('')
    $fmt = '{0,-18} {1,8} {2,8} {3,13} {4,9}'
    $lines.Add(($fmt -f 'group', 'accounts', 'meetings', 'meeting rate', 'lift'))
    foreach ($g in $groups) { $lines.Add(($fmt -f $g.Group, $g.Accounts, $g.Meetings, $g.MeetingRateText, $g.LiftText)) }
    $lines.Add('')
    $lines.Add('Lift = meeting rate of the group / meeting rate of "no signal".')
    $lines.Add('These numbers describe your file. They do not show that signals cause meetings.')

    [pscustomobject]@{
        Accounts = $totalAcc
        Meetings = $totalMtg
        Groups   = $groups.ToArray()
        Report   = ($lines -join [Environment]::NewLine)
    }
}

Export-ModuleMember -Function Get-SignalLift