Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
adityamalik1234
Frequent Visitor

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.

1 ACCEPTED SOLUTION

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

View solution in original post

7 REPLIES 7
v-yalanwu-msft
Community Support
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:

vyalanwumsft_0-1655865951818.png


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.

kitgo2
Advocate I
Advocate I

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))
 

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

adityamalik1234
Frequent Visitor

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?

tamerj1
Super User
Super User

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] )

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.

@adityamalik1234 

Ok. Please share some sample data to understand how does it look like. 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.