Looking for a Powershell way to group multiple dates by week

Viewed 31

I have a CSV file of transactions. I've been looking for a way to group the transactions per week.

I know PowerShell has Group-Object and this works to group everything into individual dates, I'm looking to aggregate things by week.

Thanks in advance!

I'm looking at Transaction Date and wanting to loop over all rows to group things in "7 day" buckets. As an example, from 8/31 to 8/24, then 8/24 to 8/17, and so on.

Sample CSV:

Transaction Date,Posted Date,Card No.,Description,Category,Debit
8/31/2022,9/1/2022,1,Meals on wheels!,a,3.5
8/30/2022,9/1/2022,1,Meals on wheels!,b,3.5
8/30/2022,8/31/2022,1,Meals on wheels!,c,3.5
8/30/2022,8/31/2022,1,Meals on wheels!,a,3.5
8/29/2022,8/31/2022,1,Meals on wheels!,a,3.5
8/29/2022,8/30/2022,1,Meals on wheels!,a,3.5
8/26/2022,8/29/2022,1,Meals on wheels!,b,3.5
8/28/2022,8/29/2022,1,Meals on wheels!,b,3.5
8/27/2022,8/29/2022,1,Meals on wheels!,b,3.5
8/25/2022,8/26/2022,1,Meals on wheels!,b,3.5
8/25/2022,8/26/2022,1,Meals on wheels!,b,3.5
8/23/2022,8/24/2022,1,Meals on wheels!,c,3.5
8/23/2022,8/24/2022,1,Meals on wheels!,c,3.5
8/22/2022,8/23/2022,1,Meals on wheels!,c,3.5
8/21/2022,8/23/2022,1,Meals on wheels!,c,3.5
8/21/2022,8/23/2022,1,Meals on wheels!,a,3.5
8/21/2022,8/22/2022,1,Meals on wheels!,a,3.5
Import-Csv C:\transaction_download.csv | ForEach-Object {

$PSItem.'Transaction Date' | Group-Object

# Here I want to create a group of the dates/transactions +7 days from the first date.

}
1 Answers

Ditto to what 'Abraham Zinala', said as not having it focus us to guess.

Nonetheless, do you mean something like this:

Clear-Host
 @(
'01/01/2022',
'01/01/2022',
'01/02/2022',
'01/03/2022',
'01/03/2022',
'01/03/2022'
) | 
ForEach-Object {((Get-Date($PSItem)).AddDays(7))} | 
Group-Object | 
Select-Object -Property Count, Name

# Results
<#
Count Name              
----- ----              
    2 08-Jan-22 00:00:00
    1 08-Feb-22 00:00:00
    3 08-Mar-22 00:00:00
#>
Related