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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
Libin7963
Helper II
Helper II

Help with Table Pivot

My table looks as shown below on the left and I want it to look as the one on the right, please assist

Libin7963_0-1729953498398.png

 

1 ACCEPTED SOLUTION

Hi @Libin7963 ,

 

Certainly.  I’ve used your original example and pivoted the previously unpivoted table, as shown below:

DataNinja777_0-1729991516048.png

The following M code will transform the source table above to a pivoted table like below:

 

let
    // Step 1: Load the source data
    Source = Table.FromRows(
        {
            {1, "A"}, {1, "B"}, {1, "C"}, {1, "D"}, {1, "E"}, {1, "F"}, {1, "G"},
            {2, "H"}, {2, "I"}, {2, "J"}, {2, "K"}, {2, "L"}, {2, "M"}, {2, "N"},
            {3, "O"}, {3, "P"}, {3, "Q"}, {3, "R"}, {3, "S"}, {3, "T"}, {3, "U"},
            {3, "V"}, {3, "W"}
        }, 
        {"Unique Key", "Column1"}
    ),

    // Step 2: Group the data by 'Unique Key'
    GroupedData = Table.Group(
        Source, 
        {"Unique Key"}, 
        {{"AllData", each 
            Table.AddIndexColumn(_, "Rank", 1, 1, Int64.Type)
        }}
    ),

    // Step 3: Convert the Rank number to text with "Column" prefix
    AddPrefix = Table.TransformColumns(
        GroupedData, 
        {
            {"AllData", each 
                Table.TransformColumns(_, {{"Rank", each "Column" & Text.From(_), type text}})
            }
        }
    ),

    // Step 4: Expand the grouped data back to a flat table
    ExpandedTable = Table.ExpandTableColumn(
        AddPrefix, 
        "AllData", 
        { "Column1", "Rank"}
    ),

    // Step 5: Pivot the Rank column
    PivotedTable = Table.Pivot(
        ExpandedTable, 
        List.Distinct(ExpandedTable[Rank]), 
        "Rank", 
        "Column1"
    )
in
    PivotedTable

 

The resultant table will look like the one below:

DataNinja777_1-1729991685588.png

You can now create a many-to-one relationship using the Unique Key column.

 

Best regards,

 

 

 

 

View solution in original post

4 REPLIES 4
DataNinja777
Super User
Super User

Hi @Libin7963 ,

 

You can achieve your required output in multiple different ways, and one of them is as follows.

First, write a calculate column DAX like below:

Rank = "Column"&
RANKX(
    FILTER(
        Data, 
        Data[Unique Key] = EARLIER(Data[Unique Key])
    ), 
    Data[Index], 
    , 
    ASC, 
    DENSE
)

 

DataNinja777_0-1729955340240.png

Then write a ConcatenateX measure to put in the values section of the matrix table.

ConcatenateX Column1 = concatenatex('Data','Data'[Column1])

The resulting output is as shown below:

DataNinja777_1-1729955446158.png

 

Since there is no inherent row order in Power BI's data view, it is important to insert an index column using Power Query at the beginning.

 

I have attached an example pbix file for your reference.

 

Best regards,

 

Thanks for your reply, Sorry if I was not clear enough. Can it be done at power query level and not in visual. I want to build a many to one relationship to this table's unique key once it is made unique. Relationship is between these two Primary key columns as One to many.

 

Here is a better example scenario.

 

Table 1

Libin7963_0-1729989378081.png

 

 

Table 2

Libin7963_1-1729989404727.png

 

Report view

 

Libin7963_3-1729989520688.png

 

 

 

Hi @Libin7963 ,

 

Certainly.  I’ve used your original example and pivoted the previously unpivoted table, as shown below:

DataNinja777_0-1729991516048.png

The following M code will transform the source table above to a pivoted table like below:

 

let
    // Step 1: Load the source data
    Source = Table.FromRows(
        {
            {1, "A"}, {1, "B"}, {1, "C"}, {1, "D"}, {1, "E"}, {1, "F"}, {1, "G"},
            {2, "H"}, {2, "I"}, {2, "J"}, {2, "K"}, {2, "L"}, {2, "M"}, {2, "N"},
            {3, "O"}, {3, "P"}, {3, "Q"}, {3, "R"}, {3, "S"}, {3, "T"}, {3, "U"},
            {3, "V"}, {3, "W"}
        }, 
        {"Unique Key", "Column1"}
    ),

    // Step 2: Group the data by 'Unique Key'
    GroupedData = Table.Group(
        Source, 
        {"Unique Key"}, 
        {{"AllData", each 
            Table.AddIndexColumn(_, "Rank", 1, 1, Int64.Type)
        }}
    ),

    // Step 3: Convert the Rank number to text with "Column" prefix
    AddPrefix = Table.TransformColumns(
        GroupedData, 
        {
            {"AllData", each 
                Table.TransformColumns(_, {{"Rank", each "Column" & Text.From(_), type text}})
            }
        }
    ),

    // Step 4: Expand the grouped data back to a flat table
    ExpandedTable = Table.ExpandTableColumn(
        AddPrefix, 
        "AllData", 
        { "Column1", "Rank"}
    ),

    // Step 5: Pivot the Rank column
    PivotedTable = Table.Pivot(
        ExpandedTable, 
        List.Distinct(ExpandedTable[Rank]), 
        "Rank", 
        "Column1"
    )
in
    PivotedTable

 

The resultant table will look like the one below:

DataNinja777_1-1729991685588.png

You can now create a many-to-one relationship using the Unique Key column.

 

Best regards,

 

 

 

 

Thank you, that worked...

 

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

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.