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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
sagdec1212
New Member

How to merge two tables based on time range

Hi all, I have 2 tables A and B

 

Table A has item name and the imported date:

itemdate
A1/7/2024
A2/7/2024
A1/6/2024
B1/7/2024
B2/7/2024

 

Table B has the valid date and the storage time:

storage_timetime_fromtime_enditem
71/6/202430/6/2024A
31/7/202430/7/2024A
11/1/202412/31/2024B

 

I want to do a mapping which meets the rule that:

 

To check the imported date and see whether the imported date falls in any validity period. For example, If item A was imported on 2/6, then the storage time should be 7.

 

And my idea output would be:

storedatestorage_time
A1/7/20247
A2/7/20247
A1/6/20243
B1/7/20241
B2/7/20241

 

Can I do this in power query editor? Because I need to transform the data further based on the storage_time

 

Thank you very much in advance!

 

1 ACCEPTED SOLUTION
v-kaiyue-msft
Community Support
Community Support

Hi @sagdec1212 ,

 

Thanks for the reply from @bhanu_gautam , please allow me to provide another insight:

 

1. Merge two tables.

vkaiyuemsft_0-1720078330735.png

 

2. Expand Table B.

vkaiyuemsft_1-1720078349774.png

 

3. Add a custom column.

if [date] >= [Table B.time_from] and [date] <= [Table B.time_end] then 1 else 0

vkaiyuemsft_2-1720078359117.png

 

4. Filter the custom column for values ​​1.

vkaiyuemsft_3-1720078385218.png

 

5. Delete unnecessary columns.

vkaiyuemsft_4-1720078395381.png

let
    Source = Table.NestedJoin(#"Table A", {"item"}, #"Table B", {"item"}, "Table B", JoinKind.LeftOuter),
    #"Expanded Table B" = Table.ExpandTableColumn(Source, "Table B", {"storage_time", "time_from", "time_end", "item"}, {"Table B.storage_time", "Table B.time_from", "Table B.time_end", "Table B.item"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Table B", "Custom", each if [date] >= [Table B.time_from] and [date] <= [Table B.time_end] then 1 else 0),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([Custom] = 1)),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Table B.time_from", "Table B.time_end", "Table B.item", "Custom"})
in
    #"Removed Columns"

If your Current Period does not refer to this, please clarify in a follow-up reply.

 

Best Regards,

Clara Gong

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

View solution in original post

3 REPLIES 3
v-kaiyue-msft
Community Support
Community Support

Hi @sagdec1212 ,

 

Thanks for the reply from @bhanu_gautam , please allow me to provide another insight:

 

1. Merge two tables.

vkaiyuemsft_0-1720078330735.png

 

2. Expand Table B.

vkaiyuemsft_1-1720078349774.png

 

3. Add a custom column.

if [date] >= [Table B.time_from] and [date] <= [Table B.time_end] then 1 else 0

vkaiyuemsft_2-1720078359117.png

 

4. Filter the custom column for values ​​1.

vkaiyuemsft_3-1720078385218.png

 

5. Delete unnecessary columns.

vkaiyuemsft_4-1720078395381.png

let
    Source = Table.NestedJoin(#"Table A", {"item"}, #"Table B", {"item"}, "Table B", JoinKind.LeftOuter),
    #"Expanded Table B" = Table.ExpandTableColumn(Source, "Table B", {"storage_time", "time_from", "time_end", "item"}, {"Table B.storage_time", "Table B.time_from", "Table B.time_end", "Table B.item"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Table B", "Custom", each if [date] >= [Table B.time_from] and [date] <= [Table B.time_end] then 1 else 0),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([Custom] = 1)),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Table B.time_from", "Table B.time_end", "Table B.item", "Custom"})
in
    #"Removed Columns"

If your Current Period does not refer to this, please clarify in a follow-up reply.

 

Best Regards,

Clara Gong

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

thank you so much! I found it quite easy and clear!

bhanu_gautam
Super User
Super User

@sagdec1212 , You can try to use below steps 

first convert date and time_from and time_end to date data type

 

Then create a new custom column in table A use m code

m
= Table.AddColumn(#"Changed Type", "CustomJoinKey", each [item] & "|" & [date])

 

Similarly in Table B

     = Table.AddColumn(#"Changed Type", "CustomJoinKey", each [item] & "|" & [time_from] & "|" & [time_end])
 
Then merge both tables using Inner join and Custom join key as column then expand the columns
Filter the rows where the date from Table A falls within the time_from and time_end range from Table B.



Did I answer your question? Mark my post as a solution! And Kudos are appreciated

Proud to be a Super User!





Helpful resources

Announcements
Power BI Carousel June 2024

Power BI Monthly Update - June 2024

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

PBI_Carousel_NL_June

Fabric Community Update - June 2024

Get the latest Fabric updates from Build 2024, key Skills Challenge voucher deadlines, top blogs, forum posts, and product ideas.

Top Solution Authors