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
pescadicto
Helper I
Helper I

Filter a table by grouping rows: aggregation plus calculations

Hi everybody!

I am a powerbi's newbie, after read in these forums a lot of related-messages I can't solve this: I'm trying to filter a table by grouping rows according to a criterion (rows with same ID) while making a calculation on the values ​​of the rows grouped (sum of Feature2) and record it by overwriting the same column (or adding that value in a new column). I apologize for my english.
In a nutshell:

 

What I have is:

Capture1.PNG

What I want is (1):

Capture2.PNG

or (2):

Capture3.PNG

I have been looking for two kinds of solutions without success:

 

I) A filter in the Power Query Editor

 

I tried to apply a Grouped Rows step in the Power Query Editor:

 

= Table.Group(#"Filtered Rows", {"ID"}, {{"NewFeature", each List.Sum([Feature2]), type nullable number}})

 

but this removes the rest of the columns which I want to keep.

I performed some workarounds with Table.AddColumn, with no succesfull.

 

II) A new table obtained through a DAX expression

 

Table = GROUPBY(OriginalTable,OriginalTable[ID],"New Column",SUMX(CURRENTGROUP(),OriginalTable[Feature2]))

 

but it doesn't work: the rest of the columns and the applied filters are lost.

 

Any suggestions? Thanks in advance

2 ACCEPTED SOLUTIONS
Jimmy801
Community Champion
Community Champion

Hello @pescadicto 

 

you have to add one more function to the group function, that takes the grouped table. Afterwards you can expand it again. Here a example

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUUpMAxKGSrE60UpOQFZSmiGQNELiGwFJYzDfGchKBik3AXNdgKwUsHJTOD8ZrNxMKTYWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Feature1 = _t, Feature2 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", type text}, {"Feature1", type text}, {"Feature2", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"ID"}, {{"NewFeature", each List.Sum([Feature2]), type number}, {"AllRows", each _, type table [ID=text, Feature1=text, Feature2=number]}}),
    #"Expanded AllRows" = Table.ExpandTableColumn(#"Grouped Rows", "AllRows", {"Feature1", "Feature2"}, {"Feature1", "Feature2"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Expanded AllRows",{"ID", "Feature1", "Feature2", "NewFeature"})
in
    #"Reordered Columns"

 

Copy paste this code to the advanced editor in a new blank query to see how the solution works.

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

View solution in original post

Hello @pescadicto 

 

check out this dynamic solution. Note, that in the expand-function I had to delete the "ID", otherwise this column would be double and the program throw and error

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUUpMAxKGSrE60UpOQFZSmiGQNELiGwFJYzDfGchKBik3AXNdgKwUsHJTOD8ZrNxMKTYWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Feature1 = _t, Feature2 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", type text}, {"Feature1", type text}, {"Feature2", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"ID"}, {{"NewFeature", each List.Sum([Feature2]), type number}, {"AllRows", each _, type table [ID=text, Feature1=text, Feature2=number]}}),
    GetColumnNamesOfTableToBeExpanded = List.Distinct(List.Combine(List.Transform(#"Grouped Rows"[AllRows], each Table.ColumnNames(_)))),
    #"Expanded AllRows" = Table.ExpandTableColumn(#"Grouped Rows", "AllRows",List.Difference(GetColumnNamesOfTableToBeExpanded, {"ID"}), List.Difference(GetColumnNamesOfTableToBeExpanded, {"ID"})),
    #"Reordered Columns" = Table.ReorderColumns(#"Expanded AllRows",{"ID", "Feature1", "Feature2", "NewFeature"})
in
    #"Reordered Columns"

 

Copy paste this code to the advanced editor in a new blank query to see how the solution works.

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

 

View solution in original post

4 REPLIES 4
Jimmy801
Community Champion
Community Champion

Hello @pescadicto 

 

you have to add one more function to the group function, that takes the grouped table. Afterwards you can expand it again. Here a example

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUUpMAxKGSrE60UpOQFZSmiGQNELiGwFJYzDfGchKBik3AXNdgKwUsHJTOD8ZrNxMKTYWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Feature1 = _t, Feature2 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", type text}, {"Feature1", type text}, {"Feature2", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"ID"}, {{"NewFeature", each List.Sum([Feature2]), type number}, {"AllRows", each _, type table [ID=text, Feature1=text, Feature2=number]}}),
    #"Expanded AllRows" = Table.ExpandTableColumn(#"Grouped Rows", "AllRows", {"Feature1", "Feature2"}, {"Feature1", "Feature2"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Expanded AllRows",{"ID", "Feature1", "Feature2", "NewFeature"})
in
    #"Reordered Columns"

 

Copy paste this code to the advanced editor in a new blank query to see how the solution works.

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

Thank you! It worked. My real table has a lot of columns. It is posible to reformulate

#"Expanded AllRows" = Table.ExpandTableColumn(#"Grouped Rows", "AllRows", {"Feature1", "Feature2"}, {"Feature1", "Feature2"}),

in such a way that the column names do not have to be made explicit?

Hello @pescadicto 

 

check out this dynamic solution. Note, that in the expand-function I had to delete the "ID", otherwise this column would be double and the program throw and error

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUUpMAxKGSrE60UpOQFZSmiGQNELiGwFJYzDfGchKBik3AXNdgKwUsHJTOD8ZrNxMKTYWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Feature1 = _t, Feature2 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", type text}, {"Feature1", type text}, {"Feature2", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"ID"}, {{"NewFeature", each List.Sum([Feature2]), type number}, {"AllRows", each _, type table [ID=text, Feature1=text, Feature2=number]}}),
    GetColumnNamesOfTableToBeExpanded = List.Distinct(List.Combine(List.Transform(#"Grouped Rows"[AllRows], each Table.ColumnNames(_)))),
    #"Expanded AllRows" = Table.ExpandTableColumn(#"Grouped Rows", "AllRows",List.Difference(GetColumnNamesOfTableToBeExpanded, {"ID"}), List.Difference(GetColumnNamesOfTableToBeExpanded, {"ID"})),
    #"Reordered Columns" = Table.ReorderColumns(#"Expanded AllRows",{"ID", "Feature1", "Feature2", "NewFeature"})
in
    #"Reordered Columns"

 

Copy paste this code to the advanced editor in a new blank query to see how the solution works.

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

 

Great work! thanks @Jimmy801!

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