Skip to content

Instantly share code, notes, and snippets.

@m-dekorte
Created November 4, 2024 23:02
Show Gist options
  • Select an option

  • Save m-dekorte/56b996be4f51a6eaab6132e1e46db30d to your computer and use it in GitHub Desktop.

Select an option

Save m-dekorte/56b996be4f51a6eaab6132e1e46db30d to your computer and use it in GitHub Desktop.
fillRightOrLeft M function | fill values right (or left) with optional fill columns and number of top rows.
( tbl as table, optional fillCols as list, optional topRows as number, optional fillLeft as logical) as table =>
let
fillFunction = if fillLeft ?? false then Table.FillUp else Table.FillDown,
t = if topRows = null then tbl else Table.FirstN(tbl, topRows),
allCols = Table.ColumnNames( tbl ),
fillSideways = Table.TransformRows( t,
each _ & Record.FromTable(
fillFunction( Record.ToTable(
Record.SelectFields(_, fillCols ?? allCols, MissingField.Ignore)
), {"Value"})
)
),
result = if topRows = null
then Value.ReplaceType(
Table.FromRecords( fillSideways ),
Value.Type(tbl)
)
else Value.ReplaceType(
Table.FromRecords( fillSideways ) & Table.Skip( tbl, topRows ),
Value.Type(tbl)
)
in
result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment