Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get inspired! Check out the entries from the Power BI DataViz World Championships preliminary rounds and give kudos to your favorites. View the vizzies.

Reply
vaalyushin
Helper I
Helper I

Measure for get current status employee

Hello!  I have table with employee status:

NameIDStatusStartDateEndDate
Jon Smith283Business trip2/10/20222/13/2022
Jon Smith283Business trip4/20/20224/22/2022
Jon Smith283Vacations7/31/20208/14/2020
Jon Smith283Vacations6/7/20216/11/2021
Jon Smith283Vacations8/16/20218/29/2021
Jon Smith283Vacations11/10/202111/11/2021
Jon Smith283Vacations11/15/202111/21/2021
Jon Smith283Vacations4/11/20224/15/2022
Jon Smith283Job2/3/2020 
Melinda Yacobs123Vacations10/14/201910/14/2019
Melinda Yacobs123Vacations11/14/201911/15/2019
Melinda Yacobs123Vacations12/4/201912/6/2019
Melinda Yacobs123Vacations3/16/20203/20/2020
Melinda Yacobs123Vacations7/20/20208/2/2020
Melinda Yacobs123Vacations8/6/20208/7/2020
Melinda Yacobs123Vacations9/7/20209/13/2020
Melinda Yacobs123Vacations11/12/202011/21/2020
Melinda Yacobs123parental leave3/4/2021 
Melinda Yacobs123Unpaid 11/21/201911/21/2019
Melinda Yacobs123Job8/1/2019 

I want to get the latest status for today with the help measure. What is important for the second employee, this status is not a job, but a parental leave (because in the future she returns to work)

1 ACCEPTED SOLUTION
rohit_singh
Solution Sage
Solution Sage

Hi @vaalyushin ,

Sure. Here it is :

1) Load source data into power query

rohit_singh_0-1652872943933.png

 

2) Add a custom column to calculate all dates between start and end dates. In case end date is null, we replace it by current date. We need to convert dates to numbers in order to perform this calculation. This gives us a list of values as numbers for each row.

rohit_singh_1-1652873017162.png

 

3) We then expand the list into new rows so we get one row for each date that lies between start and end date. We need to convert the numeric data type of the column into date. We also remove the start and end dates columns.

rohit_singh_2-1652873173418.png


4) This gives us the final dataset. Column status date is the date column based on which we can compute the latest status.

rohit_singh_3-1652873268618.png


5) In DAX, we create two measures, one to compute max date for each person and the other to compute the latest status.

Max Status Date =

CALCULATE(
MAX('Job Status'[Status Date]),
ALLEXCEPT('Job Status','Job Status'[Name])
)

Current Status =
 
CALCULATE(
CONCATENATEX(VALUES('Job Status'[Status]), 'Job Status'[Status], " , "),
FILTER(ALLEXCEPT('Job Status','Job Status'[Name]),
'Job Status'[Status Date] = [Max Status Date]
)
)


This gives us the final result

rohit_singh_4-1652873467402.png

Here is the M-code:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("lZLBasMwEER/RfgcWGnlyPG1x0BPpYUSfFASQQWObGK3319J7toqLY58EbOwT4NGczoVx86xl5sdP4pdgQfpz6fPwTozDGy8297PggMCcsSgJelml8Mih5JYRNL/s2/6okfbucFrKaAKuzx4lnCY9GOuAhVWRcAE6ceYUJNF4LAmncFx8DaLIQ0Z5D4hcRMplkj9LauRHrtzCHP6s5Bl3Hs2rXVXzd71pTvHK/GPSQnh27mofw8bcDHj+3nIxD2NRKtZ53orkPRcXz+5VCcD9sDcO0xrl8Eq2g813IZWUM9tl6Rz34s/5eFpk1bxXt+NG3XLWqO/TAxcUhfXuFfXa3tliU/8omRYgacuiphMxFjRNN8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, ID = _t, Status = _t, StartDate = _t, EndDate = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"ID", Int64.Type}, {"Status", type text}, {"StartDate", type date}, {"EndDate", type date}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each if [EndDate] <> null
then
{Number.From([StartDate])..Number.From([EndDate])}
else
{Number.From([StartDate])..Number.From(Date.From(DateTime.FixedLocalNow()))}),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"StartDate", "EndDate"}),
#"Expanded Custom" = Table.ExpandListColumn(#"Removed Columns", "Custom"),
#"Changed Type1" = Table.TransformColumnTypes(#"Expanded Custom",{{"Custom", type date}}),
#"Renamed Columns" = Table.RenameColumns(#"Changed Type1",{{"Custom", "Status Date"}})
in
#"Renamed Columns"

Kind regards,

Rohit


Please mark this answer as the solution if it resolves your issue.
Appreciate your kudos! 😊

View solution in original post

3 REPLIES 3
rohit_singh
Solution Sage
Solution Sage

Hi @vaalyushin ,

Sure. Here it is :

1) Load source data into power query

rohit_singh_0-1652872943933.png

 

2) Add a custom column to calculate all dates between start and end dates. In case end date is null, we replace it by current date. We need to convert dates to numbers in order to perform this calculation. This gives us a list of values as numbers for each row.

rohit_singh_1-1652873017162.png

 

3) We then expand the list into new rows so we get one row for each date that lies between start and end date. We need to convert the numeric data type of the column into date. We also remove the start and end dates columns.

rohit_singh_2-1652873173418.png


4) This gives us the final dataset. Column status date is the date column based on which we can compute the latest status.

rohit_singh_3-1652873268618.png


5) In DAX, we create two measures, one to compute max date for each person and the other to compute the latest status.

Max Status Date =

CALCULATE(
MAX('Job Status'[Status Date]),
ALLEXCEPT('Job Status','Job Status'[Name])
)

Current Status =
 
CALCULATE(
CONCATENATEX(VALUES('Job Status'[Status]), 'Job Status'[Status], " , "),
FILTER(ALLEXCEPT('Job Status','Job Status'[Name]),
'Job Status'[Status Date] = [Max Status Date]
)
)


This gives us the final result

rohit_singh_4-1652873467402.png

Here is the M-code:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("lZLBasMwEER/RfgcWGnlyPG1x0BPpYUSfFASQQWObGK3319J7toqLY58EbOwT4NGczoVx86xl5sdP4pdgQfpz6fPwTozDGy8297PggMCcsSgJelml8Mih5JYRNL/s2/6okfbucFrKaAKuzx4lnCY9GOuAhVWRcAE6ceYUJNF4LAmncFx8DaLIQ0Z5D4hcRMplkj9LauRHrtzCHP6s5Bl3Hs2rXVXzd71pTvHK/GPSQnh27mofw8bcDHj+3nIxD2NRKtZ53orkPRcXz+5VCcD9sDcO0xrl8Eq2g813IZWUM9tl6Rz34s/5eFpk1bxXt+NG3XLWqO/TAxcUhfXuFfXa3tliU/8omRYgacuiphMxFjRNN8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, ID = _t, Status = _t, StartDate = _t, EndDate = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"ID", Int64.Type}, {"Status", type text}, {"StartDate", type date}, {"EndDate", type date}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each if [EndDate] <> null
then
{Number.From([StartDate])..Number.From([EndDate])}
else
{Number.From([StartDate])..Number.From(Date.From(DateTime.FixedLocalNow()))}),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"StartDate", "EndDate"}),
#"Expanded Custom" = Table.ExpandListColumn(#"Removed Columns", "Custom"),
#"Changed Type1" = Table.TransformColumnTypes(#"Expanded Custom",{{"Custom", type date}}),
#"Renamed Columns" = Table.RenameColumns(#"Changed Type1",{{"Custom", "Status Date"}})
in
#"Renamed Columns"

Kind regards,

Rohit


Please mark this answer as the solution if it resolves your issue.
Appreciate your kudos! 😊

rohit_singh
Solution Sage
Solution Sage

Hello @vaalyushin ,

Based on my experience there is a really simple solution using PowerQuery. I'm not sure if you're looking to resolve this specifically using DAX, but I'll mention my results here anyway.

I have assumed null values in the End date column to be the current date, else calculations will generate errors.

This is the final result aftre applying transformations in Power Query :

rohit_singh_0-1652805452356.png

rohit_singh_2-1652805493958.png

You will notice that there are overlaps in status for the same month, so for 2nd user it shows current status as job and parental leave, since null values for end date have been replaced by today's date.

If this solution using power query suits your requirements, please let me know and I'll be happy to explain the steps in detail.

Kind regards,

Rohit


Please mark this answer as the solution if it resolves your issue.
Appreciate your kudos! 😊

can you explain this solution?

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code FABINSIDER for a $400 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

Check out the February 2025 Power BI update to learn about new features.

March2025 Carousel

Fabric Community Update - March 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors