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
gancw1
Helper III
Helper III

Show most recent record

I have a list of employees with the joined and resigned date.

 

EmployeeJoined Date   Resigned Date  Dept
20/1/202231/12/2022Sales
1/1/20231/5/2023HR
15/2/2023 Marketing
3/3/202330/4/2023Marketing
30/4/2023 Sales

 

How can I show the most recent hire (E) and the most recent resignation (B)?

Employee  CategoryDateDept
Last Hire30/4/2023Sales
BLast resignation   1/5/2023HR

 

 

Thanks

2 ACCEPTED SOLUTIONS
tackytechtom
Super User
Super User

Hi @gancw1 ,

 

Here a suggestion:

tackytechtom_0-1684720385642.png

 

 

And here the DAX:

MeasureHireResign = 
VAR _MostRecentHireDate   = CALCULATE ( MAX ( 'Table'[Joined Date] ), ALL ( 'Table' ) )
VAR _MostRecentResignDate = CALCULATE ( MAX ( 'Table'[Resigned Date] ), ALL ( 'Table' ) )
RETURN
IF ( 
    SELECTEDVALUE ( 'Table'[Joined Date]   ) = _MostRecentHireDate, "Last Hire",
    IF (
        SELECTEDVALUE ( 'Table'[Resigned Date] ) = _MostRecentResignDate, "Last Resignation", 
        BLANK()
    )
)


To get the exact same result that you want, I'd probably change the table structure a little bit (unpivot). 

 

I'll add another response for that.

 

/Tom
https://www.tackytech.blog/
https://www.instagram.com/tackytechtom/

 

 

 



Did I answer your question➡️ Please, mark my post as a solution ✔️

Also happily accepting Kudos 🙂

Feel free to connect with me on LinkedIn! linkedIn

#proudtobeasuperuser 

View solution in original post

tackytechtom
Super User
Super User

Hi @gancw1 ,

 

For the unpivoting, I usually use Power Query. I used the table from above and unpivoted on the two date columns. I also renamed the columns.

 

Here the code in Power Query M that you can paste into the advanced editor (if you do not know, how to exactly do this, please check out this quick walkthrough)

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclRQ0lEyMtA31DcyMDICso0N9Q2NYJzgxJzUYqVYnWglJ5A6Q4gyYzDTFMb0CAKrcAarAIrCxEF838Si7NSSzLx0sBIXkJCxvjFMhbGBvgmMjarSVQFNWgHhmlgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Employee = _t, #"Joined Date" = _t, #"Resigned Date" = _t, Dept = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Employee", type text}, {"Joined Date", type date}, {"Resigned Date", type date}, {"Dept", type text}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Employee", "Dept"}, "Type", "Date")
in
    #"Unpivoted Columns"

 

Now, we can use a similar DAX measure as per below:

tackytechtom_3-1684721238472.png

 

Here the DAX measure:

MeasureHireResign2 = 
VAR _MostRecentHireDate   = CALCULATE ( MAX ( 'TablePivot'[Date] ), ALL ( 'TablePivot' ), TablePivot[Type] = "Joined Date" )
VAR _MostRecentResignDate = CALCULATE ( MAX ( 'TablePivot'[Date] ), ALL ( 'TablePivot' ), TablePivot[Type] = "Resigned Date" )
RETURN
IF ( 
    SELECTEDVALUE ( 'TablePivot'[Date] ) = _MostRecentHireDate && SELECTEDVALUE ( 'TablePivot'[Type] ) = "Joined Date", "Last Hire",
    IF (
        SELECTEDVALUE ( 'TablePivot'[Date] ) = _MostRecentResignDate && SELECTEDVALUE ( 'TablePivot'[Type] ) = "Resigned Date", "Last Resignation", 
        BLANK()
    )
)

 

Let me know if either of the two solutions work for you 🙂

 

/Tom
https://www.tackytech.blog/
https://www.instagram.com/tackytechtom/



Did I answer your question➡️ Please, mark my post as a solution ✔️

Also happily accepting Kudos 🙂

Feel free to connect with me on LinkedIn! linkedIn

#proudtobeasuperuser 

View solution in original post

6 REPLIES 6
tackytechtom
Super User
Super User

Hi @gancw1 ,

 

For the unpivoting, I usually use Power Query. I used the table from above and unpivoted on the two date columns. I also renamed the columns.

 

Here the code in Power Query M that you can paste into the advanced editor (if you do not know, how to exactly do this, please check out this quick walkthrough)

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclRQ0lEyMtA31DcyMDICso0N9Q2NYJzgxJzUYqVYnWglJ5A6Q4gyYzDTFMb0CAKrcAarAIrCxEF838Si7NSSzLx0sBIXkJCxvjFMhbGBvgmMjarSVQFNWgHhmlgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Employee = _t, #"Joined Date" = _t, #"Resigned Date" = _t, Dept = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Employee", type text}, {"Joined Date", type date}, {"Resigned Date", type date}, {"Dept", type text}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Employee", "Dept"}, "Type", "Date")
in
    #"Unpivoted Columns"

 

Now, we can use a similar DAX measure as per below:

tackytechtom_3-1684721238472.png

 

Here the DAX measure:

MeasureHireResign2 = 
VAR _MostRecentHireDate   = CALCULATE ( MAX ( 'TablePivot'[Date] ), ALL ( 'TablePivot' ), TablePivot[Type] = "Joined Date" )
VAR _MostRecentResignDate = CALCULATE ( MAX ( 'TablePivot'[Date] ), ALL ( 'TablePivot' ), TablePivot[Type] = "Resigned Date" )
RETURN
IF ( 
    SELECTEDVALUE ( 'TablePivot'[Date] ) = _MostRecentHireDate && SELECTEDVALUE ( 'TablePivot'[Type] ) = "Joined Date", "Last Hire",
    IF (
        SELECTEDVALUE ( 'TablePivot'[Date] ) = _MostRecentResignDate && SELECTEDVALUE ( 'TablePivot'[Type] ) = "Resigned Date", "Last Resignation", 
        BLANK()
    )
)

 

Let me know if either of the two solutions work for you 🙂

 

/Tom
https://www.tackytech.blog/
https://www.instagram.com/tackytechtom/



Did I answer your question➡️ Please, mark my post as a solution ✔️

Also happily accepting Kudos 🙂

Feel free to connect with me on LinkedIn! linkedIn

#proudtobeasuperuser 

tackytechtom
Super User
Super User

Hi @gancw1 ,

 

Here a suggestion:

tackytechtom_0-1684720385642.png

 

 

And here the DAX:

MeasureHireResign = 
VAR _MostRecentHireDate   = CALCULATE ( MAX ( 'Table'[Joined Date] ), ALL ( 'Table' ) )
VAR _MostRecentResignDate = CALCULATE ( MAX ( 'Table'[Resigned Date] ), ALL ( 'Table' ) )
RETURN
IF ( 
    SELECTEDVALUE ( 'Table'[Joined Date]   ) = _MostRecentHireDate, "Last Hire",
    IF (
        SELECTEDVALUE ( 'Table'[Resigned Date] ) = _MostRecentResignDate, "Last Resignation", 
        BLANK()
    )
)


To get the exact same result that you want, I'd probably change the table structure a little bit (unpivot). 

 

I'll add another response for that.

 

/Tom
https://www.tackytech.blog/
https://www.instagram.com/tackytechtom/

 

 

 



Did I answer your question➡️ Please, mark my post as a solution ✔️

Also happily accepting Kudos 🙂

Feel free to connect with me on LinkedIn! linkedIn

#proudtobeasuperuser 

What if I want to show the last 3 most recent hires. How do I do that ?

 

Employee     Joined Date    Dept
30/4/2023Sales
3/3/2023Marketing
15/2/2023Marketing

Hi @gancw1 ,

 

You can solve such things with measures, too, but I usually recommend using the Filter pane as you most likely want to keep down the number of measures in your model:

tackytechtom_0-1684723395963.png

 

/Tom
https://www.tackytech.blog/
https://www.instagram.com/tackytechtom/

 



Did I answer your question➡️ Please, mark my post as a solution ✔️

Also happily accepting Kudos 🙂

Feel free to connect with me on LinkedIn! linkedIn

#proudtobeasuperuser 

noted and thanks

Thanks for the quick response !

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.