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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
salihhh
Regular Visitor

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
Community Champion
Community Champion

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
Community Champion
Community Champion

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
Community Champion
Community Champion

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
Community Champion
Community Champion

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

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Fabric Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

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.