Forum Discussion
DAX Lookup value in the same table
It looks like you want to assign the "Assignee" value to each row in your table based on certain conditions. To achieve this, you can use DAX measures and a combination of functions to perform the lookup and assignment. Here's a modified version of your DAX code to achieve the desired result:
Assigned =
VAR Key =
Table[ID]
VAR LatestStatusDate =
CALCULATE(
MAX(Table[EndDate]),
FILTER(Table, Table[ID] = Key)
)
VAR LatestAssignee =
CALCULATE(
MAX(Table[Assignee]),
FILTER(
Table,
Table[ID] = Key &&
Table[AssigneeEndDate] <= LatestStatusDate
)
)
RETURN
LatestAssignee
This code should assign the appropriate "Assignee" value to each row based on the conditions you specified. The key points in this code are:
- We use a VAR statement to define the "Key" and "LatestStatusDate" variables.
- For "Key," we simply reference the "ID" column from the table.
- For "LatestStatusDate," we use the CALCULATE function with a FILTER condition to find the maximum "EndDate" value for rows where the "ID" matches the current row's "ID."
- In the "LatestAssignee" variable, we use CALCULATE with another FILTER condition to find the maximum "Assignee" value for rows where the "ID" matches the current row's "ID" and the "AssigneeEndDate" is less than or equal to the "LatestStatusDate."
- Finally, we return the "LatestAssignee" value, which will be assigned to each row in your table.
When you use this measure in your table, it should give you the desired result, as shown in your "Result" section.
Thank you for the DAX code, but it's not returing the desired result.
This problem can easily done in SQL query using self left join.
Here's SQL query, the 1st select is the data from the table, the 2nd select is the correct desired result:
Note that in this sample data, I added the Type field to make it easy to get the correct result.
This is the result in Power BI:
DAX for Assigned:
I get the same result whether I've the extra filter on TempTable[Type] or not.