Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply

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

CUSTOMERcreatedonSTATUS
A08/03/2021In negociation
A10/02/2021Declined
A10/10/2020Declined
B31/05/2021Approved
B20/05/2021Declined
B08/03/2020Approved
E14/03/2021In negociation
E08/01/2021Declined
E17/08/2020Declined
H22/02/2021Approved
H17/05/2020Declined

 

Appointment table

CUSTOMERVISIT DATE
A15/04/2021
A12/11/2020
B12/06/2021
B09/07/2020
C14/08/2021
D01/01/2021
E04/06/2021
E03/06/2020
F09/08/2021
F21/12/2020
G22/04/2021
G19/05/2020
H08/01/2021
H01/07/2020
I22/02/2021
I06/08/2020
J17/05/2021
J06/08/2020

 

These two tables are related by customer

I need to get the latest value from STATUS column to appointments to create graphs and tables visuals and I'm also need to filter by year, so I think a column dax should allow it.

I must get the latest data from Status column by year, for example:

Customer A has two opportunities in 2022 and one in 2021, and in appointment table, customer A has 2 datas. So it should appear the latest value from 2022 and the value from 2021.

When a costumer doesn't have data in opportunity table, the resulto should appear "No opportunity"

 

The column in Asspointment table should appear like this:

CUSTOMERVISIT DATEFROM OPPORTUNITIES
A15/04/2021In negociation
A12/11/2020Declined
B12/06/2021Approved
B09/07/2020Approved
C14/08/2021NO OPPORTUNITIES
D01/01/2021NO OPPORTUNITIES
E04/06/2021In negociation
E03/06/2020Declined
F09/08/2021NO OPPORTUNITIES
F21/12/2020NO OPPORTUNITIES
G22/04/2021NO OPPORTUNITIES
G19/05/2020NO OPPORTUNITIES
H08/01/2021Approved
H01/07/2020Declined
I22/02/2021NO OPPORTUNITIES
I06/08/2020NO OPPORTUNITIES
J17/05/2021NO OPPORTUNITIES
J06/08/2020NO OPPORTUNITIES

 

Any tips?

1 ACCEPTED SOLUTION
Jihwan_Kim
Super User
Super User

Hi,

Please check the below picture and the attached pbix file.

 

Picture1.png

 

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"
    )

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Visit my LinkedIn page by clicking here.


Schedule a meeting with me to discuss further by clicking here.

View solution in original post

2 REPLIES 2
Jihwan_Kim
Super User
Super User

Hi,

Please check the below picture and the attached pbix file.

 

Picture1.png

 

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"
    )

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Visit my LinkedIn page by clicking here.


Schedule a meeting with me to discuss further by clicking here.

Thank you!! It works perfectly

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

Find out what's new and trending in the Fabric Community.