Finance - Payroll Tracking
This page shows how Power Query can prepare payroll data so that actual and forecast staff costs can be tracked against the budget, which is a comparison most finance directors and operations directors want to see every month.
The data
Section titled “The data”The example uses three sets of data.
Actual payroll
Section titled “Actual payroll”The actual payroll arrives as a wide journal, with one row per person and one column per nominal ledger line. The rows at the top are the amounts paid to employees, and the rows further down are the amounts paid to contractors. The journal below covers January only.
![]()
Forecast
Section titled “Forecast”A forecast is also available, for line 5000 Gross Wages for employees and line 6000 Contract Staff for contractors, with one column for each month of the year:
![]()
Budget
Section titled “Budget”The budget, which is set once at the start of the year, has the same layout:
![]()
To compare the three sets of data quickly, both by P&L and by person, they first need to be in the same shape. Doing that once in Power Query makes every formula built on them much simpler. The steps below reshape the data, and the sections after them show how to compare the three sets and calculate the variances.
1. Transform the actuals
Section titled “1. Transform the actuals”- Start with the actuals, because they are the most detailed. Load the January journal into the Power Query Editor, where it looks like this (only some of the columns are shown):
![]()
- Remove the empty rows with this M code:
= Table.SelectRows(Source, each not List.IsEmpty(List.RemoveMatchingItems(Record.FieldValues(_), {"", null})))- Transpose the table, so that it looks like this:
![]()
- Filter out the rows that are null in Column2.
- Merge Column1 and Column2 with the delimiter
=>, so that the code and the description sit in one column:
![]()
- Transpose the table back to its original layout.
- Promote the first row to headers, so that the table looks like this:
![]()
- Select every column that is not a nominal ledger column, and choose Unpivot Other Columns.
The amounts now sit in a single Value column, with one row for each person and nominal ledger line. The attribute is split at => into the code and the description (only some of the columns are shown):
![]()
- Rename the columns to remove the
=>from the headings, and name the code and description columns NL - Code and NL - Description. - Set a data type on every column, so that the result looks like this:
![]()
-
Name the query Jan Payroll.
-
Repeat the process for February to December by duplicating the query and changing the source in its first step. This assumes the workbook already has an empty placeholder sheet for each month:
![]()
- Create one more query that appends the twelve monthly queries, and name it Actuals - Payroll (All Lines). Its first step is:
= Table.Combine({#"Jan Payroll", #"Feb Payroll", #"Mar Payroll", #"Apr Payroll", #"May Payroll", #"Jun Payroll", #"Jul Payroll", #"Aug Payroll", #"Sep Payroll", #"Oct Payroll", #"Nov Payroll", #"Dec Payroll"})- Load the query to a worksheet, where it looks like this:
![]()
The query still needs one more column, which groups the nominal ledger lines for reporting. That column comes from a mapping table, built in the next step.
2. Build a nominal ledger mapping table
Section titled “2. Build a nominal ledger mapping table”- Create a blank query called NL - Lines, and give it the same first step as the combined actuals query:
= Table.Combine({#"Jan Payroll", #"Feb Payroll", #"Mar Payroll", #"Apr Payroll", #"May Payroll", #"Jun Payroll", #"Jul Payroll", #"Aug Payroll", #"Sep Payroll", #"Oct Payroll", #"Nov Payroll", #"Dec Payroll"})- Remove every column except NL - Code and NL - Description.
- Remove duplicates.
- Sort by NL - Code in ascending order, so that the query lists each nominal ledger line once:
![]()
- Load the table to a worksheet.
- In the worksheet, add a column to the table called Expense Line, and type in a broader grouping for each nominal ledger line. These groupings are the lines the comparisons will report on:
![]()
- Import this table, with its new Expense Line column, back into Power Query.
- Name the imported query NL - Lines with Mapping. Once data types are set, it looks like this:
![]()
- Go back to the NL - Lines query and add a Merge Queries step. It joins NL - Lines to NL - Lines with Mapping on both NL - Code and NL - Description, using a Left Outer join. Because NL - Lines with Mapping reads the table that NL - Lines itself loads, the query refers back to its own output. This is what lets the typed mappings survive a refresh.
![]()
- Expand the Expense Line field, so that the query looks like this:
![]()
- Load the table back to the worksheet. If Excel adds a second column called Expense Line 2, delete it.
The finished M code for the query is:
let Source = Table.Combine({#"Jan Payroll", #"Feb Payroll", #"Mar Payroll", #"Apr Payroll", #"May Payroll", #"Jun Payroll", #"Jul Payroll", #"Aug Payroll", #"Sep Payroll", #"Oct Payroll", #"Nov Payroll", #"Dec Payroll"}), #"Removed Other Columns" = Table.SelectColumns(Source,{"NL - Code", "NL - Description"}), #"Removed Duplicates" = Table.Distinct(#"Removed Other Columns"), #"Sorted Rows" = Table.Sort(#"Removed Duplicates",{{"NL - Code", Order.Ascending}}), #"Merged Queries" = Table.NestedJoin(#"Sorted Rows", {"NL - Code", "NL - Description"}, #"NL - Lines with Mapping", {"NL - Code", "NL - Description"}, "NL - Lines with Mapping", JoinKind.LeftOuter), #"Expanded NL - Lines with Mapping" = Table.ExpandTableColumn(#"Merged Queries", "NL - Lines with Mapping", {"Expense Line"}, {"Expense Line"})in #"Expanded NL - Lines with Mapping"3. Add the expense line to the combined actuals
Section titled “3. Add the expense line to the combined actuals”- Go back to the Actuals - Payroll (All Lines) query and merge it with NL - Lines with Mapping, matching on NL - Code and NL - Description with a Left Outer join:
![]()
- Expand the Expense Line field, and move the column to sit before NL - Code:
![]()
- Load the query to the worksheet again. It holds the same data as before, with the added Expense Line column:
![]()
The finished M code for the query is:
let #"Consolidated Query" = Table.Combine({#"Jan Payroll", #"Feb Payroll", #"Mar Payroll", #"Apr Payroll", #"May Payroll", #"Jun Payroll", #"Jul Payroll", #"Aug Payroll", #"Sep Payroll", #"Oct Payroll", #"Nov Payroll", #"Dec Payroll"}), #"Merged Queries" = Table.NestedJoin(#"Consolidated Query", {"NL - Code", "NL - Description"}, #"NL - Lines with Mapping", {"NL - Code", "NL - Description"}, "NL - Lines with Mapping", JoinKind.LeftOuter), #"Expanded NL - Lines with Mapping" = Table.ExpandTableColumn(#"Merged Queries", "NL - Lines with Mapping", {"Expense Line"}, {"Expense Line"}), #"Reordered Columns" = Table.ReorderColumns(#"Expanded NL - Lines with Mapping",{"Month", "Employee Code", "Supplier Reference", "Company Name", "First Name", "Last Name", "Business - P&L", "Business - Department", "Business - Sub-Department", "Expense Line", "NL - Code", "NL - Description", "Value"})in #"Reordered Columns"4. Transform the forecast
Section titled “4. Transform the forecast”- Load the forecast into the Power Query Editor, where it looks like this:
![]()
- Filter out the rows where Column1 is null or holds the word Total.
- Promote the first row to headers.
- Remove the Total column.
- Select every column whose heading is not a date, and choose Unpivot Other Columns.
- Set the correct data type on each column.
- Name the query Forecasts - Payroll (Salary Line).
- Load the result to a worksheet, where it looks like this:
![]()
The finished M code for the query is:
let Source = Excel.CurrentWorkbook(){[Name="Forecast_Salary_Data"]}[Content], #"Filtered Rows" = Table.SelectRows(Source, each ([Column1] <> null and [Column1] <> "Total")), #"Promoted Headers" = Table.PromoteHeaders(#"Filtered Rows", [PromoteAllScalars=true]), #"Removed Columns" = Table.RemoveColumns(#"Promoted Headers",{"Total"}), #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Removed Columns", {"Employee Code", "Supplier Reference", "Company Name", "First Name", "Last Name", "Business - P&L", "Business - Department", "Business - Sub-Department"}, "Attribute", "Value"), #"Changed Type" = Table.TransformColumnTypes(#"Unpivoted Other Columns",{{"Attribute", type datetime}}), #"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"Attribute", "Date"}}), #"Changed Type1" = Table.TransformColumnTypes(#"Renamed Columns",{{"Date", type date}, {"Value", type number}, {"Business - Sub-Department", type text}, {"Business - Department", type text}, {"Business - P&L", type text}, {"Last Name", type text}, {"First Name", type text}, {"Company Name", type text}, {"Supplier Reference", type text}, {"Employee Code", type text}})in #"Changed Type1"5. Transform the budget
Section titled “5. Transform the budget”- The budget has the same layout as the forecast, so the same steps apply. Name the query Budget - Payroll (Salary Line).
- Loaded to a worksheet, the result looks like this:
![]()
The finished M code for the query is:
let Source = Excel.CurrentWorkbook(){[Name="Budget_Salary_Data"]}[Content], #"Filtered Rows" = Table.SelectRows(Source, each ([Column1] <> null and [Column1] <> "Total")), #"Promoted Headers" = Table.PromoteHeaders(#"Filtered Rows", [PromoteAllScalars=true]), #"Removed Columns" = Table.RemoveColumns(#"Promoted Headers",{"Total"}), #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Removed Columns", {"Employee Code", "Supplier Reference", "Company Name", "First Name", "Last Name", "Business - P&L", "Business - Department", "Business - Sub-Department"}, "Attribute", "Value"), #"Changed Type" = Table.TransformColumnTypes(#"Unpivoted Other Columns",{{"Attribute", type datetime}}), #"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"Attribute", "Date"}}), #"Changed Type1" = Table.TransformColumnTypes(#"Renamed Columns",{{"Date", type date}, {"Value", type number}, {"Business - Sub-Department", type text}, {"Business - Department", type text}, {"Business - P&L", type text}, {"Last Name", type text}, {"First Name", type text}, {"Company Name", type text}, {"Supplier Reference", type text}, {"Employee Code", type text}})in #"Changed Type1"6. Compare budget with actual and forecast by P&L
Section titled “6. Compare budget with actual and forecast by P&L”With all three sets of data in the same long shape, ordinary SUMIFS formulas can total them and show the variances.
The comparison below is made for each P&L. For the budget and the forecast, the lines other than Salary and Contract Staff are estimated as percentages of the salary line, rather than added up from person-level figures.
![]()
The pink actual and forecast table has a switch, which is a cell holding the latest month for which actuals are available. Months up to and including that month show actuals, and later months show the forecast.
Every cell in the table builds in the switch with the same pattern. This is the formula for the Salary line:
=IF( T$7 <= $AF$5, SUMIFS( Actuals___Payroll__All_Lines[[Value]:[Value]], Actuals___Payroll__All_Lines[[Month]:[Month]], 'Comparison - P&L'!T$7, Actuals___Payroll__All_Lines[[Supplier Reference]:[Supplier Reference]], "Employee", Actuals___Payroll__All_Lines[[Business - P&L]:[Business - P&L]], 'Comparison - P&L'!$S$7, Actuals___Payroll__All_Lines[[Expense Line]:[Expense Line]], 'Comparison - P&L'!$S8 ), SUMIFS( Forecasts___Payroll__Salary_Line[[Value]:[Value]], Forecasts___Payroll__Salary_Line[[Date]:[Date]], 'Comparison - P&L'!T$7, Forecasts___Payroll__Salary_Line[[Supplier Reference]:[Supplier Reference]], "Employee", Forecasts___Payroll__Salary_Line[[Business - P&L]:[Business - P&L]], 'Comparison - P&L'!$S$7 ))The variance table on the right shows the difference for each P&L and each expense line.
7. Download the Excel file
Section titled “7. Download the Excel file”The workbook, with all its formulas and the switch, is available here: Payroll - Tracking - Excel File
8. Compare budget with actual and forecast by person
Section titled “8. Compare budget with actual and forecast by person”The comparison by person is best built on a Data Model, because employees and contracts come and go from month to month, and a Data Model picks up those changes on refresh.
The same comparison could be built with static tables on a worksheet, but it would be slow to update and easy to get wrong. If a report needs a very particular layout, CUBE functions that read from the workbook’s Data Model are another option.
![]()
The DAX article Finance - Payroll Tracking shows how to build this comparison.