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
Gavroche97
Frequent Visitor

Create a column that gives conditionnal sum

Hi,

 

I would like to create a column that would give the sum of a column for each occurences of another column.

Exemple:

Cat   Value   SumColumn

A       3              4      

A       1              4

B       4              9

B       5              9

C       7             16

C       8             16

C       1             16

 

That would be simple to reproduce in any programming langage but I can't get the way to do it in M or DAX. Can anyone help me ?

Thank you,

Victor

1 ACCEPTED SOLUTION
Jihwan_Kim
Super User
Super User

Hi,

Please check the below picture and the attached pbix file.

It is for creating a new column by writing DAX.

 

Jihwan_Kim_0-1667639338976.png

 

Sum Column =
SUMX (
    FILTER ( Data, Data[Category] = EARLIER ( Data[Category] ) ),
    Data[Value]
)

 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


View solution in original post

2 REPLIES 2
m_alireza
Solution Specialist
Solution Specialist

Hi @Gavroche97 ,

You can achieve this by using the group by function in Power Query. 

Copy the below query in your advanced editor and amend as needed: 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTJWitWBsAzBLCcgywTOMgWznIEsczjLAs4C6ogFAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Cat = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Cat", type text}, {"Value", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Cat"}, {{"SumColumn", each List.Sum([Value]), type nullable number}, {"AllRows", each _, type table [Cat=nullable text, Value=nullable number]}}),
    #"Expanded AllRows" = Table.ExpandTableColumn(#"Grouped Rows", "AllRows", {"Cat", "Value"}, {"Cat.1", "Value"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded AllRows",{"Cat.1"})
in
    #"Removed Columns"


Sample output: 
sumcol.png

Jihwan_Kim
Super User
Super User

Hi,

Please check the below picture and the attached pbix file.

It is for creating a new column by writing DAX.

 

Jihwan_Kim_0-1667639338976.png

 

Sum Column =
SUMX (
    FILTER ( Data, Data[Category] = EARLIER ( Data[Category] ) ),
    Data[Value]
)

 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


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.