Skip to content

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 example uses three sets of data.

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.

A wide payroll journal for January 2020. Each row is an employee or contractor, with columns for Month, Employee Code, Supplier Reference, Company Name, First Name, Last Name, Business - P&L, Business - Department and Business - Sub-Department, followed by one column per nominal ledger line, such as 5000 Gross Wages and 5007 Employer Tax, with the code above the description.

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:

A forecast table with the same descriptive columns, then one column per month from Jan-20 to Dec-20, a Total column and a Total row.

The budget, which is set once at the start of the year, has the same layout:

A budget table with the same layout as the forecast: the descriptive columns, one column per month from Jan-20 to Dec-20, and totals.

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.

  • 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):

The January journal in the Power Query Editor with generic headings from Column1 to Column7. Row 1 is empty, row 2 holds the real headings, and the data starts on row 3.

  • 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:

The transposed table. Column1 holds the nominal ledger codes, such as 5000 and 5002, and is null for the descriptive fields. Column2 holds the field names, such as Month, Employee Code and Gross Wages, and the remaining columns hold one person each.

  • 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:

The merged column, holding values such as =>Month, =>Employee Code and 5000=>Gross Wages, beside one column per person.

  • Transpose the table back to its original layout.
  • Promote the first row to headers, so that the table looks like this:

The table with headings such as =>Month, =>Employee Code, =>Supplier Reference and =>Business - P&L, and one row per person.

  • 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):

The unpivoted table, with the descriptive columns followed by Attribute.1 holding the code, such as 5000, Attribute.2 holding the description, such as Gross Wages, and Value holding the amount.

  • 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:

The finished query with columns Business - P&L, Business - Department, Business - Sub-Department, NL - Code, NL - Description and Value, typed as text and decimal number.

  • 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:

The Queries pane with a folder called Monthly Journals holding twelve queries, from Jan Payroll to Dec Payroll.

  • 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:

A worksheet table titled Actuals - Master Payroll Record - All Months - (All Lines!), with one row per person, nominal ledger line and month, and the amount in a Value column.

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.

  • 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:

A two-column list of nominal ledger codes and descriptions, from 5000 Gross Wages to 6000 Contracted Staff, sorted by code.

  • 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:

A worksheet table titled NL Line - Mapping, where an Expense Line column, headed in red with the words Define Here!, maps each code to Salary, Furlough, ERs Tax, Pension, Other or Contract Staff.

  • 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:

The NL - Lines with Mapping query in the Power Query Editor, with NL - Code, NL - Description and Expense Line columns.

  • 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.

The Merge dialog with NL - Lines above and NL - Lines with Mapping below, matching NL - Code and NL - Description on each side, with Join Kind set to Left Outer.

  • Expand the Expense Line field, so that the query looks like this:

The NL - Lines query with an Expense Line column, and the Applied Steps from Source to Expanded NL - Lines with Mapping.

  • 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:

The Merge dialog with Actuals - Payroll (All Lines) above and NL - Lines with Mapping below, matching NL - Code and NL - Description, with Join Kind set to Left Outer.

  • Expand the Expense Line field, and move the column to sit before NL - Code:

The actuals query with Expense Line placed between Business - Sub-Department and NL - Code.

  • Load the query to the worksheet again. It holds the same data as before, with the added Expense Line column:

The worksheet table Actuals - Master Payroll Record - All Months - (All Lines!), now with an Expense Line column before NL - Code.

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"
  • Load the forecast into the Power Query Editor, where it looks like this:

The forecast in the Power Query Editor with generic headings. Row 1 holds the real headings, such as Employee Code and Supplier Reference, and the data starts on row 2.

  • 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:

A worksheet table titled Forecast - Employees (Salary Line Only! - 5000) and Contract Staff (Line 6000), with one row per person and month, and Date and Value columns.

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"
  • 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:

A worksheet table titled Budget - Employees (Salary Line Only! - 5000) and Contract Staff (Line 6000), with one row per person and month, and Date and Value columns.

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.

A sheet titled Comparison - P&L Level, with three blocks for each P&L: Budget by P&L with a total for each expense line, Actual / Forecast by P&L with one column per month and a Latest Actual Month cell, and Variance by P&L with amounts, percentages and red or green markers.

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.

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.

A sheet titled Comparison - Unit Level, with slicers for the actuals cut-off month and a PivotTable that lists each person by P&L and department, with budget, actual or forecast, and variance in GBP and per cent for each month.

The DAX article Finance - Payroll Tracking shows how to build this comparison.