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

Relate two tables by a date range.- DirectQuery

I need to relate two tables by a code and a date range. For example: 

 

Jug_0-1675371478918.png

 

where TABLE1.SERVICE_CODE = TABLE2.SERVICE_CODE  and  TABLE1.SERVICE_DATE between TABLE2.START_DATE and TABEL2.END_DATE

 

Table 1 is the fact table that has sales information and has the service code and date of execution of the service. I need to relate to table 2 to bring the description of the service, the relationship must be made through the code and the date range. Table 2 has two date fields, start date and end date, because the same code can reference two different descriptions of the services depending on the date range. The date in table 1 must be between the date range of table 2.

 

Is there any way to relate the tables like this or adjust or add to the import code? I'm using directquery

 

Thanks!

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Jug ,

 

You can create a measure for filtering.

Measure = IF(MAX('TABLE 1'[SERVICE_CODE])=MAX('TABLE 2'[SERVICE_CODE])&&MAX('TABLE 1'[SERVICE_DATE])>=MAX('TABLE 2'[START_DATE])&&MAX('TABLE 1'[SERVICE_DATE])<=MAX('TABLE 2'[END_DATE]),1)

vstephenmsft_0-1675406400247.png

No relationship between tables.

                                                                                                                                                         

Best Regards,

Stephen Tao

 

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
Anonymous
Not applicable

Hi @Jug ,

 

You can create a measure for filtering.

Measure = IF(MAX('TABLE 1'[SERVICE_CODE])=MAX('TABLE 2'[SERVICE_CODE])&&MAX('TABLE 1'[SERVICE_DATE])>=MAX('TABLE 2'[START_DATE])&&MAX('TABLE 1'[SERVICE_DATE])<=MAX('TABLE 2'[END_DATE]),1)

vstephenmsft_0-1675406400247.png

No relationship between tables.

                                                                                                                                                         

Best Regards,

Stephen Tao

 

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

@Jug , a new measure

maxx(filter(Table2, max(Table1[Service_code]) = Table2[Service_code] && max( Table1[service_date] ) >= (Table2[Start_date]) && max(Table1[service_date]) <= max(Table2[Start_date] ) ), Table2[Service Description])

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

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

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

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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