Filter by Week Incriments

This topic contains 2 replies, has 3 voices, and was last updated by Profile photo of H Man H Man 1 year, 6 months ago.

  • Author
  • #24092
    Profile photo of H Man
    H Man

    I am using import-csv and creating a ps custom object.

    There is a date property.

    I am looking to group all the objects by the Week Begining Date

    id like to use the below approach just not sure how i can include the weekly time span

    heres what i got so far

    $grouped = { 
    Switch ($_.WS) {
    {$_ -ge 300MB} {"Large" ;break}
    {$_ -ge 100MB} {"Medium" ;break}
    {$_ -le 2MB} {"Small" ; break}
    Default { "Normal"}
    } #close switch  
    $MyObject | Sort $Grouped
  • #24102
    Profile photo of Rob Simmers
    Rob Simmers

    First thing to understand is that importing from a CSV imports everything as a string. Depending on the date format, it has to be a sortable date\time.

    PS C:\Users\rsimmers> [Get-Date].Tostring["s"]
    #Not Sortable
    PS C:\Users\rsimmers> [Get-Date].ToShortDateString[]
    #Not Sortable
    PS C:\Users\rsimmers> [Get-Date].ToLongDateString[]
    Thursday, April 9, 2015

    If you want to check the difference between dates or perform any date math, you'll need to convert the string to a datetime data type. As an example, say the date is in this format: 1-10-2015

    PS C:\Users\rsimmers> Get-Date "1-10-2015"
    Saturday, January 10, 2015 12:00:00 AM

    You stated you want to sort it by week beginning, so it sounds like you are trying to sort by the week that something occurred. Unfortunately, there isn't a default property like WeekOfYear like DayOfWeek and other default date properties:

    PS C:\Users\rsimmers> [Get-Date "1-10-2015"] | Get-Member -MemberType Properties
       TypeName: System.DateTime
    Name        MemberType     Definition                                                                                                                                            
    ----        ----------     ----------                                                                                                                                            
    DisplayHint NoteProperty   Microsoft.PowerShell.Commands.DisplayHintType DisplayHint=DateTime                                                                                    
    Date        Property       datetime Date {get;}                                                                                                                                  
    Day         Property       int Day {get;}                                                                                                                                        
    DayOfWeek   Property       System.DayOfWeek DayOfWeek {get;}                                                                                                                     
    DayOfYear   Property       int DayOfYear {get;}                                                                                                                                  
    Hour        Property       int Hour {get;}                                                                                                                                       
    Kind        Property       System.DateTimeKind Kind {get;}                                                                                                                       
    Millisecond Property       int Millisecond {get;}                                                                                                                                
    Minute      Property       int Minute {get;}                                                                                                                                     
    Month       Property       int Month {get;}                                                                                                                                      
    Second      Property       int Second {get;}                                                                                                                                     
    Ticks       Property       long Ticks {get;}                                                                                                                                     
    TimeOfDay   Property       timespan TimeOfDay {get;}                                                                                                                             
    Year        Property       int Year {get;}                                                                                                                                       
    DateTime    ScriptProperty System.Object DateTime {get=if [[] -ieq  "Date"]...

    If you take your CSV and use calculated properties you could get some sortable data to work with:

    $myObject = Import-CSV C:\data.csv  | Select Name, @{Label="Date";Expression{$[Get-Date -Date $_.Date]}}, @{Label="WeekOfYear";Expression{$[Get-Date -Date $_.Date -UFormat %W]}}
    $myObject | Sort-Object -Property WeekOfYear

    You'll need to test that your stored date in the CSV can be parsed by Get-Date. If not, you will need to possibly create a function to parse the date string. I've personally never used the WeekOfYear and the [a href=""] first thread I read[/a] stated that the weeks might start on Wednesday versus Sunday or Monday, so you may want to test and read through this thread.

  • #24116
    Profile photo of H Man
    H Man

    Thank you ! Great Info

You must be logged in to reply to this topic.