Forum Discussion

sagdec1212's avatar
sagdec1212
Frequent Visitor
2 years ago
Solved

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!

 

  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi sagdec1212 ,

     

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

     

    1. Merge two tables.

     

    2. Expand Table B.

     

    3. Add a custom column.

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

     

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

     

    5. Delete unnecessary columns.

    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.

     

3 Replies

  • 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.
  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi sagdec1212 ,

     

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

     

    1. Merge two tables.

     

    2. Expand Table B.

     

    3. Add a custom column.

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

     

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

     

    5. Delete unnecessary columns.

    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.

     

    • sagdec1212's avatar
      sagdec1212
      Frequent Visitor

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