0

I have a particular case to unpivot 8 different columns into 2 columns. I have a table in excel School with the four subjects with scores and %age.

enter image description here

I used unpivot-selected columns on Math, Bio, Social and Absent (count) to convert the 12 columns into 11 and 16 rows. enter image description here

enter image description here

But how to get the unpivot the remaining 4 columns of %age to get the columns converted from 11 to 7 columns and 16 rows?

enter image description here

Please help how to unpivot.

Thanks, NewB

2
  • Add a custom column pulling the percentage from the approp;riate percent column depending on the value in the Subject column. Commented Nov 9, 2022 at 13:01
  • I am not good in power query so not sure how to pull appropriate %colum depending on the value in the Subject column :) Commented Nov 9, 2022 at 13:06

1 Answer 1

1

I would just add a custom column.

This is M-Code accessed from Home => Advanced Editor

Examine the Applied Steps and the code comments to better understand what it is doing.

let

//change next line to reflect your actual data source (Table name)
    Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],

//set data types
    #"Changed Type" = Table.TransformColumnTypes(Source,{
        {"School", type text}, {"Class", Int64.Type}, {"Group", type text}, {"EOM", type date}, 
        {"Math", Int64.Type}, {"Bio", Int64.Type}, {"Social", Int64.Type}, {"Absent", Int64.Type}, 
        {"Math %", Int64.Type}, {"Bio%", Int64.Type}, {"Social%", Int64.Type}, {"Absent%", Int64.Type}}),

//Unpivot the Scores columns (Math, Bio, Social, Absent)
//Note the resulting two columns are named Subject and Score
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", 
        {"School", "Class", "Group", "EOM", "Math %", "Bio%", "Social%", "Absent%"}, 
        "Subject", "Score"),

//Add an Index column so the Custom Column can refer to the correct row
    #"Added Index" = Table.AddIndexColumn(#"Unpivoted Columns", "Index", 0, 1, Int64.Type),

//Add Custom column to return the appropriate percentage
// This relies on the name similarity between Subject and Percentage: eg Math => Math %
// but you could also do a bunch of If Then statements
    #"Added Custom" = Table.AddColumn(#"Added Index", "%age", each 
        Table.Column(#"Added Index", List.Select(Table.ColumnNames(#"Unpivoted Columns"), 
            (li)=> Text.Contains(li,[Subject])){0}){[Index]}, Int64.Type),

//Remove the unwanted columns
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Math %", "Bio%", "Social%", "Absent%", "Index"})
in
    #"Removed Columns"

enter image description here

Instead of the above, you could just, at the end of your existing code, select to Add Conditional Column. The dialog box would look like:
enter image description here

and the M-Code:

let

//change next line to reflect your actual data source (Table name)
    Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],

//set data types
    #"Changed Type" = Table.TransformColumnTypes(Source,{
        {"School", type text}, {"Class", Int64.Type}, {"Group", type text}, {"EOM", type date}, 
        {"Math", Int64.Type}, {"Bio", Int64.Type}, {"Social", Int64.Type}, {"Absent", Int64.Type}, 
        {"Math %", Int64.Type}, {"Bio%", Int64.Type}, {"Social%", Int64.Type}, {"Absent%", Int64.Type}}),

//Unpivot the Scores columns (Math, Bio, Social, Absent)
//Note the resulting two columns are named Subject and Score
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", 
        {"School", "Class", "Group", "EOM", "Math %", "Bio%", "Social%", "Absent%"}, 
        "Subject", "Score"),

//add Custom Column with multiple `if then else if` clauses
    #"Added Custom" = Table.AddColumn(#"Unpivoted Columns", "%age", each 
        if [Subject] = "Math" then [#"Math %"] 
        else if [Subject] = "Bio" then [#"Bio%"] 
        else if [Subject] = "Social" then [#"Social%"] 
        else if [Subject] = "Absent" then [#"Absent%"] 
        else null),

    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Math %", "Bio%", "Social%", "Absent%"})
in
    #"Removed Columns"

The advantage of the second method is that it is more obvious what is going on. However, if you add/delete/change courses and corresponding percentages, you will need to edit the code. With the first, you won't have to in that particular step. You will need to edit code in other steps, but those are more easily changed to be course-name independent.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.