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
th036
Frequent Visitor

Merge a table based on half hour time

Hi All,

 

I am new to Power Query dispite. I have created a table breaking a year down to half hourly intervals as follows:

 

01/04/2021 00:00:00
01/04/2021 00:30:00
01/04/2021 01:00:00
01/04/2021 01:30:00

 

etc for the whole year

 

I then have a second table in Excel as follows:

 

NameIDDate/Time EffectiveStatusRate
Machine A7801/03/2021 23:07Defective25
Machine B4301/01/2021 00:00Operational75
Machine B4302/04/2021 15:26Defective75
Machine B4303/04/2021 17:02Operational75
Machine A7801/05/2021 08:00Operational25
Machine A7805/05/2021 17:08Defective25
Machine A7805/05/2021 19:21Operational25
Machine C8710/12/2020 01:30Defective15

 

what i want is to is for each Machine to have its own year table, then assign the status to each half hour as appropriate 

so Machine A output would be as follows:

 

Date/TimeNameStatusRate
01/04/2021 00:00Machine ADefective25
01/04/2021 00:30   
    
01/05/2021 08:00Machine AOperational25
01/05/2021 08:30   
    
05/05/2021 17:00Machine ADefective25
05/05/2021 17:30   
05/05/2021 18:00   
05/05/2021 18:30   
05/05/2021 19:00Machine AOperational25

 

so the 01/04/00 00:00 half hour is populated as defective because that was the status going into the date range, it then changed on 01/05/21 08:00 to operational etc. I would then fill down and could run sums off the table.

 

I have a large number of status changes and could do in VBA with loops and arrays etc but i am trying to do in power query which i have only just become aware of.

 

Any help or guidance would be much appreciated. 

 

1 ACCEPTED SOLUTION

It's working how I intended it to work but feel free to round down instead of up or make whatever other adjustments you need for your particular use case. This is just a piece of the puzzle.

View solution in original post

4 REPLIES 4
AlexisOlson
Super User
Super User

It will probably be useful to add a column to your second table that maps each time to a half-hour bucket. You can then do merge/join/group by operations using this column.

 

There's got to be a better way to discretize datetime but here's one possible method:

Table.AddColumn(previousStepName, "HalfHourBucket", each
    #datetime(
        Date.Year([#"Date/Time Effective"]),
        Date.Month([#"Date/Time Effective"]),
        Date.Day([#"Date/Time Effective"]),
        Time.Hour([#"Date/Time Effective"]),
        Number.RoundUp(Time.Minute([#"Date/Time Effective"])/30) * 30,
        0
    ), type datetime
)

 

Thank you Alexis,

 

Your code gives the half hour rounded down i.e the end of the bucket rather than the start

 

HalfHourBucket
01/03/2021 23:30:00
01/01/2021 00:00:00
02/04/2021 15:30:00
03/04/2021 17:30:00
01/05/2021 08:00:00
05/05/2021 17:30:00
05/05/2021 19:30:00
10/12/2020 01:30:00

 

where the original column starts exactly on the hour or half hour its fine but where it starts after the half hour or hour it rounds to the next half hour. 

 

 
 
 
 
 
 
 

It's working how I intended it to work but feel free to round down instead of up or make whatever other adjustments you need for your particular use case. This is just a piece of the puzzle.

artemus
Employee
Employee

A for loop in power query looks like:

(year as number) as list => List.Generate(() => #date(year, 1, 1), each _ < #date(year + 1, 0, 0), each _ + #duration(0, 0, 30, 0)))

 

For here you can convert to a table if needed.

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.

Top Solution Authors
Top Kudoed Authors