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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
kulkarni21vinee
Frequent Visitor

Conditional grouping

Actual:

CountryCity
USA(M) New York
USAGreenville
USAFredericksburg
USA(M) Chicago
USABurlington
France(M) Paris
Japan(M) Tokyo
JapanNara
Japan(M) Osaka
JapanNikko
RussiaKolomna
Russia(M) Moscow

 

Expected:

CountryCity
USAGreenville, Fredericksburg, Burlington
USA(M) New York
USA(M) Chicago
France(M) Paris
JapanNara, Nikko
Japan(M) Tokyo
Japan(M) Osaka
RussiaKolomna
Russia(M) Moscow

 

Rules: PowerBI query to 

1. Group by Country with comma seperated cities

2. Ungroup when city is prefixed with (M) - Major

 

Is their something like below to ungroup in M query?

 

#"Grouped Rows" = Table.Group(
#"Expanded Column1",
{"Country"},
{not Text.Contains([City], "(M)")})

1 ACCEPTED SOLUTION
dufoq3
Super User
Super User

Hi @kulkarni21vinee, check this:

 

Output

dufoq3_0-1735667482764.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ZcxLCsIwEAbgq5SsFHoJFSooreJjISWLMQ51SMzIxFi8vbZWSHH7/Y+6Vsf9TOVqUk6zCtvsxGKVzn+8FET/JOcwwULwgkLGhnOUJgm6k8WVDDSc6DyKI9882PdYCHiDQ3sLQqHnFdzBD3pg++KRViDwV9sEsGOtyNrvcBdDIPjQmh3fPIyxW5ccDLdK6zc=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Country = _t, City = _t]),
    GroupedRows = Table.Group(Source, {"Country"}, {{"T", each 
        [ a = Table.SelectRows(_, (x)=> not Text.StartsWith(x[City], "(M)"))[City], //except Major
          b = Table.SelectRows(_, (x)=> Text.StartsWith(x[City], "(M)"))[City], //Major
          c = Table.SelectRows(Table.FromColumns( {[Country]} & { if List.IsEmpty(a)then b else {Text.Combine(a, ", ")} & b }, type table[Country=text, City=text]), (x)=> x[City] <> null)
        ][c], type table}}),
    CombinedT = Table.Combine(GroupedRows[T])
in
    CombinedT

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

4 REPLIES 4
V-yubandi-msft
Community Support
Community Support

Hi @kulkarni21vinee ,

Thanks for connecting with us on the Microsoft Fabric Community Forum. 

 

I believe the @dufoq3  response is accurate and should address your issue.

If you have any more questions or updates, please feel free to ask, and we will look into it.

If the Super User's answer meets your needs, kindly consider marking it as the Accepted solution.

 

Thank You.

wdx223_Daniel
Super User
Super User

wdx223_Daniel_0-1735793391613.png

 

ThxAlot
Super User
Super User

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ZcxLCsIwEAbgq5SsFHoJFSooreJjISWLMQ51SMzIxFi8vbZWSHH7/Y+6Vsf9TOVqUk6zCtvsxGKVzn+8FET/JOcwwULwgkLGhnOUJgm6k8WVDDSc6DyKI9882PdYCHiDQ3sLQqHnFdzBD3pg++KRViDwV9sEsGOtyNrvcBdDIPjQmh3fPIyxW5ccDLdK6zc=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Country = _t, City = _t]),
    Grouped = Table.Group(Source, "Country", {"outer", each Table.Group(Table.Sort(_, "City"), "City", {"inner", each Text.Combine([City], ", ")}, 0, (x,y) => Byte.From(Text.StartsWith(x, "(M)")))}),
    #"Expanded outer" = Table.ExpandTableColumn(Grouped, "outer", {"inner"}, {"inner"})
in
    #"Expanded outer"

ThxAlot_0-1735669429504.png



Expertise = List.Accumulate(


        {Days as from Today},


        {Skills and Knowledge},


        (Current, Everyday) => Current & Day.LearnAndPractise(Everyday)


)



dufoq3
Super User
Super User

Hi @kulkarni21vinee, check this:

 

Output

dufoq3_0-1735667482764.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ZcxLCsIwEAbgq5SsFHoJFSooreJjISWLMQ51SMzIxFi8vbZWSHH7/Y+6Vsf9TOVqUk6zCtvsxGKVzn+8FET/JOcwwULwgkLGhnOUJgm6k8WVDDSc6DyKI9882PdYCHiDQ3sLQqHnFdzBD3pg++KRViDwV9sEsGOtyNrvcBdDIPjQmh3fPIyxW5ccDLdK6zc=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Country = _t, City = _t]),
    GroupedRows = Table.Group(Source, {"Country"}, {{"T", each 
        [ a = Table.SelectRows(_, (x)=> not Text.StartsWith(x[City], "(M)"))[City], //except Major
          b = Table.SelectRows(_, (x)=> Text.StartsWith(x[City], "(M)"))[City], //Major
          c = Table.SelectRows(Table.FromColumns( {[Country]} & { if List.IsEmpty(a)then b else {Text.Combine(a, ", ")} & b }, type table[Country=text, City=text]), (x)=> x[City] <> null)
        ][c], type table}}),
    CombinedT = Table.Combine(GroupedRows[T])
in
    CombinedT

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

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Kudoed Authors