Forum Discussion

adityamalik1234's avatar
adityamalik1234
Frequent Visitor
4 years ago
Solved

Grouping date and time

Hi,

 

I would like to create a calculated column to create batches based on date and time. For example:

 

01/01/2022 11:00:00 AM to 01/02/2022 11:00:00AM Batch1

01/02/2022 11:00:00 AM to 01/03/2022 11:00:00AM Batch2

01/03/2022 11:00:00 AM to 01/04/2022 11:00:00AM Batch3

 

The logic should look for the next available date in the table and assign next batch number for current date to next available date and 24 hours difference.

 

Thanks for any ideas.

  • if you just want to select the dates in your table (May skip Weekends, holidays ect)

     

    Add Conditional Column to table in Power Query:
    = Table.AddColumn(#"Renamed Columns", "Adj Date", each if [Time] >= #time(11, 0, 0) then [Date] else (Date.AddDays([Date],-1)))

    Create a Copy of your Table in Power Query name it Batch
    Remove all Columns except for Adj Date
    Remove duplicates
    add index starting at 1 this will be your batch number

     

    Join to your financials table: [Adj Date] --> Batch Table: [Adj Date]

    change index field to not summarize

7 Replies

  • You might be able use the Day of the month or day of the year for your batch number: 

    *Batch = If(financials[Time] >= Time(11,0,0), Day(financials[Date]), Day(financials[Date] -1))
     
    • kitgo2's avatar
      kitgo2
      Advocate I

      if you just want to select the dates in your table (May skip Weekends, holidays ect)

       

      Add Conditional Column to table in Power Query:
      = Table.AddColumn(#"Renamed Columns", "Adj Date", each if [Time] >= #time(11, 0, 0) then [Date] else (Date.AddDays([Date],-1)))

      Create a Copy of your Table in Power Query name it Batch
      Remove all Columns except for Adj Date
      Remove duplicates
      add index starting at 1 this will be your batch number

       

      Join to your financials table: [Adj Date] --> Batch Table: [Adj Date]

      change index field to not summarize

  • v-yalanwu-msft's avatar
    v-yalanwu-msft
    Community Support

    Hi, adityamalik1234 ;

    Such as kitgo2 said, you could create a column by dax.

    Column = "Batch"&IF(TIME(HOUR( [datetime]),MINUTE( [datetime]),SECOND( [datetime]))<TIME(11,0,0),DAY([datetime])-1,DAY([datetime]))

    The final show:


    Best Regards,
    Community Support Team _ Yalan Wu
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

  • tamerj1's avatar
    tamerj1
    Community Champion

    Hi adityamalik1234 

    you create a table 'Batches' which contains three columns [From], [To] and [Batch]
    then the new calculated column in Table1 would be

    MAXX (

    FILTER ( Batches, Batches [From] <= Table1[Date] && Batches [To] > Table1[Date] ), Batches[Batch] )

    • adityamalik1234's avatar
      adityamalik1234
      Frequent Visitor

      Hi Tamerj,

       

      thanks for your reply. I am looking for help on creating the logic for the Batch number. So the earliest date to next date is batch1 and so on.

  • This is how the data looks like:

    1/1/2022 9:00
    1/1/2022 10:00
    1/1/2022 11:00
    1/1/2022 12:00
    1/1/2022 13:00
    1/1/2022 14:00
    1/1/2022 15:00
    1/1/2022 16:00
    2/1/2022 9:00
    2/1/2022 10:00
    2/1/2022 11:00
    2/1/2022 12:00
    2/1/2022 13:00
    2/1/2022 14:00
    2/1/2022 15:00
    2/1/2022 16:00
    3/1/2022 9:00
    3/1/2022 10:00
    3/1/2022 11:00
    3/1/2022 12:00
    3/1/2022 13:00
    3/1/2022 14:00
    3/1/2022 15:00
    3/1/2022 16:00
    5/1/2022 9:00
    5/1/2022 10:00
    5/1/2022 11:00
    5/1/2022 12:00
    5/1/2022 13:00
    5/1/2022 14:00
    5/1/2022 15:00
    5/1/2022 16:00

     

    The calculated column should result in the following:

    1/1/2022 9:00Batch 0
    1/1/2022 10:00Batch 0
    1/1/2022 11:00Batch 1
    1/1/2022 12:00Batch 1
    1/1/2022 13:00Batch 1
    1/1/2022 14:00Batch 1
    1/1/2022 15:00Batch 1
    1/1/2022 16:00Batch 1
    2/1/2022 9:00Batch 1
    2/1/2022 10:00Batch 1
    2/1/2022 11:00Batch 2
    2/1/2022 12:00Batch 2
    2/1/2022 13:00Batch 2
    2/1/2022 14:00Batch 2
    2/1/2022 15:00Batch 2
    2/1/2022 16:00Batch 2
    3/1/2022 9:00Batch 2
    3/1/2022 10:00Batch 2
    3/1/2022 11:00Batch 3
    3/1/2022 12:00Batch 3
    3/1/2022 13:00Batch 3
    3/1/2022 14:00Batch 3
    3/1/2022 15:00Batch 3
    3/1/2022 16:00Batch 3
    5/1/2022 9:00Batch 3
    5/1/2022 10:00Batch 3
    5/1/2022 11:00Batch 4
    5/1/2022 12:00Batch 4
    5/1/2022 13:00Batch 4
    5/1/2022 14:00Batch 4
    5/1/2022 15:00Batch 4
    5/1/2022 16:00Batch 4

     

    I am wondering if there is any way of doing this through switch or through loop functions?