Forum Discussion

Glenda's avatar
Glenda
Helper I
6 years ago
Solved

Finding the value based off date and ID

Hi, 

 

Ok, I am sure this is not a hard one but I just can' get it so hoping someone can help me out. I need to find the cost centre a person worked for when they had an injury. I have 2 tables: 

  1. The business table which has the employee ID, Cost Centre and Date (AWD Date From) 
  2. Injury table with the employee ID and date of injury (DOI).

I need to find the Cost Centre at the time of the injury. 

 

Business Table

Employee IDCost CentreAWD Date From
12345642252601/09/2007
12345620252606/07/2009
12345610162208/12/2014
12345620252608/01/2018
12345620017504/11/2019
98745616923520/12/2007
98745616923726/05/2008
98745616920722/12/2008
98745616924121/12/2009
98745616920925/10/2010
98745616924229/12/2014
98745674333618/06/2018
98745674333622/06/2020

 

Injury Table

Employee IDDOI
12345608/03/08
12345603/09/11
12345627/12/15
12345606/07/20
98745621/12/08
98745622/12/08

 

The output I need is the injury table plus the cost where the employee was working at the date of injury like the table below. 

Employee IDDOICost Centre
12345608/03/08422526
12345603/09/11202526
12345627/12/15101622
12345606/07/20200175
98745621/12/08169237
98745622/12/08169207

 

Here is a link to my pbix with the data loaded - https://ufile.io/3oen7t5r

  • Thanks for providing a pbix file.  Here is an expression you can use in a new DAX column on your Injury Dates table, to get the Employees cost center at the time.

     

    DOI Cost Center =
    VAR __thisemployee = 'Injury dates'[Employee ID]
    VAR __thisdate = 'Injury dates'[DOI]
    VAR __CCdate =
        CALCULATE (
            MAX ( business[AWD Date From] ),
            business[AWD Date From] <= __thisdate,
            business[Employee ID] = __thisemployee
        )
    RETURN
        CALCULATE (
            MIN ( business[Cost Centre] ),
            business[Employee ID] = __thisemployee,
            business[AWD Date From] = __CCdate
        )

     

    If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

    Regards,

    Pat

  • Glenda's avatar
    Glenda
    6 years ago

    Thank you so much it works perfectly ğŸ˜„  

2 Replies

  • mahoneypat's avatar
    mahoneypat
    Microsoft Employee

    Thanks for providing a pbix file.  Here is an expression you can use in a new DAX column on your Injury Dates table, to get the Employees cost center at the time.

     

    DOI Cost Center =
    VAR __thisemployee = 'Injury dates'[Employee ID]
    VAR __thisdate = 'Injury dates'[DOI]
    VAR __CCdate =
        CALCULATE (
            MAX ( business[AWD Date From] ),
            business[AWD Date From] <= __thisdate,
            business[Employee ID] = __thisemployee
        )
    RETURN
        CALCULATE (
            MIN ( business[Cost Centre] ),
            business[Employee ID] = __thisemployee,
            business[AWD Date From] = __CCdate
        )

     

    If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

    Regards,

    Pat

    • Glenda's avatar
      Glenda
      Helper I

      Thank you so much it works perfectly ğŸ˜„