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
mober
Frequent Visitor

Newest value for each ID from other table

I have two tables:

  • Table 1: Static data for each ID 
  • Table 2: Transactional data

I want to look up the newest value in Table 2 and add it as a new column in Table 1.

See below tables for the desired output:

mober_0-1594283282764.png

 

1 ACCEPTED SOLUTION
Icey
Community Support
Community Support

Hi @mober ,

 

Try to create a calculated column like so:

Latest Location =
VAR LatestDate =
    CALCULATE (
        MAX ( 'Table 2'[Date] ),
        FILTER ( 'Table 2', 'Table 2'[ID] = EARLIER ( 'Table 1'[ID] ) )
    )
RETURN
    CALCULATE (
        MAX ( 'Table 2'[Location] ),
        FILTER (
            'Table 2',
            'Table 2'[Date] = LatestDate
                && 'Table 2'[ID] = EARLIER ( 'Table 1'[ID] )
        )
    )

location.PNG

 

BTW, .pbix file attached.

 

 

Best Regards,

Icey

 

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

View solution in original post

8 REPLIES 8
Icey
Community Support
Community Support

Hi @mober ,

 

Try to create a calculated column like so:

Latest Location =
VAR LatestDate =
    CALCULATE (
        MAX ( 'Table 2'[Date] ),
        FILTER ( 'Table 2', 'Table 2'[ID] = EARLIER ( 'Table 1'[ID] ) )
    )
RETURN
    CALCULATE (
        MAX ( 'Table 2'[Location] ),
        FILTER (
            'Table 2',
            'Table 2'[Date] = LatestDate
                && 'Table 2'[ID] = EARLIER ( 'Table 1'[ID] )
        )
    )

location.PNG

 

BTW, .pbix file attached.

 

 

Best Regards,

Icey

 

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

amitchandak
Super User
Super User

@mober ,

latest location = LastNONBLANKVALUE(table2[Date], MAx(Table2[Location]))

 

make sure two tables have joins to display data together

@amitchandak 

I am getting the same value for all IDs, even though the two tables are joined on the ID

AllisonKennedy
Super User
Super User

As a MEASURE you could do this and use in a matrix visual: 

Latest Location = 

VAR _lastDate = MAX(Table2[Date])

RETURN MAXX(FILTER(Table2, Table2[Date]=_lastDate), Table2[Location]) 

 

or

 

Latest Location = 

VAR _lastDate = MAX(Table2[Date])

RETURN CALCULATE(SelectedValue[Table2[Location]), (FILTER(Table2, Table2[Date]=_lastDate))


Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

Thank you for your solution @AllisonKennedy .

However, I will need the value in a column in order for the data to interact correctly between multiples tables.

The same formula could work as column, but your cross filter direction will be preventing it from pulling the info through properly. It would need to be set to both.

Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

@AllisonKennedy The cross filter direction is already set to "Both", but I am receiving blank rows when using the formulas you provided.

mober_0-1594360401411.png

 

That's probably because it is a many to many relationship. I suggest you to look again at your data model and get that set up properly before you can begin to solve problems using DAX.
https://docs.microsoft.com/en-us/power-bi/transform-model/desktop-many-to-many-relationships

Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

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.