Forum Discussion
insert values in nested lists except last
- 20 days ago
Another solution
= List.ReplaceValue(
List.RemoveLastN(Source,1) ,
each _ &{"X"},
null,
(x,y,z)=>y(x))
& List.LastN(Source, 1)Stéphane
Hi Dicken,
You can simplify your first approach by transforming everything except the last item directly, then appending the last item unchanged:
let
Source = {{1,2,3}, {4,5,6}, {7,8,9}, {10,11,12}},
Result =
List.Transform(
List.RemoveLastN(Source, 1),
each _ & {"X"}
)
& List.LastN(Source, 1)
in
Result
This gives:
{{1,2,3,"X"}, {4,5,6,"X"}, {7,8,9,"X"}, {10,11,12}}List.RemoveLastN removes the final nested list, List.Transform appends "X" to everything remaining, and List.LastN adds the untouched final item back.
It avoids needing the positions or a subsequent List.ReplaceRange.
AI-assisted drafting: AI was used to help structure and phrase this response. I reviewed and validated the technical content before posting.
Hi,
= List.Transform(
List.Zip({
alist,
List.Repeat({{"X"}}, List.Count(alist)-1) & {{}} }),
List.Combine)
Stéphane
- slorin20 days ago
Super User
To avoid adding a "null" to the last item
= List.Transform(
List.Zip({
List.RemoveLastN(Source, 1),
List.Repeat({{"X"}}, List.Count(Source)-1)}),
List.Combine)
& {List.Last(Source)}