Forum Discussion
List Generate, accumulation which re starts when at text value
Hi,
so List.Generate to accumulate wiith text , i have been trying various accumulations with a mixture of number and text ,
so fist inlcude the text ; { 2,2,3, "a", 3,3 } = { 2,4, 7, "a", 10, 13} , then skip { 2,4, 7, 7, 10, 13} and last re start { 2,4, 7, "a2, 3,6}
so i have used a try otherwise for the re start but would like to see if there is another way to achiee this , the first two
i jut put in for interest, they are not a problem.
accumulate;
let
alist = {"a", 2, 3, 2, 2, "b", "c", 2, 3, 2, "d", 2, 2, 2},
result = List.Generate(
() => [x = 0, y = alist{0}, z = if Value.Is(y, type number) then y else 0],
each [x] < List.Count(alist),
each [x = [x] + 1, y = alist{x}, z = if Value.Is(y, type number) then [z] + y else [z]],
each if Value.Is([y], type number) then [z] else [y]
)
in
result
skip ;
let
alist = {"a", 2, 3, 2, 2, "b", "c", 2, 3, 2, "d", 2, 2, 2},
result = List.Generate(
() => [x = 0, y = alist{0}, z = if Value.Is(y, type number) then y else 0],
each [x] < List.Count(alist),
each [x = [x] + 1, y = alist{x}, z = if Value.Is(y, type number) then [z] + y else [z]],
each [z]
)
in
result
re start ;
let
alist = {"a", 2, 3, 2, 2, "b", "c", 2, 3, 2, "d", 2, 2, 2},
result = List.Generate(
() => [x = 0, y = alist{0}, z = y],
each [x] < List.Count(alist),
each [x = [x] + 1, y = alist{x}, z = try [z] + y otherwise y],
each [z]
)
in
result
So trying to do is get a re - start without try / otherwise;, i
I have been trying things like this
= List.Generate( ()=>
[x = 0 ,y = alist {0} , z = y ] ,
each [x] < List.Count( alist ),
each [ x = [x] + 1, y = alist {x}, z =
if Value.Is( y, type number) then [y] + y else y] ,
each [z] )but so far i have not come up wiht a method , any suggestions ?
Richard.
Hi
= List.Generate(
() => [x = 0, y = alist{0}],
each [x] < List.Count(alist),
each [x = [x] + 1,
y = List.Sum(List.LastN(List.FirstN(alist, x+1), each Value.Is(_, type number))) ?? alist{x}],
each [y]
)Stéphane
The edit option is revealed by clicking the three vertical dots that appear above your message at the right margin of the column.
If you are looking for an alternative to try ... otherwise you could use try ... catch.
Or you can check the data type and proceed depending on what that shows.
10 Replies
- slorinSuper User
Hi
= List.Generate(
() => [x = 0, y = alist{0}],
each [x] < List.Count(alist),
each [x = [x] + 1,
y = List.Sum(List.LastN(List.FirstN(alist, x+1), each Value.Is(_, type number))) ?? alist{x}],
each [y]
)Stéphane
- DickenPost Prodigy
I'm learning something form this, ; = List.LastN( alist , (x)=> Value.Is(x, type
number) ) , was not something i know, only using a number , so thanks for this.
- DickenPost Prodigy
Just, to add, ( there does not seem to be an edit option) it is using List.Generate , not other functions,
so a way to restart but avoinding try / otherwise.- ronrsnfldSuper User
The edit option is revealed by clicking the three vertical dots that appear above your message at the right margin of the column.
If you are looking for an alternative to try ... otherwise you could use try ... catch.
Or you can check the data type and proceed depending on what that shows. - tharunkumarRTKSuper User
I think this pattern would give you the same result as 'restart', that too it wont use try and otherwise
let alist = {"a", 2, 3, 2, 2, "b", "c", 2, 3, 2, "d", 2, 2, 2}, result = List.Generate( () => [x = 0, y = alist{0}, z = y], each [x] < List.Count(alist), each [x = [x] + 1, y = alist{x}, z = if Value.Is(y, type text) or Value.Is([z], type text) then y else [z] + y], each [z] ) in resultConnect on LinkedIn
read my blogs here: https://www.techietips.co.in/
Did I answer your question? Mark my post as a solution! If I helped you, click on the Thumbs Up to give Kudos.
Proud to be a Super User!
- DickenPost Prodigy
I'm not sure it works, i hvave errors thrown up, i'll work through some more and see if i can find why,
have you got it to work.
- DickenPost Prodigy
ronr,
Re edit, thanks, thoiught it must be possible. Good to know.
- DickenPost Prodigy
have made a manual example of how the generate / fist / last N works; with value is;;
let alist = let alist = {1,2,2,3,2,"a",2,3,2,2} in alist, Custom1 = List.LastN( List.FirstN( alist, 1), (x)=> Value.Is( x, type number )), Custom2 = List.LastN( List.FirstN( alist, 2 ), (x)=> Value.Is( x, type number )), Custom3 = List.LastN( List.FirstN( alist, 3), (x)=> Value.Is( x, type number )), Custom4 = List.LastN( Lustiest( alist, 5), (x)=> Value.Is( x, type number )), Custom5 = List.LastN( List.FirstN( alist, 6), (x)=> Value.Is( x, type number )), = empty list , fails at first value.is test , Custom8 = List.Sum( Custom5 ) ?? "x", List.Sum( {} ) = null coerced to value. Custom6 = List.LastN( List.FirstN( alist, 1), (x)=> Value.Is( x, type number )), Custom7 = List.LastN( List.FirstN( alist, 2 ), (x)=> Value.Is( x, type number )) in Custom7u
- DickenPost Prodigy
To answer my own question, i have ;
let alist = {1, 2, 2, "a", 2, 3, 2, 2, "b", 2, 2, 3, 3, 2, 2, "a", 2, 2, 2}, gen = List.Generate( () => [x = 0, y = alist{0}, z = y], each [x] < List.Count(alist), each [ x = [x] + 1, y = alist{x}, z = if Value.Is(y, type number) then if Value.Is([z], type number) then [z] + y else y else y ], each [z] ) in gen