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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
MrMP
Helper III
Helper III

Linking tables based on between date

Hi everyone,

 

I am trying to link two tables with relationship. Table 1 should be connected using the Date and Product with table 2 where I have Product ID and between dates. So if Product ID matches and date is in between those two dates, it should create a relationship. Is there any good way to create this rather then writing DAX in table 1 since tables are quite large and might slow down the model.

Can Power Query perform faster?

Example:

-table 1

DateProduct IDSales
05-05-2021P150
06-06-2021P175
07-07-2021P110

 

-table 2

Product IDDate fromDate toAttribute
P101-01-202131-10-2021A1
P201-01-202131-12-2021A2

 

 

Thank you!

1 ACCEPTED SOLUTION

I have managed to solve it with dax, and it calculates it in like 10sec. Doesnt look nice, but it does the trick.

Calculated Column =
CALCULATE (
    FIRSTNONBLANK ( tab2[Attribute], tab2[Attribute] ),
    FILTER (
        tab2,
        tab1[Product ID] = tab2[Product ID]
            && tab1[Date] >= tab2[DateFrom]
            && tab2[Date] <= tab2[Date To]
    )
)

  

View solution in original post

13 REPLIES 13
v-xiaotang
Community Support
Community Support

Hi @MrMP 

M code:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDVBSIjAyNDJR2lABBhaqAUqwOUMNMFImQJc1OIhLkuECFLGAJ1xAIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, #"Product ID" = _t, Sales = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Product ID", type text}, {"Sales", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type","test",each Table.SelectRows(Table2,(tb2) => tb2[Date from]<= _[Date] and tb2[Date to] >= _[Date] and tb2[Product ID]=_[Product ID])),
    #"Expanded test" = Table.ExpandTableColumn(#"Added Custom", "test", {"Attribute"}, {"test.Attribute"})
in
    #"Expanded test"

vxiaotang_0-1640247741077.png

 

Best Regards,

Community Support Team _Tang

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

Thank you for your time. I have tried it and it still loads for a very long time. I have no idea how to proceed. Maybe move this to SQL before it enters power query.

Hi @MrMP 

If you move it to SQL before ti enters power query, will it help? 

 

Best Regards,

Community Support Team _Tang

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

I have managed to solve it with dax, and it calculates it in like 10sec. Doesnt look nice, but it does the trick.

Calculated Column =
CALCULATE (
    FIRSTNONBLANK ( tab2[Attribute], tab2[Attribute] ),
    FILTER (
        tab2,
        tab1[Product ID] = tab2[Product ID]
            && tab1[Date] >= tab2[DateFrom]
            && tab2[Date] <= tab2[Date To]
    )
)

  

smpa01
Super User
Super User

@MrMP  can you use this measure

 

 

Measure =
CALCULATE (
    MAX ( Table2[Attribute] ),
    DATESBETWEEN (
        Table1[Date],
        MAX ( Table2[Date from] ),
        MAX ( Table2[Date to] )
    )
)

 

smpa01_0-1640024313581.png

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

Thank you. I also have to add table1.product id = table2.product id there somewhere and that is what I can solve now

Should stay calculated column as it will be used for slicing later on.

@MrMP  use this if you need a measure

Measure = 
CALCULATE (
    MAX ( Table2[Attribute] ),
    DATESBETWEEN (
        Table1[Date],
        MAX ( Table2[Date from] ),
        MAX ( Table2[Date to] )
    ),FILTER(Table2,Table2[Product ID]=MAX(Table1[Product ID]))
)

 

smpa01_0-1640144913075.png

 

use this if you need a calculated column

Column =
CALCULATE (
    MAXX (
        FILTER (
            CROSSJOIN (
                Table1,
                SELECTCOLUMNS (
                    Table2,
                    "id", Table2[Product ID],
                    "from", Table2[Date from],
                    "to", Table2[Date to],
                    "attribute", Table2[Attribute]
                )
            ),
            [Product ID] = [id]
                && [Date] >= [from]
                && [Date] <= [to]
        ),
        [attribute]
    )
)

 

smpa01_1-1640144978360.png

 

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

Thank you. I still have problem with calculated column since it has been calculating for 15 mins now and I have to find another way.

@MrMP  can you please try this to see if it is any better

 

Column =
CALCULATE (
    MAXX (
        FILTER (
            CROSSJOIN (
                SUMMARIZE ( Table1, Table1[Date], Table1[Product ID] ),
                SELECTCOLUMNS (
                    Table2,
                    "id", Table2[Product ID],
                    "from", Table2[Date from],
                    "to", Table2[Date to],
                    "attribute", Table2[Attribute]
                )
            ),
            [Product ID] = [id]
                && [Date] >= [from]
                && [Date] <= [to]
        ),
        [attribute]
    )
)
Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

Unfortunately no. It has been calculating for last 30 mins.

amitchandak
Super User
Super User

@MrMP , if you only need attributes from table 2, then try a new column in table 1. It should support this on table with few million rows

 

New column in table 1 = maxx(filter(Table2, Table1[date] >= table2[from Date] && Table1[Date] <= Table2[To date]) , Table2[Attribute])

 

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Thank you for help. I had to add table1.product id = table2.product id also there, but it keeps calculating for last 10mins. table 1 10mil rows, table 2 1mil rows.

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors