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

Learn from the best! Meet the four finalists headed to the FINALS of the Power BI Dataviz World Championships! Register now

Reply
milack
New Member

Data Transformations

Hello,

 

First post here... I have two spreadsheets with data laid out in different ways. I need to transform the layout of the "2024 - General - KS House (Johnson)" spreadsheet into the layout of the "2024 - General - KS House (Others)" spreadsheet.

 

The "Race" and "Candidates" columns in the "2024 - General - KS House (Johnson)" spreadsheet need to get Transposed into their respective columns and linked to the "Precinct Name" column. I hope this makes sense and thanks in advance if you can help!

Milack

 

2024 - General - KS House (Others)

2024 - General - KS House (Others).jpg

 

2024 - General - KS House (Johnson)

2024 - General - KS House (Johnson).jpg

  

1 ACCEPTED SOLUTION
Natarajan_M
Skilled Sharer
Skilled Sharer

Hi @milack , I was able to recreate your scenario . Ideally you need to use the Unpivot to get the data fromat you want (Column -> rows )

Sample data : 
each column holds the values  :

Natarajan_M_0-1774043125754.png


Transformed data : 
each row holds the values : 

Natarajan_M_1-1774043177455.png


let
    
    Source = Excel.Workbook(
        File.Contents("abcd.xlsx"),
        null, true
    ),

    
    JohnsonSheet = Source{[Item="Source (Johnson - Wide)", Kind="Sheet"]}[Data],

    
    PromotedHeaders = Table.PromoteHeaders(JohnsonSheet, [PromoteAllScalars=true]),

    
    TypedCols = Table.TransformColumnTypes(
        PromotedHeaders,
        {{"County", type text}, {"Precinct", type text}}
    ),

    // Identify ID columns vs value columns
    IdColumns    = {"County", "Precinct"},
    ValueColumns = List.Difference(Table.ColumnNames(TypedCols), IdColumns),

    
    Unpivoted = Table.UnpivotOtherColumns(TypedCols, IdColumns, "Race_Candidate", "Votes"),

    
    FilteredZeros = Table.SelectRows(
        Unpivoted,
        each [Votes] <> null and [Votes] <> 0
    ),

    
    SplitCol = Table.SplitColumn(
        FilteredZeros,
        "Race_Candidate",
        Splitter.SplitTextByEachDelimiter({" - "}, QuoteStyle.Csv, false),
        {"Race", "Candidate"}
    ),

    
    Reordered = Table.ReorderColumns(
        SplitCol,
        {"County", "Precinct", "Race", "Candidate", "Votes"}
    ),

    
    Result = Table.TransformColumnTypes(
        Reordered,
        {{"Votes", Int64.Type}, {"Race", type text}, {"Candidate", type text}}
    )

in
    Result


unpivot.pbix
Thanks 
If this response was helpful in any way, I’d gladly accept a kudo.
Please mark it as the correct solution. It helps other community members find their way faster

 

View solution in original post

3 REPLIES 3
Ashish_Mathur
Super User
Super User

Hi,

Share data in a format that can be pasted in an MS Excel file.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Natarajan_M
Skilled Sharer
Skilled Sharer

Hi @milack , I was able to recreate your scenario . Ideally you need to use the Unpivot to get the data fromat you want (Column -> rows )

Sample data : 
each column holds the values  :

Natarajan_M_0-1774043125754.png


Transformed data : 
each row holds the values : 

Natarajan_M_1-1774043177455.png


let
    
    Source = Excel.Workbook(
        File.Contents("abcd.xlsx"),
        null, true
    ),

    
    JohnsonSheet = Source{[Item="Source (Johnson - Wide)", Kind="Sheet"]}[Data],

    
    PromotedHeaders = Table.PromoteHeaders(JohnsonSheet, [PromoteAllScalars=true]),

    
    TypedCols = Table.TransformColumnTypes(
        PromotedHeaders,
        {{"County", type text}, {"Precinct", type text}}
    ),

    // Identify ID columns vs value columns
    IdColumns    = {"County", "Precinct"},
    ValueColumns = List.Difference(Table.ColumnNames(TypedCols), IdColumns),

    
    Unpivoted = Table.UnpivotOtherColumns(TypedCols, IdColumns, "Race_Candidate", "Votes"),

    
    FilteredZeros = Table.SelectRows(
        Unpivoted,
        each [Votes] <> null and [Votes] <> 0
    ),

    
    SplitCol = Table.SplitColumn(
        FilteredZeros,
        "Race_Candidate",
        Splitter.SplitTextByEachDelimiter({" - "}, QuoteStyle.Csv, false),
        {"Race", "Candidate"}
    ),

    
    Reordered = Table.ReorderColumns(
        SplitCol,
        {"County", "Precinct", "Race", "Candidate", "Votes"}
    ),

    
    Result = Table.TransformColumnTypes(
        Reordered,
        {{"Votes", Int64.Type}, {"Race", type text}, {"Candidate", type text}}
    )

in
    Result


unpivot.pbix
Thanks 
If this response was helpful in any way, I’d gladly accept a kudo.
Please mark it as the correct solution. It helps other community members find their way faster

 

Huzzah!!! It worked. Thank you very much @Natarajan_M!

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.