Forum Discussion
Replace Value in multiple columns with different conditional rules on each column
Hello, I am new to M and I need some help.
I need to perform an operation cycling like a for loop.
I have a List containing the Columns Name in text format on which I want to run the cycling operation, that list is called DynamicCol.
If I could run a simple for loop I would cycle trough the column list and run this command:
So the code could read as follows:
for i from 0 to List.Count(DynamicCol) step 1
Table.ReplaceValue(mytab2,each Record.Field(_,DynamicCol{i}),each if Record.Field(_,"Levelcount")=Number.FromText(Text.End(DynamicCol{i},1)) then Record.Field(_,"newPN") else "",Replacer.ReplaceText,{DynamicCol{i}})
end
I can't sort out how to do it in M....
Here below more detailed info on the full process I am facing:
I have s starting table Like this one:
The Levelcount column identify the hierarcy level, and is obtained as follows
=Table.AddColumn(mytab1, "Levelcount", each Text.Length([ID])-Text.Length(Text.Replace([ID],".",""))+1)
The source file can change and different file can have different levels (e.g. just couple or up to 9 levels).
after splitting the ID Column this is the table becomes as follows:
I need now to run a conditional operation on each of the "Level i" columns, since the source file (which is a BOM) can have different levels each time, and I need to make this operation dynamic.
The outpout I am looking for is this (which I can get if I apply the above rule on each row separately in a simpler form).
I managed to do write the operation on a single column with an index that can to be used for the cycling (the 0 in the below code):
Table.ReplaceValue(mytab2,each Record.Field(_,DynamicCol{0}),each if Record.Field(_,"Levelcount")=Number.FromText(Text.End(DynamicCol{0},1)) then Record.Field(_,"newPN") else "",Replacer.ReplaceText,{DynamicCol{0}})
My understanding is that with List.Generate I could mimic the for loop, to cycle trough the DynamicCol length and perform the above operation on all the columns, however is not clear to me how to do that, or if there are other cleaver and simpler methods.
Thanks in advance
Hi EmanueleP,
I think your choice could be List.Accumulate as the input is already arranged in a list. This is the snippet of how you could apply it.
let ... mytab2= ...//table DynamicCol = Table.ColumnNames(mytab2), fProcessColumns = (t as table, col as text)=> Table.ReplaceValue( t, each Record.Field(_,col), each if Record.Field(_,"Levelcount")=Number.FromText(Text.End(col,1)) then Record.Field(_,"newPN") else "",Replacer.ReplaceText,{col} ), Process = List.Accumulate(DynamicCol, mytab2, (a, n)=> fProcessColumns(a,n)) in ProcessP.S. I would sill recommend to read the article referred by Anonymous, this is quite interesting code/solution.
Kind regards,
John
3 Replies
- AnonymousNot applicable
Hi EmanueleP - The following article might contain a function that will flatten hierarchy to produce the result that you are looking for Dynamically Flatten a Parent-Child Hierarchy using Power Query M - Pivotal BI
- jbwtpMemorable Member
Hi EmanueleP,
I think your choice could be List.Accumulate as the input is already arranged in a list. This is the snippet of how you could apply it.
let ... mytab2= ...//table DynamicCol = Table.ColumnNames(mytab2), fProcessColumns = (t as table, col as text)=> Table.ReplaceValue( t, each Record.Field(_,col), each if Record.Field(_,"Levelcount")=Number.FromText(Text.End(col,1)) then Record.Field(_,"newPN") else "",Replacer.ReplaceText,{col} ), Process = List.Accumulate(DynamicCol, mytab2, (a, n)=> fProcessColumns(a,n)) in ProcessP.S. I would sill recommend to read the article referred by Anonymous, this is quite interesting code/solution.
Kind regards,
John
- EmanuelePRegular Visitor
Thanks, this works!! I will also have a look at the article which seems interesting.