Forum Discussion

vpirozzi3's avatar
vpirozzi3
New Member
6 years ago
Solved

Creating new column based on foreign key

Hi!

 

I have the following problem. I have 3 tables.

 

SCHOOLS TABLE

ID | Name | Address

 

COURSES TABLE

ID | Name | School_ID

 

CHILDREN TABLE

ID | Name | Course_ID

 

How can I add to the CHILDREN TABLE the School_ID? In other words i need to create a column which looks for the children COURSE_ID, go to the courses table and look for that id and retreive the SCHOOL_ID and add that value to the children table.


Any help would be great!  Thanks in advance

  • Hi vpirozzi3 ,

    There are two situations:

    1. If no ‘Many-to-Many’ relationship among the three tables, you can use ‘Related’ to add a ‘SchoolID’ column in table ‘Children’.

    SchoolID = RELATED('Courses 2'[School_ID])

     

    2. If with ‘Many-to-Many’ relationship among the three tables, you need to create another table.

    Table 1 =
    FILTER (
        CROSSJOIN (
            'Childen 1',
            SELECTCOLUMNS (
                'Courses 1',
                "CourseID", 'Courses 1'[ID],
                "SchoolID", 'Courses 1'[School_ID]
            )
        ),
        [CourseID] = 'Childen 1'[Course_ID]
    )

     

    This is my PBIX file.

     

    Best Regards,

    Icey

     

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

     

2 Replies

  • Icey's avatar
    Icey
    Community Support

    Hi vpirozzi3 ,

    There are two situations:

    1. If no ‘Many-to-Many’ relationship among the three tables, you can use ‘Related’ to add a ‘SchoolID’ column in table ‘Children’.

    SchoolID = RELATED('Courses 2'[School_ID])

     

    2. If with ‘Many-to-Many’ relationship among the three tables, you need to create another table.

    Table 1 =
    FILTER (
        CROSSJOIN (
            'Childen 1',
            SELECTCOLUMNS (
                'Courses 1',
                "CourseID", 'Courses 1'[ID],
                "SchoolID", 'Courses 1'[School_ID]
            )
        ),
        [CourseID] = 'Childen 1'[Course_ID]
    )

     

    This is my PBIX file.

     

    Best Regards,

    Icey

     

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

     

  • Icey's avatar
    Icey
    Community Support

    Hi vpirozzi3 ,

     

    Is this problem sloved?
     
    If it is sloved, could you kindly accept it as a solution to close this case?  
     
    If not, please let me know.  
     
    Best Regards
    Icey