0

I am trying to aggregate some statistics from my broker. I have raw data in CSV format. Here are several strings from the CSV:

"Date";"OperationType";"InstrumentType";"Payment";"Currency"
"30.07.2021 6:00:00";"Dividend";"Stock";"3,42";"USD"
"30.07.2021 6:00:00";"Dividend";"Stock";"0,16";"USD"
"29.07.2021 13:55:15";"BrokerCommission";"Currency";"-7,32";"RUB"
"29.07.2021 13:55:14";"Buy";"Currency";"-14635,5";"RUB"
"29.07.2021 13:55:13";"PayIn";;"14642,82";"RUB"
"29.07.2021 6:00:00";"Dividend";"Stock";"1,93";"USD"
"29.07.2021 6:00:00";"Dividend";"Stock";"1107";"RUB"
"29.07.2021 6:00:00";"TaxDividend";"Stock";"-144";"RUB"
"28.07.2021 12:13:18";"BrokerCommission";"Currency";"-7,34";"RUB"
"28.07.2021 12:13:17";"Buy";"Currency";"-14683";"RUB"
"28.07.2021 12:13:17";"PayIn";;"14690,35";"RUB"
"28.07.2021 12:12:38";"BrokerCommission";"Stock";"-0,1";"USD"
"28.07.2021 12:12:37";"Buy";"Stock";"-196,71";"USD"
"28.07.2021 7:58:17";"BrokerCommission";"Currency";"-3,68";"RUB"
"28.07.2021 7:58:16";"Buy";"Currency";"-7369,75";"RUB"
"28.07.2021 7:58:15";"PayIn";;"7373,44";"RUB"
"28.07.2021 0:35:08";"BrokerCommission";"Stock";"-0,06";"USD"
"28.07.2021 0:35:07";"Buy";"Stock";"-122,23";"USD"
"28.07.2021 0:34:16";"BrokerCommission";"Stock";"-0,14";"USD"
"28.07.2021 0:34:15";"Buy";"Stock";"-278,92";"USD"
"28.07.2021 0:33:18";"BrokerCommission";"Stock";"-0,07";"USD"
"28.07.2021 0:33:17";"Buy";"Stock";"-142,76";"USD"
"28.07.2021 0:32:31";"BrokerCommission";"Stock";"-0,04";"USD"
"28.07.2021 0:32:30";"Buy";"Stock";"-84,31";"USD"

Here is the code I use for it:

#Initiate arrays
$InputArray = @()
$FinalArray = @()
$GroupedArray = @()

#Create array with source data
$InputArray = Import-CSV "path_to_csv_file\file.csv" -Delimiter ";" -Encoding UTF8 | Where-Object { $_.PSObject.Properties.Value -ne '' }

#Convert strings to appopriate data types
foreach ($Object in $InputArray){
    $Object.Date = [datetime]::parse($Object.Date) | Get-Date -Format "yyyy-MM-dd"
    $Object.Payment = [double]($Object.Payment -replace(',','.'))
}#foreach

#Group objects
$GroupedArray = $InputArray | Group-Object -Property "Date","Currency","OperationType" | `
Select-Object   @{Name='Date'           ;Expression={$_.Values[0]}}, `
                @{Name='Currency'       ;Expression={$_.Values[1]}}, `
                @{Name='OperationType'  ;Expression={$_.Values[2]}}, `
                @{Name='Payment'        ;Expression={($_.Group | Measure-Object 'Payment' -Sum).Sum}} | `
                Group-Object Date

foreach ($Object in $GroupedArray){
    $Date = $Object.Name
    foreach ($Instance in $Object.Group){
        if ($Instance.OperationType = "BrokerCommission" -and $Instance.Currency -eq "RUB"){
            $BrokerComissionRUB = $null
            $BrokerComissionRUB = $Instance.Payment
        }#if
        if ($Instance.OperationType = "BrokerCommission" -and $Instance.Currency -eq "USD"){
            $BrokerComissionUSD = $null
            $BrokerComissionUSD = $Instance.Payment    
        }#If
        if ($Instance.OperationType = "Dividend" -and $Instance.Currency -eq "RUB"){
            $DividendRUB = $null
            $DividendRUB = $Instance.Payment
        }#if
        if ($Instance.OperationType = "Dividend" -and $Instance.Currency -eq "USD"){
            $DividendUSD = $null
            $DividendUSD = $Instance.Payment    
        }#If
        if ($Instance.OperationType = "PayIn" -and $Instance.Currency -eq "RUB"){
            $PayInRUB = $null
            $PayInRUB = $Instance.Payment
        }#if
    }#foreach

    $FinalArray += [PSCustomObject]@{
        "Date" = $Date
        "PayInRUB" = $PayInRUB
        "DividendRUB" = $DividendRUB
        "DividendUSD" = $DividendUSD
        "BrokerComissionRUB" = $BrokerComissionRUB
        "BrokerComissionUSD" = $BrokerComissionUSD
    }
}#foreach

$FinalArray

The problem is that in $InputArray value of, for example $InputArrary[1].OperationType, has STRING format:

$InputArray[1].OperationType | gm
TypeName: System.String

But right after grouping objects with Group-Object, OperationType value type is changed to Boolean:

$GroupedArray[1].Group.operationtype | gm
TypeName: System.Boolean

So because of this strange type transformation the actual data is lost. Nevertheless if I execute a part of a script before foreach cycle, the data type of OperationType is still STRING in $GroupedArray. If I run whole script, it changes to Boolean. I have no idea why is that haapening and how can I avoid this. Could you please advice?

1
  • As an aside: Extending arrays in a loop with += is inefficient, because a new array must be created behind the scenes in every iteration, given that arrays are of fixed size; a much more efficient approach is to use a foreach loop as an expression and let PowerShell itself collect the outputs in an array: [array] $outputs = foreach (...) { ... } - see this answer. In case you need to create arrays manually, e.g to create multiple ones, use an efficiently extensible list type - see here. Commented Nov 14, 2021 at 14:09

1 Answer 1

2

There's no strange transformation, You just have a typo, you need to use -eq and not =

You are actually set the $Instance.OperationType value to "BrokerCommission" and then replace it's value again with the results of $Instance.Currency -eq "RUB" which is a boolean value

Check it yourself in the console, execute this:

$Instance.OperationType = "BrokerCommission" -and $Instance.Currency -eq "RUB"

Results:

$Instance.OperationType
False
Sign up to request clarification or add additional context in comments.

1 Comment

I feel stupid :) It works, thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.