Forum Discussion

NeroTolentino's avatar
NeroTolentino
Frequent Visitor
5 years ago
Solved

HELP | Joining Two Tables

Hello,

 

I'm trying to join these two tables (Table 1 and Table 2). I've tried joining (full outer) and also tried the EARLIER function but I can't get the desired result (Result Table). Can you shed some ideas on how to go around this? Thanks!

 

Table 1

DateProductCategoryCount
12/1/2020XQNWA72
12/5/2020XQNWA83
12/4/2020YFLAEN25
12/8/2020YFLAEN31
12/3/2020XQNBAR4

 

Table 2

Calendar
12/1/2020
12/2/2020
12/3/2020
12/4/2020
12/5/2020
12/6/2020
12/7/2020
12/8/2020
12/9/2020
12/10/2020

 

Result:

DateProductCategoryCount
12/1/2020XQNWA72
12/2/2020XQNWA72
12/3/2020XQNWA72
12/4/2020XQNWA72
12/5/2020XQNWA83
12/6/2020XQNWA83
12/7/2020XQNWA83
12/8/2020XQNWA83
12/9/2020XQNWA83
12/10/2020XQNWA83
12/4/2020YFLAEN25
12/5/2020YFLAEN25
12/6/2020YFLAEN25
12/7/2020YFLAEN25
12/8/2020YFLAEN31
12/9/2020YFLAEN31
12/10/2020YFLAEN31
12/3/2020XQNBAR4
12/4/2020XQNBAR4
12/5/2020XQNBAR4
12/6/2020XQNBAR4
12/7/2020XQNBAR4
12/8/2020XQNBAR4
12/9/2020XQNBAR4
12/10/2020XQNBAR4
  • Hi NeroTolentino ,


    According to the data you provided, I did the following test in power query:

    let
        Source = #"Table 1",
        #"Added Custom2" = Table.AddColumn(Source, "Custom", each let 
    maxdate=Table.Max(
        Table.SelectRows(Source,(x)=>x[Product]=[Product] and x[Category]=[Category]),"Date"
    )[Date],
    nextdate=try
    Table.Min(
        Table.SelectRows(Source,(x)=>x[Product]=[Product] and x[Category]=[Category] and x[Date]>[Date]),"Date"
    )[Date]
    otherwise
    null
    in 
    if [Date]=maxdate 
    then 
    List.Generate(
        ()=>[Date],
        each _<=List.Max(#"Table 2"[Calendar]),
        each Date.AddDays(_,1)
    )
    else 
    List.Generate(
        ()=>[Date],
        each _<nextdate,
        each Date.AddDays(_,1)
    )),
        #"Expanded Custom" = Table.ExpandListColumn(#"Added Custom2", "Custom"),
        #"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"Date"}),
        #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"Custom", "Product", "Category", "Count"}),
        #"Renamed Columns" = Table.RenameColumns(#"Reordered Columns",{{"Custom", "Date"}}),
        #"Added Index" = Table.AddIndexColumn(#"Renamed Columns", "Index", 1, 1, Int64.Type)
    in
        #"Added Index"

    Then add the corresponding column to the table in the desktop:


    If the problem is still not resolved, please provide detailed error information and let me know immediately, looking forward to your reply.


    Best Regards,
    Henry


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

2 Replies

  • v-henryk-mstf's avatar
    v-henryk-mstf
    Community Support

    Hi NeroTolentino ,


    According to the data you provided, I did the following test in power query:

    let
        Source = #"Table 1",
        #"Added Custom2" = Table.AddColumn(Source, "Custom", each let 
    maxdate=Table.Max(
        Table.SelectRows(Source,(x)=>x[Product]=[Product] and x[Category]=[Category]),"Date"
    )[Date],
    nextdate=try
    Table.Min(
        Table.SelectRows(Source,(x)=>x[Product]=[Product] and x[Category]=[Category] and x[Date]>[Date]),"Date"
    )[Date]
    otherwise
    null
    in 
    if [Date]=maxdate 
    then 
    List.Generate(
        ()=>[Date],
        each _<=List.Max(#"Table 2"[Calendar]),
        each Date.AddDays(_,1)
    )
    else 
    List.Generate(
        ()=>[Date],
        each _<nextdate,
        each Date.AddDays(_,1)
    )),
        #"Expanded Custom" = Table.ExpandListColumn(#"Added Custom2", "Custom"),
        #"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"Date"}),
        #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"Custom", "Product", "Category", "Count"}),
        #"Renamed Columns" = Table.RenameColumns(#"Reordered Columns",{{"Custom", "Date"}}),
        #"Added Index" = Table.AddIndexColumn(#"Renamed Columns", "Index", 1, 1, Int64.Type)
    in
        #"Added Index"

    Then add the corresponding column to the table in the desktop:


    If the problem is still not resolved, please provide detailed error information and let me know immediately, looking forward to your reply.


    Best Regards,
    Henry


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