Forum Discussion

massotebernoull's avatar
4 years ago
Solved

Getting the latest value from two tables

Hi, I tried in a different ways, but I'm strugled with this:  I have the Opportunities table and Appointments table, as it follows: Opportunities table CUSTOMER createdon STATUS A 08/03/2...
  • Jihwan_Kim's avatar
    4 years ago

    Hi,

    Please check the below picture and the attached pbix file.

     

     

    From Opportunities CC =
    VAR currentyear =
        YEAR ( Appointment[VISIT DATE] )
    VAR currentcustomer = Appointment[CUSTOMER]
    VAR opptable_bymaxdateyear =
        CALCULATETABLE (
            Opportunities,
            TREATAS (
                SELECTCOLUMNS (
                    GROUPBY (
                        ADDCOLUMNS ( Opportunities, "@year", YEAR ( Opportunities[createdon] ) ),
                        Opportunities[CUSTOMER],
                        [@year],
                        "@maxdate_peryear", MAXX ( CURRENTGROUP (), Opportunities[createdon] )
                    ),
                    "@customer", Opportunities[CUSTOMER],
                    "@create", [@maxdate_peryear]
                ),
                Opportunities[CUSTOMER],
                Opportunities[createdon]
            )
        )
    VAR filtertable =
        FILTER (
            opptable_bymaxdateyear,
            YEAR ( Opportunities[createdon] ) = currentyear
                && Opportunities[CUSTOMER] = currentcustomer
        )
    RETURN
        IF (
            NOT ISBLANK ( MAXX ( filtertable, Opportunities[STATUS] ) ),
            MAXX ( filtertable, Opportunities[STATUS] ),
            "No Opportunity"
        )