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
Royle79
New Member

Transpose Column A as Column header and populate rows with Column B distinct

Hi All,

 

I have 2 columns of data, column A is a list of People and column B are Regions where said person operates. So where "Name One" is the Person, Region 1, Region 2, etc are the places of operation.

 

PersonPlace
Name 1Place 1
Name 1Place 2
Name 1Place 3
Name 2Place 3
Name 2Place 4
Name 3Place 5
Name 4Place 1
Name 5Place 4

 

When I transpose in PQE i am getting columns for each occurance of the Person ("Name One", "Name One_1", etc) for each of my places in B.

 

I am looking for a Power Query Solution as I have to create a number of dependant lists along the same condition. My final Output should look something like;

 

Name 1Name 2Name 3Name 4Name 5
Place 1Place 1Place 1 Place 3Place 1
Place 2 Place 2Place 4Place 2
Place 3  Place 5Place 3
    Place 4
    Place 5

 

This is as close to the solution as I can get.

 

Royle79_0-1734348999271.png

 

Any help would be appreciate.

 

Thanks.

 

 

 

 

Can this be acheived in PQE?

3 ACCEPTED SOLUTIONS
ZhangKun
Resolver V
Resolver V

I'm not sure if your source data is incorrect or I don't understand the calculation logic. My final query is as follows:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8kvMTVUwVNJRCshJTAaxYnUwBI2wCRojBI3wC5ogBI3hgqYIQRNstpsia48FAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Person = _t, Place = _t]),
    GroupByPerson = Table.Group(Source, {"Person"}, {{"G", each [Place], Int64.Type}}),
    result = Table.FromRows(List.Zip(List.Transform(GroupByPerson[G], List.Distinct)), GroupByPerson[Person])
in
    result

If you "pivot" the Person column, you'll need to do something similar, where List.Zip is used to align the data and automatically fill in nulls.

View solution in original post

Hi @Royle79 ,

 

May I ask if you have tried ZhangKun's code? You can create a new blank query and paste the code into the advanced editor.
Provide another approach.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("lZPBboMwDIZfxcqZSgkpjB6XrWiHbkVD26XjEI3QZWKJFODQt5+pxNqyrYSb//j/HMeG3Y5kyjXWACMByWr5ro6R6HRdarOHmwsBj1m+IUUwRTEKCxAZbHWNx2mnakil+/Ih+ws30pTw1lEaxjBkGkidNW3veZVm30lXwrOV5ayasvzErGnhPOdRYDVS00gYonoxum3gbnjK/RwOxCz32sPN6Uh5IGxonsd4/mRd+6GcgTUO01YgTnmBBo964Uj9izzgkqUDHvUxj/B7yhGpFeStdergDfo0lYzUGcL/RuLkZ723AYhg3pLj1bA2Nst9ZVy/3Q3woafl5LQSehFPX5MclTw0wHA1Po2dCI5E5DFkRvsfd1tVGjOcFMU3", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Person = _t, Place = _t, #"Place 2" = _t, #"Place 3" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Person", type text}, {"Place", type text}, {"Place 2", type text}, {"Place 3", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Person"}, {{"Place", each List.Distinct(_[Place])}}),
    Custom1 = Table.FromColumns(#"Grouped Rows"[Place],#"Grouped Rows"[Person])
in
    Custom1

With the given example data, it returns the result:

vcgaomsft_0-1734400353441.png

Best Regards,
Gao

Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

How to get your questions answered quickly --  How to provide sample data in the Power BI Forum

View solution in original post

Krishana_123
Frequent Visitor

This is what you want if I understand correct. Below is the solution for that please accept it as solution if that helps 

Krishana_123_0-1735122313267.png

let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],

#"Changed Type" = Table.TransformColumnTypes(Source,{{"Person ", type text}, {"Region", type text}}),

#"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"Person ", "Name"}}),

IndexedTable = Table.AddIndexColumn(#"Renamed Columns", "Index", 1, 1, Int64.Type),

PivotedTable = Table.Pivot(IndexedTable, List.Distinct(IndexedTable[Name]), "Name", "Region"),
Custom1 = Table.FromColumns(
List.Transform(
Table.ToColumns(PivotedTable),
(col) => List.Sort(
col,
(a, b) => if a = null then 1 else if b = null then -1 else Comparer.Ordinal(a, b)
)
),
Table.ColumnNames(PivotedTable)
)
in
Custom1

View solution in original post

9 REPLIES 9
Krishana_123
Frequent Visitor

This is what you want if I understand correct. Below is the solution for that please accept it as solution if that helps 

Krishana_123_0-1735122313267.png

let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],

#"Changed Type" = Table.TransformColumnTypes(Source,{{"Person ", type text}, {"Region", type text}}),

#"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"Person ", "Name"}}),

IndexedTable = Table.AddIndexColumn(#"Renamed Columns", "Index", 1, 1, Int64.Type),

PivotedTable = Table.Pivot(IndexedTable, List.Distinct(IndexedTable[Name]), "Name", "Region"),
Custom1 = Table.FromColumns(
List.Transform(
Table.ToColumns(PivotedTable),
(col) => List.Sort(
col,
(a, b) => if a = null then 1 else if b = null then -1 else Comparer.Ordinal(a, b)
)
),
Table.ColumnNames(PivotedTable)
)
in
Custom1

lbendlin
Super User
Super User

Your expected output doesn't seem to map to your sample data.  Please clarify the logic you are applying.

Hi,

 

Thanks for the message. Apologies for confusing matters. The output image details the specifics, so what you see as the headers are the names referred to as Name 1, Name 2 etc.

The final aim of this exercise is to use the process to create heirachy lists, the lists grow, as the does the header count. Eventually I will require approx 60 lists, all dependant on the option chosen from another list.

 

Person 1 = 4 places

Place 1 = 7 Subs

Sub 1 = 25 Subs2

 

etc.

 

I thought if I could pivot 2 columns at a time, using column 1 create the headers and column 2 as the values to fill those new columns.

Please provide sample data that fully covers your issue.
Please show the expected outcome based on the sample data you provided.

Here is the data;

 

PersonPlacePlace 2Place 3
Person 1Place 1Building 7Building 7 MPSL
Person 1Place 1Building 10 - BP OilFuel Farm
Person 1Place 1Building 17Land & Buildings Fronting Vanguard Road
Person 1Place 1Building 17Land adjacent Building 17
Person 1Place 1Building 19Building 19
Person 1Place 1Building 22Units C & D
Person 1Place 1Building 22Unit B
Person 1Place 1Building 22Unit E
Person 1Place 1Building 30Building 30
Person 1Place 1Building 31 & 36Northern End of B31 & B36
Person 1Place 1Building 32Building 32
Person 1Place 1Hangar 35H35 - Single Storey
Person 1Place 1Hangar 35H35
Person 1Place 1Building 38Building 38
Person 3Place 1Building 68Units A, B, C & D
Person 1Place 1Building 69Unit 1
Person 1Place 1Building 69Unit 2
Person 1Place 1Building 69Units 3 & 4
Person 1Place 1Hangar 80Hangar 80
Person 1Place 1Building 81Bays 1 - 2
Person 1Place 1Building 81Bays 3 - 5
Person 3Place 1Building 107Office 3

 

The desired outcome;

 

Person 1Person 2Person 3
Place 1Place 1Place 1
Place 2 Place 3
Place 3  

 

I want to create columns based on "Person" and populate it with matching data from "Place". This is to essentially create a set of dynaminc and dependant lists.

 

Thanks.

Hi @Royle79 ,

 

May I ask if you have tried ZhangKun's code? You can create a new blank query and paste the code into the advanced editor.
Provide another approach.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("lZPBboMwDIZfxcqZSgkpjB6XrWiHbkVD26XjEI3QZWKJFODQt5+pxNqyrYSb//j/HMeG3Y5kyjXWACMByWr5ro6R6HRdarOHmwsBj1m+IUUwRTEKCxAZbHWNx2mnakil+/Ih+ws30pTw1lEaxjBkGkidNW3veZVm30lXwrOV5ayasvzErGnhPOdRYDVS00gYonoxum3gbnjK/RwOxCz32sPN6Uh5IGxonsd4/mRd+6GcgTUO01YgTnmBBo964Uj9izzgkqUDHvUxj/B7yhGpFeStdergDfo0lYzUGcL/RuLkZ723AYhg3pLj1bA2Nst9ZVy/3Q3woafl5LQSehFPX5MclTw0wHA1Po2dCI5E5DFkRvsfd1tVGjOcFMU3", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Person = _t, Place = _t, #"Place 2" = _t, #"Place 3" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Person", type text}, {"Place", type text}, {"Place 2", type text}, {"Place 3", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Person"}, {{"Place", each List.Distinct(_[Place])}}),
    Custom1 = Table.FromColumns(#"Grouped Rows"[Place],#"Grouped Rows"[Person])
in
    Custom1

With the given example data, it returns the result:

vcgaomsft_0-1734400353441.png

Best Regards,
Gao

Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

How to get your questions answered quickly --  How to provide sample data in the Power BI Forum

nowhere in your sample data does it say "Person2" ?

ZhangKun
Resolver V
Resolver V

I'm not sure if your source data is incorrect or I don't understand the calculation logic. My final query is as follows:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8kvMTVUwVNJRCshJTAaxYnUwBI2wCRojBI3wC5ogBI3hgqYIQRNstpsia48FAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Person = _t, Place = _t]),
    GroupByPerson = Table.Group(Source, {"Person"}, {{"G", each [Place], Int64.Type}}),
    result = Table.FromRows(List.Zip(List.Transform(GroupByPerson[G], List.Distinct)), GroupByPerson[Person])
in
    result

If you "pivot" the Person column, you'll need to do something similar, where List.Zip is used to align the data and automatically fill in nulls.

Many thanks for this. Admittidly I have not ran this code as there are elements I don't understand, purely due to a lack of knowledge on JSON code. I found a simpler solution using excel functions UNIQUE & FILTER for what I needed (dynamic dropdown lists), but it is a specific fix for the project I am working on rather than a full solution to the issue I had. When I have some time to learn, test and develop I will certainly try this code. I really appreciate your input and effort in helping me find the solution.

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!

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.

Top Solution Authors
Top Kudoed Authors