Forum Discussion

tuomas-i's avatar
tuomas-i
Frequent Visitor
7 years ago
Solved

Using a certain value from a related table for calculation

I have two tables: Tasks and TaskBaselines   Tasks: TaskId, TaskFinishDate TaskBaselines: TaskId, TaskBaselineNumber, TaskBaselineFinishDate     I would like to get a DATEDIFF (in days) betw...
  • TomMartens's avatar
    7 years ago

    Hey,

    first I created a calculated column in the table TaskBaselines, that flags the highest baselinenumber per task:

    is max Baseline = 
    var currentTaskID = 'TaskBaselines'[TaskID]
    return
    IF(
        CALCULATE(
            MAX('TaskBaselines'[TaskBaselineNumber])
            ,FILTER(
                ALL(TaskBaselines)
                ,TaskBaselines[TaskID] = currentTaskID
            )
        )
        = TaskBaselines[TaskBaselineNumber]
        ,"is max"
        ,"is not max"
    )
     

    Then I created a calculated column in table tasks that calculates the difference in days using DATEDIFF, by pulling the date from TaskBaseline table utilizing the "is max Baseline" column like so:

    diff days = 
    DATEDIFF(
    'Tasks'[TaskFinishDate]
    ,LOOKUPVALUE(
        'TaskBaselines'[TaskBaselineFinishDate]
        ,TaskBaselines[TaskID],'Tasks'[TaskID]
        ,TaskBaselines[is max Baseline], "is max"
    )
    
    ,DAY
    ) 

    Hopefully this is what you are looking for.

     

    Regards,

    Tom