Forum Discussion

MJEnnis's avatar
MJEnnis
Icon for Resolver III rankResolver III
4 years ago

Problem with many to many lookup

I am having trouble creating a filter column based on a calcuation in another table. It is something I have done several times, but it just isn't working here.

 

Here is the basic structure of the reference table.

 

IDEnd_DateFilter-2
14319.02.22 00:00UNKNOWN
44810.10.14 00:00NO
44821.03.19 00:00NO
136923.07.14 00:00UNKNOWN
3646430.09.19 00:00UNKNOWN
3646419.02.22 00:00NO
3646430.09.20 00:00NO

 

And here is the table where I need to create a filter column. 

 

IDDateFilter-1Filter-2
14314.07.15 00:00L319.02.22 00:00
14326.11.14 00:00L319.02.22 00:00
14308.11.11 00:00L3 
14329.11.11 00:00L3 
14308.05.15 00:00L319.02.22 00:00
14311.04.11 00:00L2 
14309.05.12 00:00L3 
14327.11.14 00:00L2 
14305.04.12 00:00L3 
14326.11.13 00:00L319.02.22 00:00
44811.04.11 00:00L3 
44811.04.11 00:00L2 
44821.08.14 00:00L2 
44824.11.15 00:00L3 
44815.07.14 00:00L2 
44815.07.14 00:00L3 
44821.08.14 00:00L3 
136908.11.11 00:00L3 
136929.11.11 00:00L323.07.14 00:00
136908.05.12 00:00L223.07.14 00:00
136904.04.12 00:00L323.07.14 00:00
3646414.01.21 00:00L3 
3646404.10.17 00:00L3 
3646410.01.20 00:00L3 
3646420.02.21 00:00L3 
3646404.10.18 00:00L330.09.19 00:00
3646402.10.20 00:00L3 
3646404.06.21 00:00L3 
3646427.02.18 00:00L230.09.19 00:00
3646408.06.20 00:00L3 
3646405.06.19 00:00L330.09.19 00:00
3646428.09.21 00:00L3 
3646414.01.22 00:00L119.02.22 00:00
3646404.10.18 00:00L230.09.19 00:00
3646415.01.18 00:00L3 
3646420.04.17 00:00L2 

 

As you can see, I had no problem creating the Filter-1 column (from a third table), using basically the same strategy. But the Filter-2 column has multiple blanks. I have played around with it in different ways (e.g., MIN(), MAX(), etc.), and it does change which rows return the correct value, but there are always several blanks.

 

Here is the code I am using:

 

 

Filter-2 = 

Var End_Date = CALCULATE(MIN('Table1'[End_Date]), FILTER('Table1',
    'Table1'[ID] = 'Table2'[ID] &&
    'Table1'[End_Date] >= 'Table2'[Date])
    )

Var Filter_2 = CALCULATE(SELECTEDVALUE('Table1'[Filter-2]), FILTER(
    'Table1', 
    'Table1'[ID] = 'Table2'[ID] &&
    'Table1'[End Date] = End_Date)
    )

RETURN Filter_2

 

 

 

Any ideas?

18 Replies

  • MJEnnis's avatar
    MJEnnis
    Icon for Resolver III rankResolver III

    Note that in the second table above, I was testing to see if the correct end_date was being returned. The correct Filter_2 is returned in the exact same cells.

    • tamerj1's avatar
      tamerj1
      Icon for Community Champion rankCommunity Champion

      Hi Ennis,

      Are the tables connected with Many-Many realtionship?

      • MJEnnis's avatar
        MJEnnis
        Icon for Resolver III rankResolver III

        No, no connection at all yet. But there is also no connection to the third table with the Filter-1 data.

  • tamerj1's avatar
    tamerj1
    Icon for Community Champion rankCommunity Champion

    It is the context transition. CALCULATE converts the row into a filter. If the date in table 2 is missing in Table 1 then the date in the filter context is empty and CALCULATE returns blank. 

    • MJEnnis's avatar
      MJEnnis
      Icon for Resolver III rankResolver III

      All rows in Table 1 have an End Date. And the VAR End_Date calculated in the calculated column of Table 2 matches the date in Table1 (as seen in the copied entries above). It is just that CALCULATE is retrieving the match for some rows in Table 2 and not for others. If I replace SELECTEDVALUE with MIN or MAX, then it retrieves the correct date for different rows. I think for some reason CALCULATE(MIN('Table1'[End_Date])... is not working the way I expect it too.

  • v-chenwuz-msft's avatar
    v-chenwuz-msft
    Icon for Community Support rankCommunity Support

    Hi MJEnnis ,

     

    We do not use selectedvalue when create column but [column name] directly. And if we want get filter result form table1 where table1 id = table2 id, we need to use eariler() function.

     

    So please try the following code to get the min date depend on ID from table 1:

    Filter-2 =
    CALCULATE(
        MIN( 'Table1'[End_Date] ),
        FILTER(
            'Table1',
            'Table1'[ID] = EARLIER( 'Table2'[ID] )
                && 'Table1'[End_Date] >= EARLIER( 'Table2'[Date] )
        )
    )
    

     

     

    Pbix in the end you can refer.

    Best Regards

    Community Support Team _ chenwu zhu

     

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

    • MJEnnis's avatar
      MJEnnis
      Icon for Resolver III rankResolver III

      Thanks a lot for the suggestion. The code you propose doesn't really produce the result I have explained above. Moreover, I managed to get it to work with SELECTEDVALUE() and without EARLIER(). Will see about using the column name directly in the future.