Transforming and summarizing salary data with Power Query in Excel

Ozzyy

New Member
Joined
May 1, 2021
Messages
6
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hello! I have an Excel table with a row showing each time an employee has received a salary increase. Using Power Query, I'd like to summarize this information to show me one record per employee, with a column showing their salary as at the end of each year (going back to 2015 or whenever they joined the company). I'm new to Power Query and struggling to get started on this one. Any advice or guidance would be greatly appreciated.

Here's what I'm working with:

Source Table
EMPLOYEE IDSALARYSALARY START DATE
10156,0005/07/18
10157,0001/01/19
10168,0001/01/21
37865,0007/04/19
37873,0001/01/20
37881,0001/01/21
37883,0002/01/21
63865,0006/10/19
63870,0001/01/20
63876,0001/01/21
75168,0001/04/21
87085,0007/01/18
87095,0001/01/19
870101,0001/01/21


Desired Output in Power Query
EMPLOYEE ID2018 SALARY2019 SALARY2020 SALARY2021 SALARY
10156,00057,00068,00068,000
378-65,00073,00083,000
638-65,00070,00076,000
751---68,000
87085,00095,00095,000101,000


Thank you!
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"EMPLOYEE ID", Int64.Type}, {"SALARY", Int64.Type}, {"SALARY START DATE", type date}}),
    #"Inserted Year" = Table.AddColumn(#"Changed Type", "Year", each Date.Year([SALARY START DATE]), Int64.Type),
    #"Removed Columns" = Table.RemoveColumns(#"Inserted Year",{"SALARY START DATE"}),
    #"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Removed Columns", {{"Year", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(#"Removed Columns", {{"Year", type text}}, "en-US")[Year]), "Year", "SALARY", List.Sum),
    #"Reordered Columns" = Table.ReorderColumns(#"Pivoted Column",{"EMPLOYEE ID", "2018", "2019", "2020", "2021"})
in
    #"Reordered Columns"
 
Upvote 0
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"EMPLOYEE ID", Int64.Type}, {"SALARY", Int64.Type}, {"SALARY START DATE", type date}}),
    #"Inserted Year" = Table.AddColumn(#"Changed Type", "Year", each Date.Year([SALARY START DATE]), Int64.Type),
    #"Removed Columns" = Table.RemoveColumns(#"Inserted Year",{"SALARY START DATE"}),
    #"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Removed Columns", {{"Year", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(#"Removed Columns", {{"Year", type text}}, "en-US")[Year]), "Year", "SALARY", List.Sum),
    #"Reordered Columns" = Table.ReorderColumns(#"Pivoted Column",{"EMPLOYEE ID", "2018", "2019", "2020", "2021"})
in
    #"Reordered Columns"

Thanks for your help!

This is very close. There are just two things I'm noticing aren't quite working:
1. If an employee has more than one salary increase in a year (like employee id 378), I'd like to just see the latest one from that year, rather than adding them together
2. In years where an employee didn't get a salary increase, I'm looking for it to carry across the value from their previous year's increase (rather than null)

Current output from your code:
EMPLOYEE ID2018201920202021
10156,00057,00068,000
37865,00073,000164,000
63865,00070,00076,000
75168,000
87085,00095,000101,000


Desired output:
EMPLOYEE ID2018201920202021
10156,00057,00057,00068,000
37865,00073,00083,000
63865,00070,00076,000
75168,000
87085,00095,00095,000101,000


Is this possible to do? And thanks again; I really appreciate the help.
 
Upvote 0
Maybe someone else can make it happen. I don't see a viable solution using PQ for what you are asking. You may need to go with a VBA solution instead of pivoting data in PQ.
 
Upvote 0
Please try


Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Year = Table.TransformColumns(Source,{{"SALARY START DATE", Date.Year, Int64.Type}}),
    Grouped = Table.Group(Year, {"EMPLOYEE ID"}, {{"Count", each 
        let a = Table.FromColumns({[EMPLOYEE ID],[SALARY], [SALARY START DATE] & List.Distinct(Year[SALARY START DATE])}) 
        in Table.FillDown(Table.Sort(a,{"Column3",0}),{"Column2"})
    }}),
    Expanded = Table.ExpandTableColumn(Grouped, "Count", {"Column2", "Column3"}, {"Column2", "Column3"}),
    Pivoted = Table.Pivot(Table.TransformColumnTypes(Expanded, {{"Column3", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(Expanded, {{"Column3", type text}}, "en-US")[Column3]), "Column3", "Column2", List.Max)
in
    Pivoted

EMPLOYEE IDSALARYSALARY START DATEEMPLOYEE ID2018201920202021
1015600007-May-1810156,00057,00057,00068,000
1015700001-Jan-1937865,00073,00083,000
1016800001-Jan-2163865,00070,00076,000
3786500004-Jul-1975168,000
3787300001-Jan-2087085,00095,00095,000101,000
3788100001-Jan-21
3788300001-Feb-21
6386500010-Jun-19
6387000001-Jan-20
6387600001-Jan-21
7516800004-Jan-21
8708500001-Jul-18
8709500001-Jan-19
87010100001-Jan-21
 
Upvote 0
Please try


Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Year = Table.TransformColumns(Source,{{"SALARY START DATE", Date.Year, Int64.Type}}),
    Grouped = Table.Group(Year, {"EMPLOYEE ID"}, {{"Count", each
        let a = Table.FromColumns({[EMPLOYEE ID],[SALARY], [SALARY START DATE] & List.Distinct(Year[SALARY START DATE])})
        in Table.FillDown(Table.Sort(a,{"Column3",0}),{"Column2"})
    }}),
    Expanded = Table.ExpandTableColumn(Grouped, "Count", {"Column2", "Column3"}, {"Column2", "Column3"}),
    Pivoted = Table.Pivot(Table.TransformColumnTypes(Expanded, {{"Column3", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(Expanded, {{"Column3", type text}}, "en-US")[Column3]), "Column3", "Column2", List.Max)
in
    Pivoted

Thank you, this is great progress. The only outstanding issue with it is I need to ensure that, for years where there is more than one salary change for an employee, it picks up the latest one, rather than just the highest salary for them that year. The logic in your code is very close to what I need: the only scenario where it isn't quite right is if someone's salary decreases in a year (e.g., if they move from full-time to part-time). Here's a revised data set and what I'm seeing:

Source Table:
EMPLOYEE IDSALARYSALARY START DATE
10156,000May 7, 2018
10157,000January 1, 2019
10168,000January 1, 2021
37865,000July 4, 2019
37873,000January 1, 2020
37881,000January 1, 2021
37840,000February 1, 2021
63865,000June 10, 2019
63870,000January 1, 2020
63876,000January 1, 2021
75168,000January 4, 2021
87085,000July 1, 2018
87095,000January 1, 2019
870101,000January 1, 2021


OUTPUT FROM YOUR CODEDESIRED OUTPUT
EMPLOYEE ID2018201920202021EMPLOYEE ID2018201920202021
10156,00057,00057,00068,00010156,00057,00057,00068,000
37865,00073,00081,00037865,00073,00040,000
63865,00070,00076,00063865,00070,00076,000
75168,00075168,000
87085,00095,00095,000101,00087085,00095,00095,000101,000


Any thoughts on this last piece? Thanks again!
 
Upvote 0
Maybe someone else can make it happen. I don't see a viable solution using PQ for what you are asking. You may need to go with a VBA solution instead of pivoting data in PQ.
Interesting thought, thanks. Would this be complicated to tackle with VBA (and could the resulting table be loaded into Power Query)?
 
Upvote 0
Please try


Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    AYear = Table.AddColumn(Source, "Year", each Date.Year([SALARY START DATE])),
    Grouped = Table.Group(AYear, {"EMPLOYEE ID"}, {{"Count", each
        let a = Table.FromColumns({[EMPLOYEE ID],[SALARY], [SALARY START DATE], [Year] & List.Distinct(AYear[Year])})
        in Table.Sort(Table.Distinct(Table.FillUp(Table.Sort(a,{{"Column4",1},{"Column3",1}}),Table.ColumnNames(a)),"Column4"),{"Column4",0})
    }}),
    Expanded = Table.ExpandTableColumn(Grouped, "Count", {"Column2", "Column4"}),
    Pivoted = Table.Pivot(Table.TransformColumnTypes(Expanded, {{"Column4", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(Expanded, {{"Column4", type text}}, "en-US")[Column4]), "Column4", "Column2")
in
    Pivoted
 
Upvote 0
Solution
Please try


Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    AYear = Table.AddColumn(Source, "Year", each Date.Year([SALARY START DATE])),
    Grouped = Table.Group(AYear, {"EMPLOYEE ID"}, {{"Count", each
        let a = Table.FromColumns({[EMPLOYEE ID],[SALARY], [SALARY START DATE], [Year] & List.Distinct(AYear[Year])})
        in Table.Sort(Table.Distinct(Table.FillUp(Table.Sort(a,{{"Column4",1},{"Column3",1}}),Table.ColumnNames(a)),"Column4"),{"Column4",0})
    }}),
    Expanded = Table.ExpandTableColumn(Grouped, "Count", {"Column2", "Column4"}),
    Pivoted = Table.Pivot(Table.TransformColumnTypes(Expanded, {{"Column4", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(Expanded, {{"Column4", type text}}, "en-US")[Column4]), "Column4", "Column2")
in
    Pivoted
That did it! Thanks so much; I was really stumped on this and your solution is perfect. Much appreciated!
 
Upvote 0

Forum statistics

Threads
1,215,628
Messages
6,125,900
Members
449,271
Latest member
bergy32204

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top