Forum Discussion

awsiya's avatar
awsiya
Helper I
2 years ago
Solved

How to use non first row as column header

Hi  Below is a table where I want to utilise the column labeled "toprow," specifically when its value is 0, as the column header. I attempted to transpose the table, but it didn't yield the desired ...
  • amustafa's avatar
    2 years ago

    let's say your sample survey data is in Excel table "Table1" then you can use Power Query to do some transformations steps as indicated in the M Code below. See the output.

     

     

    let
        Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
        #"Sorted Rows" = Table.Sort(Source,{{"Toprow", Order.Ascending}}),
        #"Promoted Headers" = Table.PromoteHeaders(#"Sorted Rows", [PromoteAllScalars=true]),
        #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"0", Int64.Type}, {"Was food Hygienic", type text}, {"Overall Feedback", type text}, {"27/02/2024", type any}, {"Reception treated well", type text}, {"AirCon cool enough?", type text}}),
        #"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"27/02/2024", "SurveyDate"}, {"0", "Toprow"}}),
        #"Changed Type with Locale" = Table.TransformColumnTypes(#"Renamed Columns", {{"SurveyDate", type date}}, "en-GB"),
        #"Reordered Columns" = Table.ReorderColumns(#"Changed Type with Locale",{"Toprow", "SurveyDate", "Was food Hygienic", "Overall Feedback", "Reception treated well", "AirCon cool enough?"}),
        #"Removed Columns" = Table.RemoveColumns(#"Reordered Columns",{"Toprow"})
    in
        #"Removed Columns"

     

     

    Output:

    SurveyDateWas food HygienicOverall FeedbackReception treated wellAirCon cool enough?

    8/5/2022some extendBadNoYes
    6/18/2022YesGoodNoNo
    5/9/2022some extendBadYesYes
    6/25/2023some extendBadYesYes
    5/18/2022YesGoodYesYes
    5/2/2024YesExcellentNoNo
    1/13/2024NoBadsome extentYes