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

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
1 ACCEPTED SOLUTION
v-henryk-mstf
Community Support
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"

v-henryk-mstf_0-1610711314459.jpeg

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

v-henryk-mstf_1-1610711321383.png


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.

View solution in original post

2 REPLIES 2
v-henryk-mstf
Community Support
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"

v-henryk-mstf_0-1610711314459.jpeg

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

v-henryk-mstf_1-1610711321383.png


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.

amitchandak
Super User
Super User

@NeroTolentino , Create a new column in table 1.

Date 2 = minx(filter(Table1, [Date] >earlier([date] )), [Date])

 

Create a new table, Make sure column name are not same  , if need use selectcolumns to rename columns

new table = filter(crossjoin(Table1,table2), table2[Calendar] >= Table1[Date] && table2[Calendar] < Table1[Date 2])

 

also refer: https://www.sqlbi.com/articles/from-sql-to-dax-joining-tables/

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.