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

Score big with last-minute savings on the final tickets to FabCon Vienna. Secure your discount

Reply
bwarner87
Advocate I
Advocate I

Dax to calculate Time in Position ignoring an event/transaction

Hello Community,

 

First post and hoping it meets the standards of a good post 🙂  

 

Problem: Using DAX (measure or calculated column) I'm trying to calculate an employee's time in their latest position "Time in Position ignore JAC:" ignoring an event in the form of a transaction tied to a ["Reason_Code" = JAC] where they restructured job names that should NOT effect an employee's time in position calculation because essentially the job is the same but the title was updated with new codes and titles. Ultimatley i'm going to display this value for a single employee on an employee card.

 

Sample Data with 2 use cases using Excel to illustrate:

 

Use Case 1: the employee has NOT changed jobs and was part of the job title restructure.

Use Case 2: the employee HAS a valid job change that came after the job title restructure.  

 

Screen shot with details

bwarner87_0-1627493523487.png

 

Excel Format

Use Case 1       
EmpIDJob_CodeJob_TitleAppt_Start_dateReason_Code_JobTimeInPosition   
5555551234Director, HR Business Parter9/1/2020MER0.833333333 Time in Position ignore JAC: 4.416667
5555551234Director, HR Business Parter9/1/2020JAC0.833333333   
5555556566Director B, HR12/16/2017ANN3.583333333   
5555556566Director B, HR12/16/2017MER3.583333333   
5555552233Director A, HR10/20/2015MER5.75   
5555552233Director A, HR10/20/2015COM5.75   
5555554455Manager Z, Employee relations10/1/2011ADJ9.75   
5555554455Manager Z, Employee relations10/1/2011MER9.75   
         
Use Case 2       
EmpIDJob_CodeJob_TitleAppt_Start_dateReason_Code_JobTimeInPosition   
6666664000SVP4/1/2021MER0.25 Time in Position ignore JAC: 0.25
6666661234Director, HR Business Parter9/1/2020MER0.833333333   
6666661234Director, HR Business Parter9/1/2020JAC0.833333333   
6666666566Director B, HR12/16/2017ANN3.583333333   
6666666566Director B, HR12/16/2017MER3.583333333   
6666662233Director A, HR10/20/2015MER5.75   
6666662233Director A, HR10/20/2015COM5.75   
6666664455Manager Z, Employee relations10/1/2011ADJ9.75   
6666664455Manager Z, Employee relations10/1/2011MER9.75   

 

 

Note

- i highlighted in green the value i'm trying to solve for and gave the actual value it SHOULD be with the right calculation for both scenarios.

- all this data is in the same table/query in Power BI so don' worry about relationships.

What I've tried

- i'm working in Excel because if figured if i could get it to work there i could translate to DAX. 

- I first added the "TimeInPosition" column to calculate for each transaction against today's date using datedif function. You can see the formula in the formula bar for that transaction. 

- I then tried identying the latest position by looking at the min value of the TimeInPosition column which satisfies use case 2 but doesn't resolve use case 1 because use case 1 min value contains the event i want to ignore. 

 

Now i'm stuck and hoping you all can provide some guidance 🙂 Appreciate any insights. 

 

 

1 ACCEPTED SOLUTION
bwarner87
Advocate I
Advocate I

So after some time combing through the DAX formula documention and some testing against my use cases, I came up with a solution which I'll try my best to explain in layman's terms and then provide formulas.  

 

I realized i'm calculating a time in position for each employee, so I needed to move my row by row calculation in the form of a calculated column to my master employee data table.

 

I know how to calculcate a date difference from a date value and today's date but i needed to to determine my date value first. The date value I would need to calculate would need to look at a related table (two tables linked by a relationship; My master employee info linked to salary transactions table that contained Appt_Start_date) for a max value (or latest job appointment) of dates, in this case the job start date (Appt_Start_date) meaning the date that employee's job/position started. However because of our company structurally changing jobs for all employees,  I wanted to ignore those transactions because the employee's position didn't really change, the company changed how they labeled/named it. I needed to filter those rows and date values out before i did my max lookup. 

 

Solution formula 

Time in Position = DATEDIFF(MAXX(FILTER(RELATEDTABLE('Salary History (US)'),'Salary History (US)'[Action-Reason Code]<>"JRC-JAC"), 'Salary History (US)'[Effective_Job_Date]),TODAY(),MONTH)/12

 

DateDiff() - taking the date value i calculated and subracting it from today's date displayed by months in a calendar year.

 

MAXX() - for getting my max date value from a list of values from my related table that has been filtered

 

Filter() - for filtering out the transactions from the related table

 

RelatedTable() - identifying the table with a relationship to the table i was creating my calculated column in.

 

I hope this helps someone in the future!

 

 

 

 

View solution in original post

3 REPLIES 3
bwarner87
Advocate I
Advocate I

So after some time combing through the DAX formula documention and some testing against my use cases, I came up with a solution which I'll try my best to explain in layman's terms and then provide formulas.  

 

I realized i'm calculating a time in position for each employee, so I needed to move my row by row calculation in the form of a calculated column to my master employee data table.

 

I know how to calculcate a date difference from a date value and today's date but i needed to to determine my date value first. The date value I would need to calculate would need to look at a related table (two tables linked by a relationship; My master employee info linked to salary transactions table that contained Appt_Start_date) for a max value (or latest job appointment) of dates, in this case the job start date (Appt_Start_date) meaning the date that employee's job/position started. However because of our company structurally changing jobs for all employees,  I wanted to ignore those transactions because the employee's position didn't really change, the company changed how they labeled/named it. I needed to filter those rows and date values out before i did my max lookup. 

 

Solution formula 

Time in Position = DATEDIFF(MAXX(FILTER(RELATEDTABLE('Salary History (US)'),'Salary History (US)'[Action-Reason Code]<>"JRC-JAC"), 'Salary History (US)'[Effective_Job_Date]),TODAY(),MONTH)/12

 

DateDiff() - taking the date value i calculated and subracting it from today's date displayed by months in a calendar year.

 

MAXX() - for getting my max date value from a list of values from my related table that has been filtered

 

Filter() - for filtering out the transactions from the related table

 

RelatedTable() - identifying the table with a relationship to the table i was creating my calculated column in.

 

I hope this helps someone in the future!

 

 

 

 

Greg_Deckler
Community Champion
Community Champion

@bwarner87 I guess my question is, how do you know from the data what to include or not? Is MER the only reason code that should be considered? And when does this reset? In the examples, this number reset each time the Job_Code changed. Except in the latest job code change, it seems you want to preserve the time in position even though the job code changed. How do you know when and when not to reset even when the job code changes? Is it because of the JAC event, the job codes changed but the job actually stayed the same? So events prior to the JAC event should be treated one way and post the JAC another but only for the first couple of changes post JAC?



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler 

 

Thanks for the prompt response. 

 

To answer your question, we would want to reset the Time In Position if there was a job code change UNLESS that job code change was a result of the Reason_Code_Job = JAC event. 

 

Thank you, 

 

Brandon

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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