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

Data Days is here! Join us now for 60+ days of learning, challenges, and connection. Learn more

Reply
AllanBerces
Post Prodigy
Post Prodigy

SUM

Hi can someone help me on my PQ. I have table and i need a sum and if condition

- IF the category is equal to Object then the value is equal to Needed Column

- IF the category is equal to Article then sum all the value in Needed column with same Column No. value

 

AllanBerces_0-1778833476720.png

OUTPUT

39 is the sum of needed column 25 and 14

AllanBerces_1-1778833519788.png

Column No.UnitNeededCategoryTOTAL QTY 
CCCCCC00001EA25Article39
CCCCCC00001EA14Article39
ObjectEA20Object20
ObjectEA1Object1
ObjectEA1Object1
ObjectEA1Object1
2 ACCEPTED SOLUTIONS
johnt75
Super User
Super User

You could use DAX to create a calculated column like

Total Quantity =
IF (
    'Table'[Category] = "Article",
    CALCULATE (
        SUM ( 'Table'[Needed] ),
        ALLEXCEPT ( 'Table', 'Table'[Column no.] )
    ),
    'Table'[Needed]
)

View solution in original post

MFelix
Super User
Super User

Hi @AllanBerces ,

 

Try the following approach:

  • Do a group by Category and Column N.º
  • Add a Sum of needed and a All Rows

MFelix_0-1778834744991.png

  • Expand the columns Unit and Need
  • On the Total Qty do a Replace values
    • Change just one value by whateve you want
  • Redo the code of the Replace step to:
= Table.ReplaceValue(#"Expanded Allvalues",each [Total Qty], each if [Category] = "Object" then [Needed] else [Total Qty] ,Replacer.ReplaceValue,{"Total Qty"})

 

Final result:

MFelix_1-1778834822699.png

 

 

Check full code below:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcgYDAyAwVNJRcnUEEkamQMKxqCQzOSdVKVYHqxpDEzQ1/klZqcklcCMMgARUCFPWkFqSCIcZIavA7nbsSmIB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Column No." = _t, Unit = _t, Needed = _t, Category = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column No.", type text}, {"Unit", type text}, {"Needed", Int64.Type}, {"Category", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Category", "Column No."}, {{"Total Qty", each List.Sum([Needed]), type nullable number}, {"Allvalues", each _, type table [#"Column No."=nullable text, Unit=nullable text, Needed=nullable number, Category=nullable text]}}),
    #"Expanded Allvalues" = Table.ExpandTableColumn(#"Grouped Rows", "Allvalues", {"Unit", "Needed"}, {"Unit", "Needed"}),
    #"Replaced Value" = Table.ReplaceValue(#"Expanded Allvalues",each [Total Qty], each if [Category] = "Object" then [Needed] else [Total Qty] ,Replacer.ReplaceValue,{"Total Qty"})
in
    #"Replaced Value"

Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português





View solution in original post

3 REPLIES 3
MFelix
Super User
Super User

Hi @AllanBerces ,

 

Try the following approach:

  • Do a group by Category and Column N.º
  • Add a Sum of needed and a All Rows

MFelix_0-1778834744991.png

  • Expand the columns Unit and Need
  • On the Total Qty do a Replace values
    • Change just one value by whateve you want
  • Redo the code of the Replace step to:
= Table.ReplaceValue(#"Expanded Allvalues",each [Total Qty], each if [Category] = "Object" then [Needed] else [Total Qty] ,Replacer.ReplaceValue,{"Total Qty"})

 

Final result:

MFelix_1-1778834822699.png

 

 

Check full code below:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcgYDAyAwVNJRcnUEEkamQMKxqCQzOSdVKVYHqxpDEzQ1/klZqcklcCMMgARUCFPWkFqSCIcZIavA7nbsSmIB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Column No." = _t, Unit = _t, Needed = _t, Category = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column No.", type text}, {"Unit", type text}, {"Needed", Int64.Type}, {"Category", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Category", "Column No."}, {{"Total Qty", each List.Sum([Needed]), type nullable number}, {"Allvalues", each _, type table [#"Column No."=nullable text, Unit=nullable text, Needed=nullable number, Category=nullable text]}}),
    #"Expanded Allvalues" = Table.ExpandTableColumn(#"Grouped Rows", "Allvalues", {"Unit", "Needed"}, {"Unit", "Needed"}),
    #"Replaced Value" = Table.ReplaceValue(#"Expanded Allvalues",each [Total Qty], each if [Category] = "Object" then [Needed] else [Total Qty] ,Replacer.ReplaceValue,{"Total Qty"})
in
    #"Replaced Value"

Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português





johnt75
Super User
Super User

You could use DAX to create a calculated column like

Total Quantity =
IF (
    'Table'[Category] = "Article",
    CALCULATE (
        SUM ( 'Table'[Needed] ),
        ALLEXCEPT ( 'Table', 'Table'[Column no.] )
    ),
    'Table'[Needed]
)

Hi @johnt75 @MFelix thank you very much for the reply work as i need

Helpful resources

Announcements
Fabric Data Days is here Carousel

Fabric Data Days 2026

Don't miss out on Data Days, June 15 through August 7. Learn Fabric, Power BI, SQL, AI and more.

May Power BI Update Carousel

Power BI Monthly Update - May 2026

Check out the May 2026 Power BI update to learn about new features.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.