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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
jlauyfc1
Regular Visitor

Table.Pivot - without aggreagation but to list values

I am new to power query, how can I pivot a table without aggreagation but showing a list of (distinct) value? For example:

 

Table:

A | B

-----

a   X

b   X

 Y

 Y

e   Y

 

Expected result:

X | Y

-----

a   c

b  d

    e

 

Code:

What i am thinking about is:

 

= Table.Pivot(#"Source", List.Distinct(#"Source"[#"B"]), "B", "A", each .....)

 

 

Thanks in advance!!

1 ACCEPTED SOLUTION
Omid_Motamedise
Super User
Super User

use this formula

let
    Query1 = Table.FromColumns({{"a".."e"},{"X","X","Y","Y","Y"}},{"A","B"}),
    Pivot = Table.Pivot(Query1, List.Distinct(Query1[B]), "B", "A", each _),
    Custom1 = Table.FromRows(List.Zip({Pivot[X]{0},Pivot[Y]{0}}),Table.ColumnNames(Pivot))
in
    Custom1

 

it result in 

Omid_Motamedise_0-1725759196284.png

 

If my answer helped solve your issue, please consider marking it as the accepted solution. It helps others in the community find answers faster—and keeps the community growing stronger!
You can also check out my YouTube channel for tutorials, tips, and real-world solutions in Power Query with the following link
https://youtube.com/@omidbi?si=96Bo-ZsSwOx0Z36h

View solution in original post

4 REPLIES 4
Omid_Motamedise
Super User
Super User

use this formula

let
    Query1 = Table.FromColumns({{"a".."e"},{"X","X","Y","Y","Y"}},{"A","B"}),
    Pivot = Table.Pivot(Query1, List.Distinct(Query1[B]), "B", "A", each _),
    Custom1 = Table.FromRows(List.Zip({Pivot[X]{0},Pivot[Y]{0}}),Table.ColumnNames(Pivot))
in
    Custom1

 

it result in 

Omid_Motamedise_0-1725759196284.png

 

If my answer helped solve your issue, please consider marking it as the accepted solution. It helps others in the community find answers faster—and keeps the community growing stronger!
You can also check out my YouTube channel for tutorials, tips, and real-world solutions in Power Query with the following link
https://youtube.com/@omidbi?si=96Bo-ZsSwOx0Z36h

This is nicer with shorter coding. I amended your last line of code to make it work on any number of columns:

= Table.FromRows(List.Zip(Record.ToList(Table.First(Pivot))), Table.ColumnNames(Pivot))

 

Yes, it is dynamic and can work on any number, if you need more help or explanation just let me know

If my answer helped solve your issue, please consider marking it as the accepted solution. It helps others in the community find answers faster—and keeps the community growing stronger!
You can also check out my YouTube channel for tutorials, tips, and real-world solutions in Power Query with the following link
https://youtube.com/@omidbi?si=96Bo-ZsSwOx0Z36h
tackytechtom
Super User
Super User

Hi @jlauyfc1 ,

 

How about this:

tackytechtom_0-1725731260547.png

 

Here the code in Power Query M that you can paste into the advanced editor (if you do not know, how to exactly do this, please check out this quick walkthrough)

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSlTSUYpQitWJVkqCs5KBrEgwKwXOSoWwYgE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [A = _t, B = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"A", type text}, {"B", type text}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"A", Order.Ascending}}),
    #"Grouped Rows" = Table.Group(#"Sorted Rows", {"B"}, {{"Grouping", each _, type table [A=nullable text, B=nullable text]}}),
    #"Added Index" = Table.AddColumn(#"Grouped Rows", "Index", each Table.AddIndexColumn ( [Grouping], "Index", 1 )),
    #"Removed Columns 1" = Table.RemoveColumns(#"Added Index",{"B", "Grouping"}),
    #"Expanded Index" = Table.ExpandTableColumn(#"Removed Columns 1", "Index", {"A", "B", "Index"}, {"A", "B","Index"}),
    #"Pivoted Column" = Table.Pivot(#"Expanded Index", List.Distinct(#"Expanded Index"[B]), "B", "A"),
    #"Removed Columns 2" = Table.RemoveColumns(#"Pivoted Column",{"Index"})
in
    #"Removed Columns 2"

 

 

Let me know, if this solved your query.

 

/Tom
https://www.tackytech.blog/
https://www.instagram.com/tackytechtom/



Did I answer your question➡️ Please, mark my post as a solution ✔️

Also happily accepting Kudos 🙂

Feel free to connect with me on LinkedIn! linkedIn

#proudtobeasuperuser 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.