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

The Power BI DataViz World Championships are on! With four chances to enter, you could win a spot in the LIVE Grand Finale in Las Vegas. Show off your skills.

Reply
GiuPowerBi
Regular Visitor

PowerQuery. Building a FK based on condition in Foreign Table

Hello,

I would like to create a foreign key on my fact table based on conditions on another table.

In particulare need to retrive the key base on a range of date and on another column.

 

Following example hope will clarify:

 

GiuPowerBi_1-1706259629779.png

 

and my resulting table should be:

 

GiuPowerBi_2-1706259681649.png

 

Any help on this?

Thank you

 

 

2 ACCEPTED SOLUTIONS
Anonymous
Not applicable

Hi @GiuPowerBi ,

Based on the information you have provided, you can follow the steps below:

1.Add new column.

Key =
IF (
    'Table'[Type] = "Employee"
        && 'Table'[Date] >= MAX ( 'Table2'[Lower Date] )
        && 'Table'[Date] <= MAX ( 'Table2'[Upper Date] ),
    1,
    IF (
        'Table'[Type] = "Freelance"
            && 'Table'[Date] >= MAX ( 'Table2'[Lower Date] )
            && 'Table'[Date] <= MAX ( 'Table2'[Upper Date] ),
        2,
        IF (
            'Table'[Type] = "Employee"
                && 'Table'[Date] >= MIN ( 'Table2'[Lower Date] )
                && 'Table'[Date] <= MIN ( 'Table2'[Upper Date] ),
            3,
            4
        )
    )
)

 Final output:

vyifanwmsft_0-1706493259500.png

 

How to Get Your Question Answered Quickly - Microsoft Fabric Community

If it does not help, please provide more details with your desired out put and pbix file without privacy information.

 

Best Regards,

Ada Wang

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

Ahmedx
Super User
Super User

pls try code in Power Query

Screenshot_1.png

View solution in original post

5 REPLIES 5
Ahmedx
Super User
Super User

pls try code in Power Query

Screenshot_1.png

to know how to do this watch my video

338.mp4

Anonymous
Not applicable

Hi @GiuPowerBi ,

Based on the information you have provided, you can follow the steps below:

1.Add new column.

Key =
IF (
    'Table'[Type] = "Employee"
        && 'Table'[Date] >= MAX ( 'Table2'[Lower Date] )
        && 'Table'[Date] <= MAX ( 'Table2'[Upper Date] ),
    1,
    IF (
        'Table'[Type] = "Freelance"
            && 'Table'[Date] >= MAX ( 'Table2'[Lower Date] )
            && 'Table'[Date] <= MAX ( 'Table2'[Upper Date] ),
        2,
        IF (
            'Table'[Type] = "Employee"
                && 'Table'[Date] >= MIN ( 'Table2'[Lower Date] )
                && 'Table'[Date] <= MIN ( 'Table2'[Upper Date] ),
            3,
            4
        )
    )
)

 Final output:

vyifanwmsft_0-1706493259500.png

 

How to Get Your Question Answered Quickly - Microsoft Fabric Community

If it does not help, please provide more details with your desired out put and pbix file without privacy information.

 

Best Regards,

Ada Wang

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

PijushRoy
Super User
Super User

Hi @GiuPowerBi 

What is the logic to declare the Foreigh key in your last screenshot




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

Proud to be a Super User!





Hello,

logic is Date should be within other table LowerDate and Upper Date and Type should match.

 

Thank you

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

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

Feb2025 NL Carousel

Fabric Community Update - February 2025

Find out what's new and trending in the Fabric community.