Two custom M functions

JGordon11

Well-known Member
Joined
Jan 18, 2021
Messages
845
Office Version
  1. 365
Platform
  1. Windows
I have written two functions below to do seemingly simple tasks. One, TableReplaceItem, replaces a single element in a table identified by row and column number. The other, RowMultiply, multiplies each element in a table row by a number. I think there must be simpler and more efficient ways to code these for those purposes. Also the functions below would fail in the off chance that the input table has columns named the same as the interim added index and custom columns. If someone knows how to code these better, I would like to learn.

Power Query:
(tblInput as table, numRow as number, numCol as number, anyReplaceWith) as table =>
let
    lstTCN = Table.ColumnNames(tblInput),
    strCol = lstTCN{numCol},
    tbl1 = Table.AddIndexColumn(tblInput,"myIndex123456",0,1,Int64.Type),
    tbl2 = Table.AddColumn(tbl1, "myCustom1234", each if [myIndex123456] = numRow then anyReplaceWith else Table.Column(tbl1,strCol){[myIndex123456]}),
    tbl3 = Table.RemoveColumns(tbl2,{strCol, "myIndex123456"}),
    tbl4 = Table.RenameColumns(tbl3,{{"myCustom1234", strCol}}),
    tblResult = Table.ReorderColumns(tbl4,lstTCN)
in
    tblResult

So for the blue table, Data, TableReplaceItem(Data,2,2,9999) results in:

Pivot and Split.xlsm
ABCDEFGHIJKLM
1Column1Column2Column3Column4Column5Column6Column1Column2Column3Column4Column5Column6
217131925311713192531
328142026322814202632
43915212733399999212733
54101622283441016222834
65111723293551117232935
76121824303661218243036
Sheet5


and

Power Query:
(tblInput as table, numRow as number, numMultiplier as number) as table =>
let
    tbl1 = Table.AddIndexColumn(tblInput,"myIndex12345",0,1,Int64.Type),
    lstListToMultiply = Record.ToList(tbl1{numRow}),
    rcdRecordToFind = Record.FromList(lstListToMultiply,Table.ColumnNames(tbl1)),
    tblConvertToTable = Table.FromList(lstListToMultiply, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    tblMultiply = Table.AddColumn(tblConvertToTable, "Multiplication", each [Column1] * numMultiplier, type number)[Multiplication],
    rcdRecordToOutput = Record.FromList(tblMultiply,Table.ColumnNames(tbl1)),
    tbl2 = Table.ReplaceMatchingRows(tbl1, {rcdRecordToFind,rcdRecordToOutput}),
    tblResult = Table.RemoveColumns(tbl2,{"myIndex12345"})
in
    tblResult

RowMultiply(Data,3,.01) results in the green table

Pivot and Split.xlsm
ABCDEFNOPQRST
1Column1Column2Column3Column4Column5Column6Column1Column2Column3Column4Column5Column6
217131925311713192531
328142026322814202632
439152127333915212733
5410162228340.040.10.160.220.280.34
65111723293551117232935
76121824303661218243036
Sheet5
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Please try


Power Query:
(tblInput as table, numRow as number, numCol as number, anyReplaceWith) as table =>
let
    ListCol = Table.ToColumns(tblInput),
    Col = List.FirstN(ListCol{numCol},numRow ) &{anyReplaceWith} & List.Skip(ListCol{numCol},numRow+1),
    NewListCol = List.FirstN(ListCol,numCol) & {Col} & List.Skip(ListCol,numCol+1),
    tblResult = Table.FromColumns(NewListCol,Table.ColumnNames(tblInput))
in
    tblResult

and


Power Query:
(tblInput as table, numRow as number, numMultiplier as number) as table =>
let
    torow = Table.ToRows(tblInput),
    rows = List.FirstN(torow,numRow) & {List.Transform(torow{numRow},each _*numMultiplier)}  & List.Skip(torow,numRow+1),
    tblResult = Table.FromRows(rows,Table.ColumnNames(tblInput))
in
    tblResult
 
Upvote 0
Solution

Forum statistics

Threads
1,223,703
Messages
6,173,941
Members
452,539
Latest member
delvey

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top