Forum Discussion
henrys1995
2 years agoRegular Visitor
Difference in Months - Columns
Hi all, How can Power Query give the below required output: Source Data: Employee ID Employee Name Start Date End Date 2321 ...
- 2 years ago
Hi henrys1995 - yes, absolutely. You can modify this part of the script to customize the format of the date.
jennratten
2 years agoSuper User
Hello - this is how you can do it in Power Query. Basically you would add a column which contains a list of months from the start to the end date, expand the list to rows and pivot it to columns.
Script
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8srPyFMIzs0syVDSUTI0MjYBUqb6hvpGBkbGYBF9YygnNhYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Employee = _t, ID = _t, #"Start Date" = _t, #"End Date" = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Employee", type text}, {"ID", Int64.Type}, {"Start Date", type date}, {"End Date", type date}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each List.Transform ( List.Select ( List.Dates([Start Date], Number.From ( [End Date] ) - Number.From ( [Start Date] ),#duration(1,0,0,0)), each Date.Day ( _ ) = 1 ), each Date.ToText ( _, "MMMyy" ) ) ),
#"Expanded Custom" = Table.ExpandListColumn(#"Added Custom", "Custom"),
#"Removed Other Columns" = Table.SelectColumns(#"Expanded Custom",{"Employee", "ID", "Custom"}),
#"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Removed Other Columns", {{"Custom", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(#"Removed Other Columns", {{"Custom", type text}}, "en-US")[Custom]), "Custom", "Employee", List.Count)
in
#"Pivoted Column"
Result
FYI, when needing to show dates in columns instead of rows there is usually a better way of designing the solution, such as using a data table in the model.
- henrys19952 years agoRegular Visitor
jennratten thanks - is it possible have instead of MMMyy - have the first of each month so 01/01/24 as a date
- jennratten2 years agoSuper User
Hi henrys1995 - yes, absolutely. You can modify this part of the script to customize the format of the date.