Nick Burns
Member
- Joined
- May 24, 2017
- Messages
- 162
- Reaction score
- 0
- Points
- 16
- Excel Version(s)
- Office 365
Borrowing some code from here Add blank row at each change in value
I made a couple of adjustments allowing the ability add multiple rows. I also changed how the Column Types get reset.
Given that the code is several years old, I was wondering if there are any improvements that could be made?
I made a couple of adjustments allowing the ability add multiple rows. I also changed how the Column Types get reset.
Code:
let
AddEmptyRows = (Tbl as table, HeadersToGroup as list, groupKind as number, optional NumberOfRows as nullable number) as table =>
let
FirstRecord = Tbl{0},
InsertCount = if NumberOfRows = null or NumberOfRows = 0 then 1 else NumberOfRows,
// create an empty record
EmptyRecord =
let
FieldNames = Record.FieldNames(FirstRecord),
EmptyValues = List.Repeat({""},Record.FieldCount(FirstRecord))
in
Record.FromList(EmptyValues,FieldNames),
// group table
GrpTable = Table.Group(
Tbl,
HeadersToGroup,
{"Tbl", each _, type table},
groupKind ),
// transform table column of grouped table adding Number Of Rows at the bottom
TransformTblCol = Table.TransformColumns(
GrpTable,
{"Tbl", each Table.InsertRows(_, Table.RowCount(_), List.Repeat({EmptyRecord},InsertCount))}
),
// Select the tbl column and expand it
ExpandColumns = Table.ExpandTableColumn(
Table.SelectColumns(
TransformTblCol,
{"Tbl"}
),
"Tbl",
Record.FieldNames(FirstRecord)
),
// Restore Column Types
Result = Value.ReplaceType(ExpandColumns,Value.Type(Tbl))
in
Result
in
AddEmptyRows
Given that the code is several years old, I was wondering if there are any improvements that could be made?