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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
fmasselink
New Member

Grouping & summarizing using column name lists

Hi,

 

I have a table (very simplified) with table headers that have a / (and my other columns also have some with a double-colon)
(not sure how relevant that is with referencing the column/row)

Sellerrevenue/volumerevenue/amount
John310
Marc14
John23
Marc28

 

I want to perform a group action in PowerQuery, while I need to reduce the size of my resulting .pbix file.
I'm aware I will loose detail by doing the below, though that is as planned.

 

I have the following steps in my Query Editor:

A1 = Table.ColumnNames(source)                // the source table from above
A2 = { "revenue/volume", "revenue/amount" } // list with columns to summarize over
A3 = List.Difference(A1, A2) // get list with columns without columns from A2

 

In the next step I want to group and summarize over the columns in A2, but I'm uncertain of how to do that...

A4 = Table.Group (source, A3, ????????)       // group by A3, summarize by ....

 

Within the section of the question marks I want to do two things:

1. Add a counter "revenue/numberofsales" that counts the rows that are grouped together.

    {"revenue/numberofsales", each Table.RowCount(_), Int64.Type}

    This works (if this was the only thing in there... but I want to concatenate two lists...)

2. I want to Summarize over all the columns that are in A2 with a function rather than writing 

{"sumField1", each List.Sum([#"sumField1"]), type nullable number}, 
{"sumField2", each List.Sum([#"sumField2"]), type nullable number},
...,
{"sumField9", each List.Sum([#"sumField9"]), type nullable number}

like in

each columnname in A2, do
    { _, each List.Sum([_]), type nullable number }
done
return as list

Any suggestion on how to do that with function?

 

That should result in something like this then (I guess)

A4 = Table.Group (source, A3, List.Combine(
{
{"revenue/numberofsales", each Table.RowCount(_), Int64.Type}
},
{
{<something that returns the "each columnname in A2" as a list>}
}
)
)

providing this as the resulting table

Sellerrevenue/volumerevenue/amountrevenue/numberofsales
John5132
Marc3122

 

 

1 ACCEPTED SOLUTION
User101
Microsoft Employee
Microsoft Employee

Perhaps you are looking for something like this:

   

let

    Source = Sql.Databases("......"),

    AdventureWorksDW2019 = Source{[Name="AdventureWorksDW2019"]}[Data],

    dbo_DimProduct = AdventureWorksDW2019{[Schema="dbo",Item="DimProduct"]}[Data],

    #"Removed Columns" = Table.RemoveColumns(dbo_DimProduct,{"ProductKey", "ProductAlternateKey", "LargePhoto", "HebrewDescription", "ArabicDescription", "ChineseDescription", "TurkishDescription", "JapaneseDescription", "ThaiDescription", "GermanDescription", "FrenchDescription"}),

    allColumns = Table.ColumnNames(#"Removed Columns"),

    aggCols = {"ListPrice", "DealerPrice"},

    remaining = List.Difference(allColumns, aggCols),

    aggs = List.Transform(aggCols, (c) => {c, (t) => List.Sum(Table.Column(t, c)), type nullable number })

in

   Table.Group(#"Removed Columns", "EnglishProductName", aggs)

View solution in original post

6 REPLIES 6
User101
Microsoft Employee
Microsoft Employee

Perhaps you are looking for something like this:

   

let

    Source = Sql.Databases("......"),

    AdventureWorksDW2019 = Source{[Name="AdventureWorksDW2019"]}[Data],

    dbo_DimProduct = AdventureWorksDW2019{[Schema="dbo",Item="DimProduct"]}[Data],

    #"Removed Columns" = Table.RemoveColumns(dbo_DimProduct,{"ProductKey", "ProductAlternateKey", "LargePhoto", "HebrewDescription", "ArabicDescription", "ChineseDescription", "TurkishDescription", "JapaneseDescription", "ThaiDescription", "GermanDescription", "FrenchDescription"}),

    allColumns = Table.ColumnNames(#"Removed Columns"),

    aggCols = {"ListPrice", "DealerPrice"},

    remaining = List.Difference(allColumns, aggCols),

    aggs = List.Transform(aggCols, (c) => {c, (t) => List.Sum(Table.Column(t, c)), type nullable number })

in

   Table.Group(#"Removed Columns", "EnglishProductName", aggs)

@User101 

 

That is exactly what I was looking for 🙂

 

So my piece of code now looks like, nice and clean to add a new column and summarize in the same action:

 

A4 = Table.Group (source, A3, List.Combine(
{
{"revenue/numberofsales", each Table.RowCount(_), Int64.Type}
},
List.Transform(A2, (c) => {c, (t) => List.Sum(Table.Column(t, c)), type nullable number })
)

One additional question: how come the "(t)" and "t" works inside the List.Transform?  How does "t" know it is the table that is being grouped?

 

 

Im getting the error "2 arguments were sent to a function that expects 1" from this ?

something was wrong with placement of {}

We can think of .. List.Transform .. Just returns a function that accepts t and returns a column c.

Table.Group expects a function.

We can determine the parameter type using value.type

let
t1 = Table.Group(
Table.FromRecords(
{[CustomerID= 1, price = 5]}), "CustomerID", {"paramType",each Value.Type(_)}),
#"1" = t1{[CustomerID=1]}[paramType]
in
#"1"

Vera_33
Resident Rockstar
Resident Rockstar

Hi @fmasselink 

 

I guess you may have multiple columns when try to groupby, and multiple columns you want to do List.Sum, otherwise it is not worth the effort...I am not sure how to groupby, but here is another way, all with List.Sum but your result was showing one as Table.RowCount(_)

 

Vera_33_0-1641545503899.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8srPyFPSUTIGYkMDpVidaCXfxKJkEA+ITcACUCVGYGVIKkACFkqxsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Seller = _t, #"revenue/volume" = _t, #"revenue/amount" = _t]),
    A1 = Table.ColumnNames(Source),                // the source table from above
A2 = { "revenue/volume", "revenue/amount" } ,  // list with columns to summarize over
A3 = List.Difference(A1, A2),
    A4 = Table.Distinct( Table.SelectColumns(Source, A3)),
    A5 = Table.NestedJoin(A4, A3, Source, A3, "Custom", JoinKind.LeftOuter),
    Custom1 = List.Accumulate(A2, A5, (state, current) => Table.AddColumn(state, current, each List.Sum(List.Transform( Table.ToList( Table.SelectColumns(_[Custom],current)),Number.From)))),
    #"Removed Columns" = Table.RemoveColumns(Custom1,{"Custom"})
in
    #"Removed Columns"

 

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.