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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Anonymous
Not applicable

Sum all values of the under the same name

Vieiraguilherme_0-1605025381278.png

Ok, i have about 2000 itens, and i need to know the sum of each one of these because i will use it as a condition for my dashboard. 
I know this probably is something dumb, but i just started at power bi, so i guess is fair use of the forum haha' 

Edit: is there anyway to create a column that shows me a acumulative result, i mean:

Vieiraguilherme_0-1605025915988.png

Something like this..
I would like to learn both ways if possible. 

1 ACCEPTED SOLUTION
PhilipTreacy
Super User
Super User

Hi @Anonymous 

I assume you want a grouped running total where the total resets when there's a change in name/Material, or from x to y in your 2nd image.

Here's a sample PBIX file with all the code shown below.

The code for a grouped running total in Power Query/M is

 

let
    GRT = (values as list, grouping as list) as list =>

let
    GRTList = List.Generate
    ( 
        ()=> [ GRT = values{0}, i = 0 ],

        each [i] < List.Count(values),

        each try 
                 if grouping{[i]} = grouping{[i] + 1} 
                 then [GRT = [GRT] + values{[i] + 1}, i = [i] + 1]
                 else [GRT = values{[i] + 1}, i = [i] + 1]
        
             otherwise [i = [i] + 1]
    ,
        each [GRT]
    )
in
    GRTList,

        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Zc1bCsAwCETRvfgdQU3tYy0h+0i7+iYtJSP9OxdGLIUaJeJdhGp6A80KPqY1bBwqBK/yvzm7M5hD6IblEAbuXx5f3QuYLc/4Hg6b+RzpGNUb", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Material = _t, Teste = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Teste", Int64.Type}}),
    BufferedValues = List.Buffer(#"Changed Type"[Teste]),
    BufferedGrouping = List.Buffer(#"Changed Type"[Material]),

    GroupedRunningTotal = Table.FromList(GRT(BufferedValues, BufferedGrouping), Splitter.SplitByNothing(), {"GRT"}, null, ExtraValues.Error),
    Columns = List.Combine({Table.ToColumns(#"Changed Type"),Table.ToColumns(GroupedRunningTotal)}),
    #"Converted to Table" = Table.FromColumns(Columns,List.Combine({Table.ColumnNames(#"Changed Type"),{"GRT"}}))

in
    #"Converted to Table"

 

which gives you this

grt.png

 

But if you want a running total where you're just adding the next value to thelast one, use this

 

let
    RT = (values as list, grouping as list) as list =>

let
    RTList = List.Generate
    ( 
        ()=> [ RT = values{0}, i = 0 ],

        each [i] < List.Count(values),

        each [RT = [RT] + values{[i] + 1}, i = [i] + 1]
    ,
        each [RT]
    )
in
    RTList,

    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Zc1bCsAwCETRvfgdQU3tYy0h+0i7+iYtJSP9OxdGLIUaJeJdhGp6A80KPqY1bBwqBK/yvzm7M5hD6IblEAbuXx5f3QuYLc/4Hg6b+RzpGNUb", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Material = _t, Teste = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Teste", Int64.Type}}),
    BufferedValues = List.Buffer(#"Changed Type"[Teste]),
    BufferedGrouping = List.Buffer(#"Changed Type"[Material]),

    RunningTotal = Table.FromList(RT(BufferedValues, BufferedGrouping), Splitter.SplitByNothing(), {"RT"}, null, ExtraValues.Error),
    Columns = List.Combine({Table.ToColumns(#"Changed Type"),Table.ToColumns(RunningTotal)}),
    #"Converted to Table" = Table.FromColumns(Columns,List.Combine({Table.ColumnNames(#"Changed Type"),{"RT"}}))

in
    #"Converted to Table"

 

which gives you this

rt.png

 

Phil


If I answered your question please mark my post as the solution.
If my answer helped solve your problem, give it a kudos by clicking on the Thumbs Up.



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


View solution in original post

1 REPLY 1
PhilipTreacy
Super User
Super User

Hi @Anonymous 

I assume you want a grouped running total where the total resets when there's a change in name/Material, or from x to y in your 2nd image.

Here's a sample PBIX file with all the code shown below.

The code for a grouped running total in Power Query/M is

 

let
    GRT = (values as list, grouping as list) as list =>

let
    GRTList = List.Generate
    ( 
        ()=> [ GRT = values{0}, i = 0 ],

        each [i] < List.Count(values),

        each try 
                 if grouping{[i]} = grouping{[i] + 1} 
                 then [GRT = [GRT] + values{[i] + 1}, i = [i] + 1]
                 else [GRT = values{[i] + 1}, i = [i] + 1]
        
             otherwise [i = [i] + 1]
    ,
        each [GRT]
    )
in
    GRTList,

        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Zc1bCsAwCETRvfgdQU3tYy0h+0i7+iYtJSP9OxdGLIUaJeJdhGp6A80KPqY1bBwqBK/yvzm7M5hD6IblEAbuXx5f3QuYLc/4Hg6b+RzpGNUb", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Material = _t, Teste = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Teste", Int64.Type}}),
    BufferedValues = List.Buffer(#"Changed Type"[Teste]),
    BufferedGrouping = List.Buffer(#"Changed Type"[Material]),

    GroupedRunningTotal = Table.FromList(GRT(BufferedValues, BufferedGrouping), Splitter.SplitByNothing(), {"GRT"}, null, ExtraValues.Error),
    Columns = List.Combine({Table.ToColumns(#"Changed Type"),Table.ToColumns(GroupedRunningTotal)}),
    #"Converted to Table" = Table.FromColumns(Columns,List.Combine({Table.ColumnNames(#"Changed Type"),{"GRT"}}))

in
    #"Converted to Table"

 

which gives you this

grt.png

 

But if you want a running total where you're just adding the next value to thelast one, use this

 

let
    RT = (values as list, grouping as list) as list =>

let
    RTList = List.Generate
    ( 
        ()=> [ RT = values{0}, i = 0 ],

        each [i] < List.Count(values),

        each [RT = [RT] + values{[i] + 1}, i = [i] + 1]
    ,
        each [RT]
    )
in
    RTList,

    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Zc1bCsAwCETRvfgdQU3tYy0h+0i7+iYtJSP9OxdGLIUaJeJdhGp6A80KPqY1bBwqBK/yvzm7M5hD6IblEAbuXx5f3QuYLc/4Hg6b+RzpGNUb", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Material = _t, Teste = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Teste", Int64.Type}}),
    BufferedValues = List.Buffer(#"Changed Type"[Teste]),
    BufferedGrouping = List.Buffer(#"Changed Type"[Material]),

    RunningTotal = Table.FromList(RT(BufferedValues, BufferedGrouping), Splitter.SplitByNothing(), {"RT"}, null, ExtraValues.Error),
    Columns = List.Combine({Table.ToColumns(#"Changed Type"),Table.ToColumns(RunningTotal)}),
    #"Converted to Table" = Table.FromColumns(Columns,List.Combine({Table.ColumnNames(#"Changed Type"),{"RT"}}))

in
    #"Converted to Table"

 

which gives you this

rt.png

 

Phil


If I answered your question please mark my post as the solution.
If my answer helped solve your problem, give it a kudos by clicking on the Thumbs Up.



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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