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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
ValeriaBreve
Post Partisan
Post Partisan

Table.Group by different criteria in the same table

Hi all

I need to group my table as per below example:

ValeriaBreve_1-1666612460140.png

 

So by Product, summing the amount, concatenating the Product Code and Type IF DIFFERENT ONLY, and then getting the MAX date.

 

Now I could only do a part of it:

 

#"Grouped Rows" = Table.Group(#"Changed Type", {"Product"}, {{"Amount", each List.Sum([Amount])},{"Max Update Date", each List.Max([Date])},{"Type", each Text.Combine([Type], ", "), type text}})

 

which leads me to:

ValeriaBreve_2-1666612552351.png

now I am still missing:

1) How can I tell PowerQuery to only concatenate if the text is different?

2) How can I work with concatenating the Product Type, which is a number? I tried to use Number.ToText but every time I get the error "We cannot convert a value of type List to type Number".  Can somebody please explain to me why this happens?

 

Thank you very much in advance for your help!

Kind regards

Valeria

 

 

 

1 ACCEPTED SOLUTION
ppm1
Solution Sage
Solution Sage

Try wrapping the column reference inside your Text.Combine with List.Distinct like this:

 

Text.Combine(List.Distinct([Type]), ", ")

 

Pat

Microsoft Employee

View solution in original post

8 REPLIES 8
Anonymous
Not applicable

Hi @ValeriaBreve ,

 

Could you please tell me that if your problems have been solved?

If so, please mark the helpful replies as solution. More people will benefit from them.

 

Best Regards,

Stephen Tao

of course and sorry for th delay- I was out of office. Here actually there are 2 posts that are a solution for me - I guess I can't mark them both as solutions (but tell me if I am wrong) - I will go with the first one answered....

AntrikshSharma
Super User
Super User

@ValeriaBreve You can use this:

let
    Source = Table.FromRows (
        Json.Document (
            Binary.Decompress (
                Binary.FromText (
                    "i45WCijKTylNLnFU0lEyNQIShiBsoG8EQkYgAafM7NRipVgduFInoKCZMZAwQlcaklGUmpiCrNYZKGppAiSM0dU65+SXZGTmpSOrBjnCzAJEQFWb4DYZ5ApTMyRX4FHrDDUSQynCEbEA",
                    BinaryEncoding.Base64
                ),
                Compression.Deflate
            )
        ),
        let
            _t = ( ( type nullable text ) meta [ Serialized.Text = true ] )
        in
            type table [ Product = _t, Amount = _t, #"Product Code" = _t, Date = _t, Type = _t ]
    ),
    ChangedType = Table.TransformColumnTypes (
        Source,
        {
            { "Product", type text },
            { "Amount", Int64.Type },
            { "Product Code", type text },
            { "Date", type date },
            { "Type", type text }
        }
    ),
    Group = Table.Group (
        ChangedType,
        { "Product" },
        {
            {
                "Count",
                ( Rows ) =>
                    let
                        TotalAmount = List.Sum ( Rows[Amount] ),
                        ProductCode = 
                            Text.Combine ( 
                                List.Distinct ( Rows[Product Code] ), 
                                ";" 
                            ),
                        MaxDate = List.Max ( Rows[Date] ),
                        Type = Text.Combine ( List.Distinct ( Rows[Type] ), ";" ),
                        Result = [
                            Amount       = TotalAmount,
                            Product Code = ProductCode,
                            Date         = MaxDate,
                            Type         = Type
                        ]
                    in
                        Result
            }
        }
    ),
    ExpandedCount = Table.ExpandRecordColumn (
        Group,
        "Count",
        { "Amount", "Product Code", "Date", "Type" },
        { "Amount", "Product Code", "Date", "Type" }
    ),
    ChangedType2 = Table.TransformColumnTypes (
        ExpandedCount,
        {
            { "Amount", Currency.Type },
            { "Product Code", type text },
            { "Date", type date },
            { "Type", type text }
        }
    )
in
    ChangedType2

 

ppm1
Solution Sage
Solution Sage

Try wrapping the column reference inside your Text.Combine with List.Distinct like this:

 

Text.Combine(List.Distinct([Type]), ", ")

 

Pat

Microsoft Employee

Hello, this does not work in my query, it still gives me the error: 

Expression.Error: We cannot convert the value 1 to type Text.
Details:
Value=1
Type=[Type]

It works great! Very efficient!!!! Thank you 🙂 I will wait to mark the post as solved because I am still missing question 2...

Try this for question 2:

Text.Combine(List.Transform(List.Distinct([Product Code]), Text.From), ",")

works very nicely - thank you!

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

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.