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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
henkka
Helper II
Helper II

How to get latest value if duplicate id exist

Hi,

 

Can someone please help. I have tried to get latest wage, if multiple workagreements exist without any luck.

 

The table is below and what I'm trying to achieve is to check if duplicate Employee.id existis, then sumx the wage based from wage.Id (biggest number is the latest). If duplicate not existing then sumx wage.

henkka_0-1659964433900.png

 

1 ACCEPTED SOLUTION
johnt75
Super User
Super User

Try

Wage =
SUMX (
    'Table',
    VAR currentEmployee = 'Table'[Employee ID]
    VAR summaryTable =
        TOPN (
            1,
            FILTER ( 'Table', 'Table'[Employee ID] = currentEmployee ),
            'Table'[Workagreement.Id]
        )
    RETURN
        SUMX ( summaryTable, 'Table'[Monthly wage] )
)

View solution in original post

4 REPLIES 4
henkka
Helper II
Helper II

@johnt75  Great, thank you! works like a charm 🙂

johnt75
Super User
Super User

Try

Wage =
SUMX (
    'Table',
    VAR currentEmployee = 'Table'[Employee ID]
    VAR summaryTable =
        TOPN (
            1,
            FILTER ( 'Table', 'Table'[Employee ID] = currentEmployee ),
            'Table'[Workagreement.Id]
        )
    RETURN
        SUMX ( summaryTable, 'Table'[Monthly wage] )
)

Hi @johnt75  actually when I looked the numbers more carefully and there is some issue still with the measure. Everything else is ok, but it sums wages twise if there is duplicate employee.id. For example the first employee from the table has the latest wage of 3 600 and the measure returns 7 200 (3 600*2). All good when there is only one wage.
 

That employee has 2 records with the same work agreement ID, you may need to use the wage.id column to disambiguate. The below code will use the highest wage.id in the event of ties.

Wage =
SUMX (
    'Table',
    VAR currentEmployee = 'Table'[Employee ID]
    VAR summaryTable =
        TOPN (
            1,
            FILTER ( 'Table', 'Table'[Employee ID] = currentEmployee ),
            'Table'[Workagreement.Id], DESC,
            'Table'[wage.id], DESC
        )
    RETURN
        SUMX ( summaryTable, 'Table'[Monthly wage] )
)

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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