Created
November 4, 2024 23:02
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ( 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