Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
Dicken
Helper IV
Helper IV

Power Query Accumulate with text in list

Hi, 
 is there another way to deal with an accumulation when text values appear on the list ; I have broken and then used insert ; 
let
alist = {"A", 2, 3, 4, 3, 2, 3, 2},
slist = List.Skip(alist, 1),
gen = List.Generate(() => [x = 0, y = slist{0}],
each [x] < List.Count(slist),
each [x = [x] + 1, y = [y] + slist{x}],
each [y]
)
in
List.InsertRange(gen, 0, List.FirstN(alist, 1))

 

I don't know if you could put it back together in more effecient way or perhaps not break up in the first place  I 
have tried a try / othewise but not got it working as insert would not be great in the text appeared and some random point. 

Richard
i

1 ACCEPTED SOLUTION
OwenAuger
Super User
Super User

Hi @Dicken 

Is it possible that text values can be anywhere in the list, and should all be ignored for the cumulative sum?

e.g.

This:

 

{"A", 2, 3, "B", 4, 3, 2, 3, "C", 2}

 

becomes this:

 

{"A", 2, 5, "B", 9, 12, 14, 17, "C", 19}

 

Here is an example of how I might handle this more general case (as well as your original example):

 

let
  alist = {"A", 2, 3, "B", 4, 3, 2, 3, "C", 2},
  CumulativeList = List.Generate(
    () => [
      Index          = 0,
      Value          = alist{0},
      IsNumber       = Value.Is(Value, type nullable number),
      NumericalValue = if IsNumber then Value else 0,
      CumulativeSum  = NumericalValue,
      DisplayValue   = if IsNumber then CumulativeSum else Value
    ],
    each [Index] < List.Count(alist),
    each [
      Index          = [Index] + 1,
      Value          = alist{Index},
      IsNumber       = Value.Is(Value, type nullable number),
      NumericalValue = if IsNumber then Value else 0,
      CumulativeSum  = [CumulativeSum] + NumericalValue,
      DisplayValue   = if IsNumber then CumulativeSum else Value
    ],
    each [DisplayValue]
  )
in
  CumulativeList

 

 


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

View solution in original post

5 REPLIES 5
Dicken
Helper IV
Helper IV

Thanks I have not had a proper chance to work through it all but it looks good. 

Vijay_A_Verma
Super User
Super User

One alternative

 

let
    alist = {"A", 2, 3, 4, "B", 3, 2, "C", 3, 2, "D"},
    BuffList = List.Buffer(alist),
    Running_Total = List.Generate(()=>[x=BuffList{0}, y=List.Buffer(List.Transform(BuffList, (x)=> if Value.FromText(x) is number then x else 0)), i=0], each [i]<List.Count([y]), each [i=[i]+1, x=if Value.FromText(BuffList{i}) is text then BuffList{i} else List.Sum(List.FirstN(y, i+1)), y = [y]], each [x])
in
    Running_Total

 

ronrsnfld
Super User
Super User

If the first item in the list will always be the one you need replace, you can use:

 {List.First(alist)} & gen
OwenAuger
Super User
Super User

Hi @Dicken 

Is it possible that text values can be anywhere in the list, and should all be ignored for the cumulative sum?

e.g.

This:

 

{"A", 2, 3, "B", 4, 3, 2, 3, "C", 2}

 

becomes this:

 

{"A", 2, 5, "B", 9, 12, 14, 17, "C", 19}

 

Here is an example of how I might handle this more general case (as well as your original example):

 

let
  alist = {"A", 2, 3, "B", 4, 3, 2, 3, "C", 2},
  CumulativeList = List.Generate(
    () => [
      Index          = 0,
      Value          = alist{0},
      IsNumber       = Value.Is(Value, type nullable number),
      NumericalValue = if IsNumber then Value else 0,
      CumulativeSum  = NumericalValue,
      DisplayValue   = if IsNumber then CumulativeSum else Value
    ],
    each [Index] < List.Count(alist),
    each [
      Index          = [Index] + 1,
      Value          = alist{Index},
      IsNumber       = Value.Is(Value, type nullable number),
      NumericalValue = if IsNumber then Value else 0,
      CumulativeSum  = [CumulativeSum] + NumericalValue,
      DisplayValue   = if IsNumber then CumulativeSum else Value
    ],
    each [DisplayValue]
  )
in
  CumulativeList

 

 


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

Really very impressed with this, I have not got all quite figured but have if nothing else have learned 
Value.Is , as I would have used Value.Type ( x) = type , which has led to me finding Value.As , 

is there not end to m functions. 

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

Find out what's new and trending in the Fabric Community.

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.