1

I am using DAX language in Power BI Desktop.

I have a tricky situation where I am trying to use the column name generated from a table variable.

Table 1: SourceTable

enter image description here

Table 2: ReferenceTable

enter image description here

I need to develop a calculated column in SourceTable called EmploymentStatus, based on the corresponding column in ReferenceTable. But I need only the EmploymentStatus value from ReferenceTable, for the maximum InternalID for a given EmployeeEmail.

For example, for the email xyz.gmail.com in SourceTable, I need the EmploymentStatus (calculated column) as 'Active' from ReferenceTable, since 'Active' has the maximum of the two available InternalID values (17, 15).

I tried the following code (Calculated Column in SourceTable):

EmploymentStatus_SourceTable_CalculatedColumn =
VAR tabl1 =
    SUMMARIZE (
        ReferenceTable,
        ReferenceTable[EmployeeEmail],
        "MaxInteralID", MAX ( ReferenceTable[InternalID] )
    )
VAR tabl2 =
    FILTER (
        ReferenceTable,
        ReferenceTable[InternalID] IN VALUES ( tabl1[MaxInteralID] )
    )
VAR NewCol =
    LOOKUPVALUE (
        tabl2[EmploymentStatus],
        tabl2[EmployeeEmail], SourceTable[EmployeeEmail]
    )
RETURN
    NewCol

I realize that I cannot use the column generated from the table variable.

For example, tabl1[MaxInteralID], tabl2[EmployeeStatus], tabl2[EmployeeEmail] - are all invalid.

Any idea on how to handle this? You can even provide me with a solution that does not use variables at all. Am okay with any solution.

0

1 Answer 1

1

Similar to here, you can find the maximal ID for each email and look up the status for that ID.

Table and column names shortened for readability:

CalcCol =
VAR Email = Source[Email]
VAR MaxID = CALCULATE ( MAX ( Ref[ID] ), Ref[Email] = Email )
RETURN
    LOOKUPVALUE ( Ref[Status], Ref[Email], Email, Ref[ID], MaxID )
Sign up to request clarification or add additional context in comments.

5 Comments

thanks very much, will get back shortly. appreciate your help
EmploymentStatus_SourceTable_CalculatedColumn = VAR Email = SourceTable[EmployeeEmail] VAR MaxID = CALCULATE ( MAX(ReferenceTable[InternalID]), ReferenceTable[EmployeeEmail] = Email ) RETURN LOOKUPVALUE( ReferenceTable[EmploymentStatus], ReferenceTable[EmployeeEmail], Email, ReferenceTable[InternalID], MaxID )
the above comment has proper table and column names, for anyone who wants to refer later; the solution is what Alexis posted
EmploymentStatus_SourceTable_CalculatedColumn = var CurrentEmail = SourceTable[EmployeeEmail] var Status_ = SELECTCOLUMNS( topn(1, filter( ReferenceTable, ReferenceTable[EmployeeEmail] = CurrentEmail ), ReferenceTable[InternalID], DESC ), "@Status", ReferenceTable[EmploymentStatus] ) return Status_
this code immediately above, is a solution with better performance

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.