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
salihhh
New Member

Applying a function while grouping (or on a group)

I have a situation where while grouping (or post the grouping is also fine) I want to select only a particular value from the last group based on a condition.

Suppose I have the below table. I write a function that prioritze which SubProduct to choose while grouping. In this case CC>BB>AA is the priority order, when they appear in the same group.

salihhh_0-1710476590099.png

The resulting table will look like this. Row count is not important, as its just for the sake of grouping. 

salihhh_2-1710477125531.png

 

Can we accomplish this using power query ? 

 

2 ACCEPTED SOLUTIONS
wdx223_Daniel
Super User
Super User

NewStep=Table.Group(PreviousStepName,"Product",{{"SubProduct",each List.Sort([SubProduct],each List.PositionOf({"CC","BB","AA"},_)){0}},{"Row Count",Table.RowCount}})

View solution in original post

dufoq3
Super User
Super User

Hi @salihhh, similar approach:

 

Result

dufoq3_0-1710621408134.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCtA1MjRW0lFydFSK1YFwTTC5Tk5wrhkm19lZKTYWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Product = _t, SubProduct = _t]),
    GroupedRows = Table.Group(Source, {"Product"}, {{"SubProduct", each Table.Last(_)[SubProduct], type text}, {"Row Count", each Table.RowCount(_), Int64.Type}})
in
    GroupedRows

Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

View solution in original post

2 REPLIES 2
dufoq3
Super User
Super User

Hi @salihhh, similar approach:

 

Result

dufoq3_0-1710621408134.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCtA1MjRW0lFydFSK1YFwTTC5Tk5wrhkm19lZKTYWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Product = _t, SubProduct = _t]),
    GroupedRows = Table.Group(Source, {"Product"}, {{"SubProduct", each Table.Last(_)[SubProduct], type text}, {"Row Count", each Table.RowCount(_), Int64.Type}})
in
    GroupedRows

Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

wdx223_Daniel
Super User
Super User

NewStep=Table.Group(PreviousStepName,"Product",{{"SubProduct",each List.Sort([SubProduct],each List.PositionOf({"CC","BB","AA"},_)){0}},{"Row Count",Table.RowCount}})

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.

Top Solution Authors
Top Kudoed Authors