March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early bird discount ends December 31.
Register NowBe one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now
Hi,
As a starting point ;
let alist = {3,2,2,2,"a","b",2,2,2,"c",2,2,2}
in List.Accumulate( alist, {} , (s,c)=>
let x = if List.Count(s) = 0 then 0 else List.Last(List.Select(s, (x)=> Value.Is(x, type number)))
in s & { try x + c otherwise c} )
which works, but if the first value is text then this does not work, you can change the seed to {0} , and then skip ; so ;
let alist = {"a",2,2,2,"a","b",2,2,2,"c",2,2,2}
in
List.Accumulate( alist, {0} , (s,c)=>
s & { try List.Last( List.Select( s, (x)=>Value.Is(x, type number))) + c otherwise c } )
So A; why I can't see why the {} seed fails can anyone help, I'm more interested in why it fails that just a solution with no
information of links to where I can find it.
Richard.
Solved! Go to Solution.
Hi @Dicken
The core reason for the failure with the {} seed (in that the resulting list contains nulls rather than cumulative sums of numbers) is that arithmetic operators produce a null result when one of the operands is null. In this case, null + c = null.
See here for the documentation on the addition operator:
https://learn.microsoft.com/en-us/powerquery-m/m-spec-operators#addition-operator
More specifically, the {} seed version fails when alist starts with a text value because:
Possible solutions:
1. When the List.Last expression returns null, convert it to zero using the null-coalescing operator ??:
let
alist = {"b", 2, 2, 2, "a", "b", 2, 2, 2, "c", 2, 2, 2}
in
List.Accumulate(
alist,
{},
(s, c) =>
let
x =
List.Last(List.Select(s, (x) => Value.Is(x, type number))) ?? 0
in
s & {try x + c otherwise c}
)
2. Use List.Sum (which ignores nulls, unlike the + operator) to sum x & c:
let
alist = {"b", 2, 2, 2, "a", "b", 2, 2, 2, "c", 2, 2, 2}
in
List.Accumulate(
alist,
{},
(s, c) =>
let
x =
List.Last(List.Select(s, (x) => Value.Is(x, type number)))
in
s & {try List.Sum({x,c}) otherwise c}
)
Do either of these work for you?
Hi @Dicken,
Thanks for connecting with us on the Microsoft Fabric Community Forum.
Code:
= List.Accumulate(
alist,
{0}, // Start with a numeric value
(s, c) =>
s & {
try List.Last(List. Select(s, (x) => Value. Is(x, type number))) + c
otherwise c
}
)
Output:
If my answer addressed your query, kindly mark it as the Accepted Solution to assist others.
I'd also be grateful for a 'Kudos' if you found my response useful!
Hi @Dicken
The core reason for the failure with the {} seed (in that the resulting list contains nulls rather than cumulative sums of numbers) is that arithmetic operators produce a null result when one of the operands is null. In this case, null + c = null.
See here for the documentation on the addition operator:
https://learn.microsoft.com/en-us/powerquery-m/m-spec-operators#addition-operator
More specifically, the {} seed version fails when alist starts with a text value because:
Possible solutions:
1. When the List.Last expression returns null, convert it to zero using the null-coalescing operator ??:
let
alist = {"b", 2, 2, 2, "a", "b", 2, 2, 2, "c", 2, 2, 2}
in
List.Accumulate(
alist,
{},
(s, c) =>
let
x =
List.Last(List.Select(s, (x) => Value.Is(x, type number))) ?? 0
in
s & {try x + c otherwise c}
)
2. Use List.Sum (which ignores nulls, unlike the + operator) to sum x & c:
let
alist = {"b", 2, 2, 2, "a", "b", 2, 2, 2, "c", 2, 2, 2}
in
List.Accumulate(
alist,
{},
(s, c) =>
let
x =
List.Last(List.Select(s, (x) => Value.Is(x, type number)))
in
s & {try List.Sum({x,c}) otherwise c}
)
Do either of these work for you?
Just as a for the reord I've also found List.Generate works well, probably better;
let alist = {"a",3,2,"a","b",2,2,2,"c",2,2}
in List.Generate( ()=> [ x = 0 , y = 0 ],
each [x] < List.Count(alist) ,
each [x = [x]+1, y = if Value.Is( alist{x} ,type number) then [y] + alist {x} else [y] ],
each if Value.Is( alist{[x]} , type number) then [y] else alist{[x]} )
Yes, I had used sum, which also means if you're not dealing with text you can still start with {}
and not have the leadign zero . I like the use of coalece which is something I forget about,
it also the advantage of being understandable,
Thanks, so it's the null that causes the problem.
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!
Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.