Forum Discussion
chloebelle
3 years agoRegular Visitor
Combining several line items into one column in PowerQuery
Hopefully I am phrasing this coherently. Basically, if an employee clocks out for their break and has several time durations during one shift, the output from my job's scheduling system will be split...
- 3 years ago
1 Fill down the columns of name, date, positions
2 Transform the data type of Shift duration to number
3 group the data by the columns of name, date, positions, and sum the shift duration as Actual.
jgeddes
3 years agoSuper User
If you need to keep the columns that indicate the individual shift durations that make up the actual sum you can use the following code.
let
Source =
Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8srPyFMIzs0syVDSUTI01vUqzdM1MgayHZOT80vzSjLz0oEcUz1TIGmhFKsTrQRkQJABWBAshmqKCQ5TLPWMEMYoAFkwbKhnYgxmYpplisMsIz0zc+xmwQ1CFjXWM8BlgxkOG0z0zHHYAPY3ph0GEO8BJWIB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"LEGAL NAME" = _t, DATE = _t, POSITIONS = _t, #"SHIFT DURATION" = _t, #"SCH. SHIFT DURATION" = _t]),
changeDataTypes =
Table.TransformColumnTypes(Source,{{"LEGAL NAME", type text}, {"DATE", type date}, {"POSITIONS", type text}, {"SHIFT DURATION", type number}, {"SCH. SHIFT DURATION", Int64.Type}}),
replaceNulls =
//replace blank spaces with null to allow fill down
Table.ReplaceValue(changeDataTypes,"",null,Replacer.ReplaceValue,{"LEGAL NAME", "POSITIONS"}),
replaceNulls1 =
Table.ReplaceValue(replaceNulls," ",null,Replacer.ReplaceValue,{"LEGAL NAME", "POSITIONS"}),
fillDownColumns =
//fill down to replace blank spaces and allow grouping
Table.FillDown(replaceNulls1,{"LEGAL NAME", "DATE", "POSITIONS", "SCH. SHIFT DURATION"}),
//group by all columns except SHIFT DURATION, choosing all rows (no aggregation)
groupRows =
Table.Group(fillDownColumns, {"LEGAL NAME", "DATE", "POSITIONS", "SCH. SHIFT DURATION"}, {{"innerTable", each _, type table [LEGAL NAME=nullable text, DATE=nullable date, POSITIONS=nullable text, SHIFT DURATION=nullable number, SCH. SHIFT DURATION=nullable number]}}),
//select only the SHIFT DURATION column in the inner tables
addGroupedTable =
Table.TransformColumns(groupRows, {"innerTable", each Table.SelectColumns(_, "SHIFT DURATION")}),
//add column that sums SHIFT DURATION to get actual time worked
addActualColumn =
Table.AddColumn(addGroupedTable, "ACTUAL", each List.Sum([innerTable][SHIFT DURATION]), type number),
//transpose the inner tables (rows to columns)
transposeInnerTable =
Table.TransformColumns(addActualColumn, {"innerTable", each Table.Transpose(_)}),
//create a column of lists (needed to build the inner table expansion)
addListCount =
Table.AddColumn(transposeInnerTable, "listCount", each Table.ColumnNames([innerTable])),
//get distinct list of column names (used in expansion and renaming of columns)
getDistinctList =
List.Distinct(List.Combine(addListCount[listCount])),
//convert the list to a table in order to manipulate the list values
convertListToTable =
Table.FromList(getDistinctList, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
//replace the 'Column' value with 'SHIFT DURATION' (for renaming of expanded columns)
replaceColumnValue =
Table.ReplaceValue(convertListToTable,"Column","SHIFT DURATION",Replacer.ReplaceText,{"Column1"}),
//convert the table back into a list
convertToList =
replaceColumnValue[Column1],
//create a list of lists for renaming of expanded columns
createRenamingList =
List.Zip({getDistinctList, convertToList}),
//expand the inner table using the created list (getDistinctList) to expand all possible columns
expandInnerTable =
Table.ExpandTableColumn(addListCount, "innerTable", getDistinctList, getDistinctList),
//rename the columns using the created list of lists (createRenamingList)
renameColumns =
Table.RenameColumns(expandInnerTable,createRenamingList),
//remove un-needed column
removeColumn =
Table.RemoveColumns(renameColumns,{"listCount"})
in
removeColumn