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

The Power BI Data Visualization World Championships is back! It's time to submit your entry. Live now!

Reply
ashameen01
Regular Visitor

Transform Data Table in Power Query

I am new to PQ, I need help on the following:

 

I have a source data in the following format in excel:

LicenceYearCompanyCategory
LTE2022BNLGD
LTE2022ELELLLGL
LTE2022VECLLGL
LTE2023RNGLGL
LTE2023TOCELGL
LTE2023UTMLGL
LTE2023RNG2LGL
LTE2023SPLGPL
LTE2023ACELGL
LTE2024ANDARAGD
LTE2024OHURGPL
LTE2024RHFDGPL
LTC2022INDLGD
LTC2023FMGGPL
LTC2023NALGLGL
LTC2023SAPLGD
LTC2024ANDARAGD
AIHC2022COTTGPL
AIHC2023POCGPL
AIHC 2024ODAFGPL
LTO2022ASHGPL
LTO2022NEPGPL
LTO2022DFLGD
LTO2022NOTRGPL
LTO2023GRANVLLGL
LTO2024GAILGPL

And I need to transform it to the following form in Power Query:

 

LicenceYearGPLGDLGLList of Companies granted
LTE2022 12VECL, BNL, ELELL
LTC2022 1 INDL
AIHC20221  COTT
LTO202231 ASH, NEP, DFL,NOTR
LTE20231 5RNG, TOCE, UTM,RNG2,SPL,ACE
LTC2023111FMG,NALG,SAPL
AIHC20231  POC
LTO2023  1GRANVL
LTE202421 OHUR,RHFD,ANDARA
LTC2024 1 ANDARA
AIHC20241  ODAF
LTO20241  GAIL

 

 

Thank you for your kind help.

 

 

 

1 ACCEPTED SOLUTION
jennratten
Super User
Super User

Hello @ashameen01, you can achieve the desired result by grouping the rows by license and year, and then aggregating categories and companies by the groups.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fZJBDsIgEEWvYrp2YaoXGIHSJgiEoolpumhMY1zYGuPG2wsuBHRw02n6mD//T+m6QlxO43Qai2VxHIe7K2S+3obp6d+Gx3ie78+iX7pzlrlP5aosXdlK4Z6c/hImmPBMcPELD4ygbO2KkTyHrCIsx/Z290exzLFWvwNoBAE+bOORpGAASe6hqvcG0/TM1BVNGAlLaSRNlkmClWrHsS6PJIh0XxFrQWOKWAJo6sgKUdZGAwP0qlqRL7aIwlOoEqsqqEJb55BkOodolWSIm5Q1WJc3yQ3IQ3rHVHDJofn89v4F", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"(blank)" = _t, #"(blank).1" = _t, #"(blank).2" = _t, #"(blank).3" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"(blank)", type text}, {"(blank).1", type text}, {"(blank).2", type text}, {"(blank).3", type text}}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars=true]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"Licence", type text}, {"Year", Int64.Type}, {"Company", type text}, {"Category", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type1", {"Licence", "Year"}, {{"GPL", each List.Count ( List.Select ( _[Category], each _ = "GPL" ) ), type number }, {"GD", each List.Count ( List.Select ( _[Category], each _ = "GD" ) ), type number}, {"LGL", each List.Count ( List.Select ( _[Category], each _ = "LGL" ) ), type number}, {"Companies", each Text.Combine ( _[Company], ", " ), type nullable text}})
in
    #"Grouped Rows"

jennratten_0-1717280932075.png

 

View solution in original post

4 REPLIES 4
AlienSx
Super User
Super User

 

let
    Source = your_table,
    group = Table.Group(
        Source, {"Licence", "Year"}, 
        {{"x", (x) => Function.Invoke(
            Record.FromList,
            List.Reverse(
                Table.ToColumns(
                    Table.Group(x, "Category", {"count", Table.RowCount})
                )
            )
        )},
        {"List of companies", (x) => Text.Combine(List.Distinct(x[Company]), ", ")}}),
    xpand = Table.ExpandRecordColumn(group, "x", List.Distinct(Source[Category]))
in
    xpand

or slightly modified @jennratten 's code to avoid hard coded categories

let
    Source = your_table,
    lst = List.Transform(
        List.Distinct(Source[Category]), 
        (x) => {x, (y) => List.Count(List.Select(y[Category], (w) => w = x))}
    ),
    group = Table.Group(
        Source, {"Licence", "Year"}, 
        lst & {{"List of companies", (x) => Text.Combine(List.Distinct(x[Company]), ", ")}})
in
    group

 

jennratten
Super User
Super User

Hello @ashameen01, you can achieve the desired result by grouping the rows by license and year, and then aggregating categories and companies by the groups.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fZJBDsIgEEWvYrp2YaoXGIHSJgiEoolpumhMY1zYGuPG2wsuBHRw02n6mD//T+m6QlxO43Qai2VxHIe7K2S+3obp6d+Gx3ie78+iX7pzlrlP5aosXdlK4Z6c/hImmPBMcPELD4ygbO2KkTyHrCIsx/Z290exzLFWvwNoBAE+bOORpGAASe6hqvcG0/TM1BVNGAlLaSRNlkmClWrHsS6PJIh0XxFrQWOKWAJo6sgKUdZGAwP0qlqRL7aIwlOoEqsqqEJb55BkOodolWSIm5Q1WJc3yQ3IQ3rHVHDJofn89v4F", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"(blank)" = _t, #"(blank).1" = _t, #"(blank).2" = _t, #"(blank).3" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"(blank)", type text}, {"(blank).1", type text}, {"(blank).2", type text}, {"(blank).3", type text}}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars=true]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"Licence", type text}, {"Year", Int64.Type}, {"Company", type text}, {"Category", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type1", {"Licence", "Year"}, {{"GPL", each List.Count ( List.Select ( _[Category], each _ = "GPL" ) ), type number }, {"GD", each List.Count ( List.Select ( _[Category], each _ = "GD" ) ), type number}, {"LGL", each List.Count ( List.Select ( _[Category], each _ = "LGL" ) ), type number}, {"Companies", each Text.Combine ( _[Company], ", " ), type nullable text}})
in
    #"Grouped Rows"

jennratten_0-1717280932075.png

 

Many thanks, that worked, to get a wrap on the steps, so you grouped by "Licence" and "Year" without aggregation and added custom columns: " { {"GPL", each List.Count(List.Select(_[Category], each _ = "GPL")), type number}, {"GD", each List.Count(List.Select(_[Category], each _ = "GD")), type number}, {"LGL", each List.Count(List.Select(_[Category], each _ = "LGL")), type number}, {"Companies", each Text.Combine(_[Company], ", "), type nullable text} } )" to count categories and combined list of companies?

 

 

Yes, that's correct!  Please let me know if I can further assist with this.

 

If this helped you please consider clicking Accept as Solution.  

Helpful resources

Announcements
December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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 Solution Authors