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

Create a new column from non related table

Hi,

I'm trying to create a new column in a table that will be based on another table which is not related.

The first table contains a user_ID which I want to lookup in the second table and bring the value for the employment status, but bring the value for the latest one.

 

Risks

Risk IDRisk_Owner_User_IDRisk Owner Employment Status (new column I need)
1AActive (currently getting "Inactive")
2BActive

 

Employee History

Employee IDUser_IDEmployee StatusValid_To
123AInactive1/1/2020
456AActive31/12/9999
789BActive

31/12/2023

 

My new column in risks table should include the Employee_Status for the latest Valid_To date.

 

I created this which is giving me some false-positives, and I've tried a few variations with no luck so far (the IF() is because some records don't have a user_ID and I was getting incorrect values in those):

 

Risk Owner Employment Status =
IF(
    Risks[Risk_Owner_UserID]<>"",
        CALCULATE(
            MAX(Employee_History[Employee_Status]),
            FILTER(Employee_History, MAX(Employee_History[ValidTo]) && Risks[Risk_Owner_UserID] = Employee_History[UserID]
            )
        ),
    ""
)
 
Thanks!
1 ACCEPTED SOLUTION

Thank you @Jihwan_Kim 

I reproduced the expression in my project but still had false-positives.  I don't fully understand what the TREATAS() does, but then I created a table with your CALCULATETABLE() to see what it was producing and it's the latest record for each User_ID.  What I believe it was missing was a link to the current User_ID in my risks table, so I included a CALCULATE() in the expression which appears to work now.

Risk Owner Employment Status =
IF (
    Risks[Risk_Owner_User_ID] <> "",
        CALCULATE(
            MAXX (
                CALCULATETABLE (
                    'Employee History',
                    TREATAS (
                        GROUPBY (
                            'Employee History',
                            'Employee History'[User_ID],
                            "@maxdate", MAXX ( CURRENTGROUP (), 'Employee History'[Valid_To] )
                        ),
                        'Employee History'[User_ID],
                        'Employee History'[Valid_To]
                    )
                ),
                'Employee History'[Employee Status]
            ),
        FILTER('Employee History', 'Employee History'[User_ID] = Risks[Risk_Owner_User_ID])
    ),
    ""
)

View solution in original post

2 REPLIES 2
Jihwan_Kim
Super User
Super User

Hi,

Please check the attached pbix file and the below picture.

 

Picture1.png

 

Risk Owner Employment Status =
IF (
    Risks[Risk_Owner_User_ID] <> "",
    MAXX (
        CALCULATETABLE (
            'Employee History',
            TREATAS (
                GROUPBY (
                    'Employee History',
                    'Employee History'[User_ID],
                    "@maxdate", MAXX ( CURRENTGROUP (), 'Employee History'[Valid_To] )
                ),
                'Employee History'[User_ID],
                'Employee History'[Valid_To]
            )
        ),
        'Employee History'[Employee Status]
    ),
    ""
)

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.


Go to My LinkedIn Page


Thank you @Jihwan_Kim 

I reproduced the expression in my project but still had false-positives.  I don't fully understand what the TREATAS() does, but then I created a table with your CALCULATETABLE() to see what it was producing and it's the latest record for each User_ID.  What I believe it was missing was a link to the current User_ID in my risks table, so I included a CALCULATE() in the expression which appears to work now.

Risk Owner Employment Status =
IF (
    Risks[Risk_Owner_User_ID] <> "",
        CALCULATE(
            MAXX (
                CALCULATETABLE (
                    'Employee History',
                    TREATAS (
                        GROUPBY (
                            'Employee History',
                            'Employee History'[User_ID],
                            "@maxdate", MAXX ( CURRENTGROUP (), 'Employee History'[Valid_To] )
                        ),
                        'Employee History'[User_ID],
                        'Employee History'[Valid_To]
                    )
                ),
                'Employee History'[Employee Status]
            ),
        FILTER('Employee History', 'Employee History'[User_ID] = Risks[Risk_Owner_User_ID])
    ),
    ""
)

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.

Top Solution Authors