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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
smjzahid
Helper V
Helper V

List.Sum throwing error Power Query

Hi 

 

I have created a custom column named Units (derived from an existing column in the table). I want to create another calculated column which returns me the total for each row filtered by column Units.

The M code for the custom column UNITS is below

= Table.AddColumn(#"Filtered Rows", "Units", each if Text.Contains([Metric], "tonnes") then "Tonnes" else if Text.Contains([Metric], "Petrol ") then "Petrol Litres" else if Text.Contains([Metric], "Hours") then "Hours" else if Text.Contains([Metric], "kWh") then "KWH" else if Text.Contains([Metric], "Tour") then "Tour" else if Text.Contains([Metric], "m3") then "Metric Cube m3" else if Text.Contains([Metric], "LPG") then "LPG Litres" else if Text.Contains([Metric], "Diesel ") then "Diesel Litres" else null)

 

 

Note: The datatype for column Value is DECIMAL NUMBER

for eg. 

if [Units] = "Hours" 
then List.Sum([VALUE])
else null

 Here is the snip of my table below 

 

smjzahid_1-1597146348848.png

and the error returned is below

smjzahid_2-1597146497126.png

 

 

1 ACCEPTED SOLUTION
Icey
Community Support
Community Support

Hi @smjzahid ,

 

Please check:

running total.PNG

 

1. Add an Index column.

2. Convert [Value] to list.

3. Add a custom column.

4. Remove Index column.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8sgvLSpW0lEyVIrVQfCMUHjGKDwTMC87PAOuC86OBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Units = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Value", type number}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
    Value = #"Added Index"[Value],
    #"Added Custom Column"  = Table.AddColumn(#"Added Index", "Running Total", each if [Units] = "Hours" then List.Sum(List.Range(Value,0,[Index]+1)) else [Value]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom Column",{"Index"})
in
    #"Removed Columns"

 

running total.gif

 

BTW, .pbix file attached.

 

 

Best Regards,

Icey

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

7 REPLIES 7
Icey
Community Support
Community Support

Hi @smjzahid ,

 

Please check:

running total.PNG

 

1. Add an Index column.

2. Convert [Value] to list.

3. Add a custom column.

4. Remove Index column.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8sgvLSpW0lEyVIrVQfCMUHjGKDwTMC87PAOuC86OBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Units = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Value", type number}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
    Value = #"Added Index"[Value],
    #"Added Custom Column"  = Table.AddColumn(#"Added Index", "Running Total", each if [Units] = "Hours" then List.Sum(List.Range(Value,0,[Index]+1)) else [Value]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom Column",{"Index"})
in
    #"Removed Columns"

 

running total.gif

 

BTW, .pbix file attached.

 

 

Best Regards,

Icey

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

smjzahid
Helper V
Helper V

Hi @edhans

 

Your formual works, However it gives me a Grand Total summed up in all rows (the value is same for all row. 

I am expecting it to give me total row by row (similar to how SUMX and other iterator function works in DAX ).

 

Below is what I am getting is shown below.

 

I want the result like column Value (see the red arrow)

 

smjzahid_0-1597174666467.png

 

 

 

I had an error in my first formula but edited it within 10min. Can you verify? Post your formula here if you are still getting the wrong value.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

= Table.AddColumn(#"Changed Type", "Total Hours", each if [Units] = "Hours" 
then List.Sum(
    Table.SelectRows(
        #"Changed Type", each [Units] = "Hours")
        [VALUE])
        
else null)

Hi @smjzahid , is this what you are wanting, what is in the far right "custom" column?

edhans_0-1597180169457.png

If so, your formula should just be:

 

 

if [Units] = "Hours" then [Value] else null

 

 

No need for List.Sum. Sorry if I've overcomplicated it. I used List.Sum to total all rows where Units = "Hours" which is why it is 10 for those for rows (1+2+3+4) and null for the kWh rows.

 

If that still isn't what you need, please give us an example of what you expect. You can screenshot somethig from Excel to mock it up if you want, just explain the logic.

 

If this is what you want, then Power Query is the best place to do this. Power Query excels in this kind of transformation that is done for each record vs trying to access an entire table at a time per record.

 



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting
edhans
Super User
Super User

You have to reference the entire table, then specify the column, and the table is the previous step, which needs to be filtered.
The key formula is:

 

if [Units] = "Hours" then 
     List.Sum(
         Table.SelectRows(
             #"Changed Type",
             each [Units] = "Hours"
         )[Value]
     ) 
else null

 

To see a full example of this use this M code below by pasting it into a blank query.

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8sgvLSpW0lEyVIrVQfCMUHjGKDwTMC87PAOuC86OBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Units = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Value", type number}}),
    #"Add Total" = 
        Table.AddColumn(
            #"Changed Type",
            "Total",
            each
                if [Units] = "Hours" then 
                    List.Sum(
                        Table.SelectRows(
                            #"Changed Type",
                            each
                            [Units] = "Hours"
                        )[Value]) 
                else null
                        
        )
in
    #"Add Total"

 

 

This will return the following new column called Total:

edhans_0-1597164422159.png

 

Note that this will not perform well on large data sets. Your better bet is to return the data to DAX and use a measure to return that information. You can use a calculated column, but those have issues of their own.

But if you need it in Power Query, and your dataset isn't too large, this will work fine.

 

In general, try to avoid calculated columns. There are times to use them, but it is rare. Getting data out of the source system, creating columns in Power Query, or DAX Measures are usually preferred to calculated columns. See these references:
Calculated Columns vs Measures in DAX
Calculated Columns and Measures in DAX
Storage differences between calculated columns and calculated tables

 

1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
5) See this article if you need help using this M code in your model.

 



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting
mahoneypat
Employee
Employee

Why not do this in a DAX calculated column?  It would be easier.  However, your expression is trying to use the value in the current row of the [Value] column, which is why it can't convert it to a list.  To do what you are looking for, you will need something like this to filter the table from the previous step to just the rows for "Hours" and reference the [Value] column to get the list of values.

 

= List.Sum(Table.SelectRows(#"Previous Step", each [Units]="Hours")[Value])

 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

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.

Top Solution Authors
Top Kudoed Authors