Skip to content

HR - Inventory Movements & Tenure

Suppose you work in HR and need to show your team and your managers how the workforce has moved over time: who joined, who left, and who moved within the company. The usual way to show this is an inventory movement table, which counts the people at the start of each period, the joiners and leavers during it, and the people at the end.

The starting point is an extract of employee history from the HR system, like this:

An Excel table with one row per employee record: User ID, First Name, Last Name, Gender, Start Date, End Date, Country, Office, Department, Job Grade and Full or Part. Two invented employees have several records each.

The system creates a new record for an employee whenever their country, office, department, job grade or full-time or part-time status changes, so each record covers one period in which the employee’s profile stayed the same. Not every HR system keeps this much history, but this example assumes yours does. Salary could be tracked in the same way as one more field.

Before the inventory movement table can be built, the data has to be reshaped so that three questions can be answered quickly and accurately:

  1. Whether each change of record is internal, which means a move within the company, or external, which means joining or leaving it.
  2. What each employee’s total running tenure is, up to the end of a given month or up to today.
  3. What each employee’s profile was at a given point in time, such as a particular month or quarter.

This page builds the intermediate queries that answer these questions, one at a time.

To tell an internal move from a joining or a leaving, each record needs the end date of the employee’s previous record and the start date of their next one. The steps below add those two dates and then compare them with the record’s own dates.

  • Import the employee history table into Power Query. Once loaded, it looks like this:

The employee history in the Power Query Editor, with dates shown as date-times and a null End Date on the current record.

2. Group by employee and number the records

Section titled “2. Group by employee and number the records”
  • Use Group By on User ID, with the operation All Rows and the new column name Details:

The Group By dialog in Basic mode, grouping by User ID into a new column called Details with the operation All Rows.

  • Click OK. Each employee is now one row, with all their records held in a nested table in Details.
  • Add three custom columns, each of which adds an index column to the nested table. The indexes start at 1, 0 and 2, and are called Record, Previous and Next:

The Custom Column dialog creating a column called Index with the formula Table.AddIndexColumn([Details], “Record”, 1, 1). The Custom Column dialog creating a column called Index 2 with the formula Table.AddIndexColumn([Index], “Previous”, 0, 1). The Custom Column dialog creating a column called Index 3 with the formula Table.AddIndexColumn([Index 2], “Next”, 2, 1).

Each new column builds on the one before it, so Index 3 holds all three indexes. The query now looks like this:

One row per User ID, with Details, Index, Index 2 and Index 3 columns that each hold a nested table.

  • Remove every column except User ID and Index 3.
  • Expand all the fields in Index 3.

Each record now carries its own number in Record, the number of the record before it in Previous, and the number of the record after it in Next. The numbering restarts at 1 for each employee:

The expanded table, showing Job Grade, Full or Part and the three index columns Record, Previous and Next, with Previous one lower and Next one higher than Record.

  • An employee’s current record has no end date. Replace those nulls with 31/12/9998, using this M code:
= Table.ReplaceValue(#"Expanded Index 3",null,#datetime(9998, 12, 31, 0, 0, 0),Replacer.ReplaceValue,{"End Date"})

4. Merge the query with itself to fetch the previous and next dates

Section titled “4. Merge the query with itself to fetch the previous and next dates”
  • Use Merge Queries to merge the query with itself. On the first table select User ID and then Previous, and on the second select User ID and then Record. Use a Left Outer join:

The Merge dialog with Staff Table with Join & Leave Logic as both tables, matching Previous in the first table to Record in the second, and Join Kind set to Left Outer. The User ID columns, also selected, are scrolled out of view.

  • From the merged column, expand only End Date, and name it Previous End Date. Each record now shows the end date of the record before it:

The table with a Previous End Date column. The first record of each employee shows null, because no earlier record exists.

  • Merge the query with itself again in the same way, this time matching User ID and Next to User ID and Record:

The Merge dialog matching Next in the first table to Record in the second, with Join Kind set to Left Outer.

  • From the new merged column, expand only Start Date, and name it Next Start Date:

The table with both Previous End Date and Next Start Date columns. The last record of each employee shows null in Next Start Date, and the rows are no longer in order.

  • Sort by User ID and then by Start Date, because the merges can change the order of the rows.
  • Remove the Previous and Next columns, which are no longer needed.
  • An employee’s first record has no previous end date. Replace those nulls with 01/01/1900, using this M code:
= Table.ReplaceValue(#"Removed Index Columns",null,#datetime(1900, 1, 1, 0, 0, 0),Replacer.ReplaceValue,{"Previous End Date"})
  • An employee’s last record has no next start date. Replace those nulls with 31/12/9999, using this M code:
= Table.ReplaceValue(#"Fill Blank Previous End Dates",null,#datetime(9999, 12, 31, 0, 0, 0),Replacer.ReplaceValue,{"Next Start Date"})
  • Change all four date columns to the date type, using this M code:
= Table.TransformColumnTypes(#"Fill Blank Next Start Dates",{{"Start Date", type date}, {"End Date", type date}, {"Previous End Date", type date}, {"Next Start Date", type date}})

With Previous End Date and Next Start Date on every record, four columns can now classify each change.

  • Add two custom columns that test the gaps between records. External Join is “Y” when the record starts more than 30 days after the previous one ended, and External Leave is “Y” when the next record starts more than 30 days after this one ends:

The Custom Column dialog creating External Join with the formula if Duration.Days(Duration.From([Start Date]-[Previous End Date])) > 30 then “Y” else “N”. The Custom Column dialog creating External Leave with the formula if Duration.Days(Duration.From([Next Start Date]-[End Date])) > 30 then “Y” else “N”.

  • Add two conditional columns that take the opposite value. Internal Join is “N” where External Join is “Y”, and “Y” otherwise. Internal Leave does the same with External Leave:

The Add Conditional Column dialog creating Internal Join: if External Join equals Y then N, otherwise Y. The Add Conditional Column dialog creating Internal Leave: if External Leave equals Y then N, otherwise Y.

The query now ends with four new columns:

Previous End Date and Next Start Date followed by External Join, External Leave, Internal Join and Internal Leave. The first record of each employee is an external join, because its previous end date is 01/01/1900.

Every change of record is now classified as external or internal.

  • Name the query Staff Table with Join & Leave Logic.

The finished M code for the query is:

let
Source = Excel.CurrentWorkbook(){[Name="Inventory_Table"]}[Content],
#"Grouped Rows" = Table.Group(Source, {"User ID"}, {{"Details", each _, type table}}),
#"Added Index" = Table.AddColumn(#"Grouped Rows", "Index", each Table.AddIndexColumn([Details], "Record", 1, 1)),
#"Added Index 2" = Table.AddColumn(#"Added Index", "Index 2", each Table.AddIndexColumn([Index], "Previous", 0, 1)),
#"Added Index 3" = Table.AddColumn(#"Added Index 2", "Index 3", each Table.AddIndexColumn([Index 2], "Next", 2, 1)),
#"Removed Columns" = Table.RemoveColumns(#"Added Index 3",{"Details", "Index", "Index 2"}),
#"Expanded Index 3" = Table.ExpandTableColumn(#"Removed Columns", "Index 3", {"First Name", "Last Name", "Gender", "Start Date", "End Date", "Country", "Office", "Department", "Job Grade", "Full or Part", "Record", "Previous", "Next"}, {"First Name", "Last Name", "Gender", "Start Date", "End Date", "Country", "Office", "Department", "Job Grade", "Full or Part", "Record", "Previous", "Next"}),
#"Fill Blank End Dates" = Table.ReplaceValue(#"Expanded Index 3",null,#datetime(9998, 12, 31, 0, 0, 0),Replacer.ReplaceValue,{"End Date"}),
#"Merged Queries" = Table.NestedJoin(#"Fill Blank End Dates",{"User ID", "Previous"},#"Fill Blank End Dates",{"User ID", "Record"},"Reordered Columns",JoinKind.LeftOuter),
#"Show Previous End Date" = Table.ExpandTableColumn(#"Merged Queries", "Reordered Columns", {"End Date"}, {"Previous End Date"}),
#"Merged Queries1" = Table.NestedJoin(#"Show Previous End Date",{"User ID", "Next"},#"Show Previous End Date",{"User ID", "Record"},"Expanded Reordered Columns",JoinKind.LeftOuter),
#"Show Next Start Date" = Table.ExpandTableColumn(#"Merged Queries1", "Expanded Reordered Columns", {"Start Date"}, {"Next Start Date"}),
#"Sort by Users then Start Date" = Table.Sort(#"Show Next Start Date",{{"User ID", Order.Ascending}, {"Start Date", Order.Ascending}}),
#"Removed Index Columns" = Table.RemoveColumns(#"Sort by Users then Start Date",{"Previous", "Next"}),
#"Fill Blank Previous End Dates" = Table.ReplaceValue(#"Removed Index Columns",null,#datetime(1900, 1, 1, 0, 0, 0),Replacer.ReplaceValue,{"Previous End Date"}),
#"Fill Blank Next Start Dates" = Table.ReplaceValue(#"Fill Blank Previous End Dates",null,#datetime(9999, 12, 31, 0, 0, 0),Replacer.ReplaceValue,{"Next Start Date"}),
#"Changed Date Field Types" = Table.TransformColumnTypes(#"Fill Blank Next Start Dates",{{"Start Date", type date}, {"End Date", type date}, {"Previous End Date", type date}, {"Next Start Date", type date}}),
#"Added External Join" = Table.AddColumn(#"Changed Date Field Types", "External Join", each if Duration.Days(Duration.From([Start Date]-[Previous End Date])) > 30 then "Y" else "N"),
#"Added External Leave" = Table.AddColumn(#"Added External Join", "External Leave", each if Duration.Days(Duration.From([Next Start Date]-[End Date])) > 30 then "Y" else "N"),
#"Added Internal Join" = Table.AddColumn(#"Added External Leave", "Internal Join", each if [External Join] = "Y" then "N" else "Y"),
#"Added Internal Leave" = Table.AddColumn(#"Added Internal Join", "Internal Leave", each if [External Leave] = "Y" then "N" else "Y")
in
#"Added Internal Leave"

The columns added above could instead be added to the original employee table as calculated columns in DAX, without transforming the table in M.

Where a column can be worked out in advance, M is usually the better place for it, although DAX works too and the choice is often a matter of taste. DAX is the stronger choice for more complex calculations, such as running totals or running averages, as the Salary DAX article HR - Showing Employee Salaries & analysing trends in Actual and Average Running (Pro-Rated) Salaries over time shows.

The DAX formulas that create the same columns, and calculate tenure from them, are in the Calculated Columns section of the Tenure DAX article HR - Showing Inventory Movements & analysing Total Running Tenure over time.

To see each employee’s profile and running tenure in a given month or quarter, every record has to be paired with every month. The steps below build a monthly calendar that spans the data, cross join it with the employee query, and then add the tenure and headcount columns.

  • Create a query that returns the earliest start or end date in the employee table, using this M code:
let
Source = Excel.CurrentWorkbook(){[Name="Inventory_Table"]}[Content],
#"Removed Other Columns" = Table.SelectColumns(Source,{"Start Date", "End Date"}),
#"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Removed Other Columns", {}, "Attribute", "Value"),
#"Removed Columns" = Table.RemoveColumns(#"Unpivoted Columns",{"Attribute"}),
#"Changed Type" = Table.TransformColumnTypes(#"Removed Columns",{{"Value", type date}}),
#"Filtered Rows" = Table.SelectRows(#"Changed Type", let earliest = List.Min(#"Changed Type"[Value]) in each [Value] = earliest),
Value = #"Filtered Rows"{0}[Value]
in
Value
  • Name the query Min_Date.
  • Create a query that returns the later of the latest start or end date in the table and today’s date, using this M code:
let
Source = Excel.CurrentWorkbook(){[Name="Inventory_Table"]}[Content],
#"Removed Other Columns" = Table.SelectColumns(Source,{"Start Date", "End Date"}),
#"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Removed Other Columns", {}, "Attribute", "Value"),
#"Removed Columns" = Table.RemoveColumns(#"Unpivoted Columns",{"Attribute"}),
#"Changed Type" = Table.TransformColumnTypes(#"Removed Columns",{{"Value", type date}}),
#"Filtered Rows" = Table.SelectRows(#"Changed Type", let latest = List.Max(#"Changed Type"[Value]) in each [Value] = latest),
Value = #"Filtered Rows"{0}[Value],
Today = DateTime.Date(DateTime.LocalNow()),
#"Max Date" = List.Max({Value, Today}, 1)
in
#"Max Date"
  • Name the query Max_Date.
  • Using Min_Date and Max_Date, create a calendar with one row per month, using this M code:
let
End_Date = Max_Date,
Start_Date = Min_Date,
durationDays = Duration.Days( End_Date - Start_Date ) + 1,
dates = List.Dates(Start_Date, durationDays, #duration( 1, 0, 0, 0 ) ),
#"Converted to Table" = Table.FromList(dates, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
#"Changed Type" = Table.TransformColumnTypes(#"Converted to Table",{{"Column1", type date}}),
#"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"Column1", "Date"}}),
#"Inserted Start of Month" = Table.AddColumn(#"Renamed Columns", "Start of Month", each Date.StartOfMonth([Date]), type date),
#"Inserted End of Month" = Table.AddColumn(#"Inserted Start of Month", "End of Month", each Date.EndOfMonth([Date]), type date),
#"Removed Other Columns" = Table.SelectColumns(#"Inserted End of Month",{"Start of Month", "End of Month"}),
#"Removed Duplicates" = Table.Distinct(#"Removed Other Columns"),
#"Inserted Year" = Table.AddColumn(#"Removed Duplicates", "Year", each Date.Year([Start of Month]), Int64.Type),
#"Inserted Quarter" = Table.AddColumn(#"Inserted Year", "Quarter", each Text.Insert(Text.From(Date.QuarterOfYear([Start of Month])), 0, "Q"), Int64.Type),
#"Merged Columns" = Table.CombineColumns(Table.TransformColumnTypes(#"Inserted Quarter", {{"Year", type text}, {"Quarter", type text}}, "en-GB"),{"Year", "Quarter"},Combiner.CombineTextByDelimiter(" - ", QuoteStyle.None),"Year & Quarter"),
#"Inserted Year1" = Table.AddColumn(#"Merged Columns", "Year", each Date.Year([Start of Month]), Int64.Type),
#"Inserted Quarter1" = Table.AddColumn(#"Inserted Year1", "Quarter", each Date.QuarterOfYear([Start of Month]), Int64.Type),
#"Merged Columns1" = Table.CombineColumns(Table.TransformColumnTypes(#"Inserted Quarter1", {{"Year", type text}, {"Quarter", type text}}, "en-GB"),{"Year", "Quarter"},Combiner.CombineTextByDelimiter("", QuoteStyle.None),"Year & Quarter - Number"),
#"Changed Type1" = Table.TransformColumnTypes(#"Merged Columns1",{{"Year & Quarter - Number", Int64.Type}}),
#"Inserted Year2" = Table.AddColumn(#"Changed Type1", "Year", each Date.Year([Start of Month]), Int64.Type)
in
#"Inserted Year2"

The query lists every day between the two dates, reduces them to one row per month, and adds a year and a quarter in two forms: as text such as “2020 - Q1”, and as a number such as 20201 that sorts correctly. The calendar looks like this:

The monthly calendar with Start of Month, End of Month, Year & Quarter, Year & Quarter - Number and Year columns, from March 2020 to December 2020.

  • Name the query Monthly Calendar.

4. Cross join the calendar with the employee query

Section titled “4. Cross join the calendar with the employee query”
  • Create a new query that references Monthly Calendar, and add a custom column whose formula is just the name of the employee query:

The Custom Column dialog creating a column called Custom with the formula #“Staff Table with Join & Leave Logic”.

Every month now holds a copy of the whole employee table:

The monthly calendar with a new Custom column that holds a nested table on every row.

  • Expand all the fields in the new column. This completes the cross join, so every employee record now appears once for every month.

The next columns show each employee’s total running tenure, and their running tenure in each job grade.

  • Add a custom column called Days in Service, which counts the days each record contributes up to the end of the month, using this M code:
= List.Max({Number.From([Start Date]),
List.Min({Number.From(Date.AddDays(DateTime.Date(DateTime.LocalNow()), 1)), Number.From(Date.AddDays([End Date], 1)), Number.From(Date.AddDays([End of Month], 1))})}) - Number.From([Start Date])

The formula ends the count at whichever comes first of today, the end of the month and the record’s own end date. It never ends the count before the record’s start date, so a record that begins after the month ends contributes zero days.

  • Use Group By in Advanced mode, grouping by Start of Month and User ID. Add an All Rows column called Details, and a Sum of Days in Service called Running Days in Service:

The Group By dialog in Advanced mode, grouping by Start of Month and User ID, with an All Rows output called Details and a Sum of Days in Service called Running Days in Service.

  • Expand all the fields in Details except Start of Month and User ID, which the grouping has already kept.

Every record for an employee in a given month now carries that employee’s total days of service up to that month:

Internal Join, Internal Leave, Days in Service and Running Days in Service columns, where Running Days in Service repeats the employee’s total on each of their records for the month.

  • Convert the running days into years with a custom column called Running Tenure Yrs:

The Custom Column dialog creating Running Tenure Yrs with the formula [Running Days in Service] / 365.25.

  • Remove the Running Days in Service column, which is no longer needed.
  • Add a Tenure Bracket custom column that puts each running tenure into a band, from “Nil” and “0 to 0.5 years” up to “More than 8 years”:

The Custom Column dialog creating Tenure Bracket with a chain of if, then and else tests on Running Tenure Yrs.

  • Group the data again in the same way, but this time group by Job Grade as well, and call the sum Running Days in Service - by Job Grade:

The Group By dialog in Advanced mode, grouping by Start of Month, User ID and Job Grade, with an All Rows output called Details and a Sum of Days in Service.

  • Expand all the fields in Details except Start of Month, User ID and Job Grade, which the grouping has already kept.

The last columns of the query now look like this:

Internal Leave, Days in Service, Running Tenure Yrs, Tenure Bracket and Running Days in Service - by Job Grade columns.

  • Convert the job grade days into months with a custom column called Running Tenure Mnths - by Job Grade:

The Custom Column dialog creating Running Tenure Mnths - by Job Grade with the formula [#“Running Days in Service - by Job Grade”] * 12 / 365.25.

  • Remove the Running Days in Service - by Job Grade column, which is no longer needed.
  • Add five custom columns that mark, for each month, whether the employee started in it, left in it, or was part of the headcount at its start or its end:

The Custom Column dialog creating Started in Month ? with the formula if ([Start Date] >= [Start of Month] and [Start Date] <= [End of Month]) then “Y” else “N”.

The Custom Column dialog creating Left in Month ? with the formula if ([End Date] >= [Start of Month] and [End Date] <= [End of Month]) then “Y” else “N”.

The Custom Column dialog creating Part of HeadCount at Start of Month with the formula if ([Start Date] < [Start of Month] and [End Date] >= [Start of Month]) then “Y” else “N”.

The Custom Column dialog creating Part of HeadCount at End of Month with the formula if ([Start Date] <= [End of Month] and [End Date] > [End of Month]) then “Y” else “N”.

The Custom Column dialog creating Part of HeadCount Start or End of Month, which is “Y” when either of the two headcount columns is “Y”.

  • Reorder the columns if you wish.

Loaded to a worksheet, the query shows the columns added before and after the cross join, and one row for every record in every month. This is the inventory table:

The loaded table, showing Job Grade and Full or Part, then the join and leave columns, Days in Service, Running Tenure Yrs, Tenure Bracket, Running Tenure Mnths - by Job Grade and the five Y or N headcount columns.

The cross-joined query is the source for HR inventory movement and tenure reports, such as the PivotTables below. The Tenure DAX article HR - Showing Inventory Movements & analysing Total Running Tenure over time shows how to build them on a Data Model that sits on top of this table.

A PivotTable titled Running Tenure (Years) - each Individual, filtered to the headcount at the end of the month, showing each employee’s department, job grade and average running tenure in years.

A PivotTable titled Individual Count - by Department, Job Grade, & Tenure Bracket, counting employees by department, job grade and tenure bracket, split by gender.

A PivotTable titled Running Tenure Split by Job Grade (Months) - each Individual, showing months of tenure for each User ID in each job grade, with colour-scaled cells.

The finished M code for the cross join query is:

let
Source = #"Monthly Calendar",
#"Added Custom" = Table.AddColumn(Source, "Custom", each #"Staff Table with Join & Leave Logic"),
#"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"User ID", "First Name", "Last Name", "Gender", "Start Date", "End Date", "Country", "Office", "Department", "Job Grade", "Full or Part", "External Join", "External Leave", "Internal Join", "Internal Leave"}, {"User ID", "First Name", "Last Name", "Gender", "Start Date", "End Date", "Country", "Office", "Department", "Job Grade", "Full or Part", "External Join", "External Leave", "Internal Join", "Internal Leave"}),
#"Added Days in Service" = Table.AddColumn(#"Expanded Custom", "Days in Service", each List.Max({Number.From([Start Date]),
List.Min({Number.From(Date.AddDays(DateTime.Date(DateTime.LocalNow()), 1)), Number.From(Date.AddDays([End Date], 1)), Number.From(Date.AddDays([End of Month], 1))})}) - Number.From([Start Date])),
#"Grouped Rows Total Tenure" = Table.Group(#"Added Days in Service", {"Start of Month", "User ID"}, {{"Details", each _, type table [Start of Month=date, End of Month=date, #"Year & Quarter"=text, #"Year & Quarter - Number"=nullable number, Year=number, User ID=text, First Name=text, Last Name=text, Gender=text, Start Date=date, End Date=date, Country=text, Office=text, Department=text, Job Grade=text, Full or Part=text, External Join=text, External Leave=text, Internal Join=text, Internal Leave=text, Days in Service=number]}, {"Running Days in Service", each List.Sum([Days in Service]), type number}}),
#"Expanded Details" = Table.ExpandTableColumn(#"Grouped Rows Total Tenure", "Details", {"End of Month", "Year & Quarter", "Year & Quarter - Number", "Year", "First Name", "Last Name", "Gender", "Start Date", "End Date", "Country", "Office", "Department", "Job Grade", "Full or Part", "External Join", "External Leave", "Internal Join", "Internal Leave", "Days in Service"}, {"End of Month", "Year & Quarter", "Year & Quarter - Number", "Year", "First Name", "Last Name", "Gender", "Start Date", "End Date", "Country", "Office", "Department", "Job Grade", "Full or Part", "External Join", "External Leave", "Internal Join", "Internal Leave", "Days in Service"}),
#"Added Tenure Yrs" = Table.AddColumn(#"Expanded Details", "Running Tenure Yrs", each [Running Days in Service] / 365.25),
#"Removed Columns1" = Table.RemoveColumns(#"Added Tenure Yrs",{"Running Days in Service"}),
#"Added Tenure Bracket" = Table.AddColumn(#"Removed Columns1", "Tenure Bracket", each if [Running Tenure Yrs] = 0 then "Nil" else if [Running Tenure Yrs] > 0 and [Running Tenure Yrs] <= 0.5 then "0 to 0.5 years" else if [Running Tenure Yrs] > 0.5 and [Running Tenure Yrs] <= 1 then "0.5 to 1 year" else if [Running Tenure Yrs] > 1 and [Running Tenure Yrs] <= 3 then "1 to 3 years" else if [Running Tenure Yrs] > 3 and [Running Tenure Yrs] <= 5 then "3 to 5 years" else if [Running Tenure Yrs] > 5 and [Running Tenure Yrs] <= 8 then "5 to 8 years" else if [Running Tenure Yrs] > 8 and [Running Tenure Yrs] <= 1000 then "More than 8 years" else "Other"),
#"Grouped Rows JG Tenure" = Table.Group(#"Added Tenure Bracket", {"Start of Month", "User ID", "Job Grade"}, {{"Details", each _, type table [Start of Month=date, User ID=text, End of Month=nullable date, #"Year & Quarter"=nullable text, #"Year & Quarter - Number"=nullable number, Year=nullable number, First Name=nullable text, Last Name=nullable text, Gender=nullable text, Start Date=nullable date, End Date=nullable date, Country=nullable text, Office=nullable text, Department=nullable text, Job Grade=nullable text, Full or Part=nullable text, External Join=nullable text, External Leave=nullable text, Internal Join=nullable text, Internal Leave=nullable text, Days in Service=nullable number, Running Tenure Yrs=number, Tenure Bracket=text]}, {"Running Days in Service - by Job Grade", each List.Sum([Days in Service]), type nullable number}}),
#"Expanded Details1" = Table.ExpandTableColumn(#"Grouped Rows JG Tenure", "Details", {"End of Month", "Year & Quarter", "Year & Quarter - Number", "Year", "First Name", "Last Name", "Gender", "Start Date", "End Date", "Country", "Office", "Department", "Full or Part", "External Join", "External Leave", "Internal Join", "Internal Leave", "Days in Service", "Running Tenure Yrs", "Tenure Bracket"}, {"End of Month", "Year & Quarter", "Year & Quarter - Number", "Year", "First Name", "Last Name", "Gender", "Start Date", "End Date", "Country", "Office", "Department", "Full or Part", "External Join", "External Leave", "Internal Join", "Internal Leave", "Days in Service", "Running Tenure Yrs", "Tenure Bracket"}),
#"Added Custom2" = Table.AddColumn(#"Expanded Details1", "Running Tenure Mnths - by Job Grade", each [#"Running Days in Service - by Job Grade"] * 12 / 365.25),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom2",{"Running Days in Service - by Job Grade"}),
#"Started in Month" = Table.AddColumn(#"Removed Columns", "Started in Month ?", each if ([Start Date] >= [Start of Month] and [Start Date] <= [End of Month]) then "Y" else "N"),
#"Left in Month" = Table.AddColumn(#"Started in Month", "Left in Month ?", each if ([End Date] >= [Start of Month] and [End Date] <= [End of Month]) then "Y" else "N"),
#"HC at Start of Month" = Table.AddColumn(#"Left in Month", "Part of HeadCount at Start of Month", each if ([Start Date] < [Start of Month] and [End Date] >= [Start of Month]) then "Y" else "N"),
#"HC at End of Month" = Table.AddColumn(#"HC at Start of Month", "Part of HeadCount at End of Month", each if ([Start Date] <= [End of Month] and [End Date] > [End of Month]) then "Y" else "N"),
#"HC at Start or End of Month" = Table.AddColumn(#"HC at End of Month", "Part of HeadCount Start or End of Month", each if [Part of HeadCount at Start of Month] = "Y" or [Part of HeadCount at End of Month] = "Y" then "Y" else "N"),
#"Reordered Columns" = Table.ReorderColumns(#"HC at Start or End of Month",{"Start of Month", "End of Month", "Year & Quarter", "Year & Quarter - Number", "Year", "User ID", "First Name", "Last Name", "Gender", "Start Date", "End Date", "Country", "Office", "Department", "Job Grade", "Full or Part", "External Join", "External Leave", "Internal Join", "Internal Leave", "Days in Service", "Running Tenure Yrs", "Tenure Bracket", "Running Tenure Mnths - by Job Grade", "Started in Month ?", "Left in Month ?", "Part of HeadCount at Start of Month", "Part of HeadCount at End of Month"})
in
#"Reordered Columns"