Forum Discussion

Rajakkr1996's avatar
Rajakkr1996
Regular Visitor
3 years ago
Solved

Problem with Many to Many relationships to get max date record based on a slicer

Hi, I am new to Power BI development.I need help to resolve the problem below. 

I have three tables(Table1,Table2 and CalenderTable). Table1 has Many to Many relationships with Table2. I have added a date slicer based on CalenderTable. If the user selects one date on the date slicer, I need to find max date(Max Date should be less than Slicer Date) records in Table2 for each Product ID and need to display all the Table1 columns along with the Table2 Price Column by using ProductID. 

Table1:

Table2:

 

 

Expected Final Result 😞Let's assume the user Picked date in Slicer is 28-Mar-2022 )

 

Model View:

 



  • Rajakkr1996 OK, I think I got this. See PBIX attached below signature.

    Measure = 
        VAR __CalendarDate = MAX('CalendarTable'[Date])
        VAR __Product = MAX('Table1'[ProductID])
        VAR __MaxDate = MAXX(FILTER('Table2', [ValueDate] <= __CalendarDate), [ValueDate])
        VAR __Value = MAXX(FILTER('Table2',[ValueDate] = __MaxDate),[Price])
        VAR __Result = IF(HASONEVALUE(CalendarTable[Date]) && __MaxDate <= __CalendarDate, __Value, BLANK())
    RETURN
        __Result

4 Replies

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

    Rajakkr1996 Can you paste that sample data as text in a table so I can mock it up?

    Sorry, having trouble following, can you post sample data as text and expected output?
    Not really enough information to go on, please first check if your issue is a common issue listed here: https://community.powerbi.com/t5/Community-Blog/Before-You-Post-Read-This/ba-p/1116882

    Also, please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490

    The most important parts are:
    1. Sample data as text, use the table tool in the editing bar
    2. Expected output from sample data
    3. Explanation in words of how to get from 1. to 2.

    • Rajakkr1996's avatar
      Rajakkr1996
      Regular Visitor


      Greg_Deckler 
      Thanks for the reply and guidance. Sample data for your reference,
      Table1:

      RowIDProductIDOrderedDate
      1118-03-2022
      2119-03-2022
      3220-03-2022
      4221-03-2022
      5222-03-2022
      6323-03-2022
      7324-03-2022
      8325-03-2022
      9426-03-2022
      10427-03-2022

      Table2:

      ProductIDValueDatePrice
      101-02-2022100.00
      102-02-2022200.00
      222-02-2022155.67
      202-02-2022143.78
      217-02-2022356.89
      322-02-2022557.43
      307-02-2022111.32
      308-02-2022888.87
      419-02-2022567.89
      410-02-2022222.44

       

      Expected Output:

      RowIDProductIDOrderedDatePrice
      1118-03-2022200.00
      2119-03-2022200.00
      3220-03-2022356.89
      4221-03-2022356.89
      5222-03-2022356.89
      6323-03-2022111.32
      7324-03-2022111.32
      8325-03-2022111.32
      9426-03-2022567.89
      10427-03-2022567.89
      • Greg_Deckler's avatar
        Greg_Deckler
        Icon for Community Champion rankCommunity Champion

        Rajakkr1996 OK, I think I got this. See PBIX attached below signature.

        Measure = 
            VAR __CalendarDate = MAX('CalendarTable'[Date])
            VAR __Product = MAX('Table1'[ProductID])
            VAR __MaxDate = MAXX(FILTER('Table2', [ValueDate] <= __CalendarDate), [ValueDate])
            VAR __Value = MAXX(FILTER('Table2',[ValueDate] = __MaxDate),[Price])
            VAR __Result = IF(HASONEVALUE(CalendarTable[Date]) && __MaxDate <= __CalendarDate, __Value, BLANK())
        RETURN
            __Result