Search code examples
excelexcel-formulaouter-join

joining two tables with multiple keys in excel?


Table A:

From_Node To_Node Avg_Time_real
1975 3752 4.06
1975 4874 28
1975 4900 23.4
1975 5016 27.4
3752 5949 4
3752 6204 32
3752 6228 16
7100 1004 9
7100 1006 10
4874 1975 29
4874 3752 28

Table B:

From_Node To_Node Avg_Time_Contract
1975 3752 6
1975 4874 27
1975 4900 22
3752 5949 6
3752 6204 7
3752 6228 10
3752 6350 11
115 116 5
115 21469 7
4874 1975 30
4874 3752 31

How below can be performed, as I have data in excel with more than 20,000 rows.

Output:

From_Node To_Node Avg_Time_real Avg_Time_Contract
1975 3752 4.06 6
1975 4874 28 27
1975 4900 23.4 22
1975 5016 27.4 Null
3752 5949 4 6
3752 6204 32 7
3752 6228 16 10
3752 6350 Null 11
7100 1004 9 NUll
7100 1006 10 Null
115 116 Null 5
115 21469 Null 7
4874 1975 29 30
4874 3752 28 31

I tried to remove duplicates from column to node but there are two keys, so unable to achieve the expected output. Can importing this dataset in sql and using outer join will give the expected result? But I am not sure how outer join can be performed using two keys with this case. Any help is appreciated. Thanks


Solution

  • Transform/Join Two Tables Into One

    =LET(rData,A1:C12,cData,E1:G12,na,"Null",dlm,"@",
        rd,DROP(rData,1),ru,TAKE(rd,,1)&dlm&CHOOSECOLS(rd,2),
        cd,DROP(cData,1),cu,TAKE(cd,,1)&dlm&CHOOSECOLS(cd,2),
        cv,TAKE(cData,,-1),
        rui,VSTACK(TAKE(cv,1),IFNA(INDEX(DROP(cv,1),XMATCH(ru,cu)),na)),
        cf,FILTER(cd,ISNA(XMATCH(cu,ru))),
        cui,HSTACK(TAKE(cf,,2),
            IF(SEQUENCE(ROWS(cf)),na),TAKE(cf,,-1)),
    VSTACK(HSTACK(rData,rui),cui))
    

    enter image description here

    Edit

    • This following 'rearrangement' allows you to sort the data (see d).
    =LET(rData,A1:C12,cData,E1:G12,na,"Null",dlm,"@",
        rd,DROP(rData,1),ru,TAKE(rd,,1)&dlm&CHOOSECOLS(rd,2),
        cd,DROP(cData,1),cu,TAKE(cd,,1)&dlm&CHOOSECOLS(cd,2),
        cv,TAKE(cData,,-1),
        rui,IFNA(INDEX(DROP(cv,1),XMATCH(ru,cu)),na),
        cf,FILTER(cd,ISNA(XMATCH(cu,ru))),
        cui,HSTACK(TAKE(cf,,2),
            IF(SEQUENCE(ROWS(cf)),na),TAKE(cf,,-1)),
        h,HSTACK(TAKE(rData,1),TAKE(cv,1)),
        d,SORT(VSTACK(HSTACK(rd,rui),cui)),
    VSTACK(h,d))