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

A new Data Days event is coming soon! This time we’re going bigger than ever. Fabric, Power BI, SQL, AI and more. Don't miss out.

Reply
Anonymous
Not applicable

conditional relationship between tables

Hi, I need to create a complex relationship between two tables, AGGR and OFF based on dates. Basically the relationship between AGGR[COD_PROM] and OFF[codprom] should only occur if AGGR[date]>= OFF[startdate] and AGGR[date]<=OFF[enddate].

 

userpien_1-1717487356420.png

 

 

i want that relationship between on columns, as keys, cod_prom and codprom works ONLY if a date in AGGR is on range of startdate and end date of table OFF. Thanks!

1 ACCEPTED SOLUTION
Alex87
Solution Sage
Solution Sage

Hello,

In terms of modelling, I am using a simple star schema with 1 to many relationship between a dimension table that contains Cod_Prom - you can create this table by duplicating one of the tables and keeping only this column without duplicates

Alex87_0-1717489345879.png

 

in terms of measure you can create the following:

Solution = 
IF(
    MAX(AGGR[date]) >= MAX(OFF[startdate]) && MAX(AGGR[date]) <= MAX(OFF[enddate]), 
        CALCULATE(
            SELECTEDVALUE(AGGR[date]),
            TREATAS(VALUES(AGGR[cod_prom]), OFF[Codprom])))

the result is the following (cod prom coming from the dimension and the measure created above)

 

Alex87_1-1717489398975.png

I tested the scenario when the condion is not met. The date will not appear as shown below

Alex87_2-1717489558800.png

If it answers your question, please mark my reply as the solution. Thanks!




Did I answer your question? Mark my post as a solution!
Appreciate your Like/Kudos

Proud to be a Super User!




View solution in original post

3 REPLIES 3
Alex87
Solution Sage
Solution Sage

Hello,

In terms of modelling, I am using a simple star schema with 1 to many relationship between a dimension table that contains Cod_Prom - you can create this table by duplicating one of the tables and keeping only this column without duplicates

Alex87_0-1717489345879.png

 

in terms of measure you can create the following:

Solution = 
IF(
    MAX(AGGR[date]) >= MAX(OFF[startdate]) && MAX(AGGR[date]) <= MAX(OFF[enddate]), 
        CALCULATE(
            SELECTEDVALUE(AGGR[date]),
            TREATAS(VALUES(AGGR[cod_prom]), OFF[Codprom])))

the result is the following (cod prom coming from the dimension and the measure created above)

 

Alex87_1-1717489398975.png

I tested the scenario when the condion is not met. The date will not appear as shown below

Alex87_2-1717489558800.png

If it answers your question, please mark my reply as the solution. Thanks!




Did I answer your question? Mark my post as a solution!
Appreciate your Like/Kudos

Proud to be a Super User!




danextian
Super User
Super User

Hi @Anonymous 

 

While you can drag a column from a table onto a column from another table and that will most likely create a relationship, in your case using relationships won't give the correct result. There is not such thing as conditional relationship in the context of Power BI. What you can do is create a calculated column to  retrieve the matching value from another table based on certain conditions and use that in a relationship or create calcualted columns/measures for all the values you want to retrieve from a disconnected table.  Try this as a calc column in OFF

Calc Date =
CALCULATE (
    MAX ( AGGR[date] ),
    FILTER (
        ALL ( AGGR ),
        AGGR[date] <= EARLIER ( off[startdate] )
            && AGGR[date] >= EARLIER ( off[startdate] )
    )
)




Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.
Anonymous
Not applicable

I added this column to the OFF table. Now should I link this OFF table to the AGGR table using the cod_prom and codprom columns? Because all the other information I need is in the AGGR table.

Helpful resources

Announcements
May Power BI Update Carousel

Power BI Monthly Update - May 2026

Check out the May 2026 Power BI update to learn about new features.

Fabric SQL PBI Data Days

Data Days 2026 coming soon!

Sign up to receive a private message when registration opens and key events begin.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.