Forum Discussion

afaherty's avatar
afaherty
Icon for Helper V rankHelper V
10 months ago
Solved

Issue with pivoting via DAX

Hello,

This is piggy-backing off of a previous post I had made and I continue to struggle which is my fault because my real data has more columns of interest that I did not share (apologies for that).

 

I have this fake data here BUT it is important to note that my real data has many more students than just #1-4. (It also has many more columns but they are not of interest).

 

StudentIDLevelTestScoreCategory
1High SchoolMath88Above Average
1Middle SchoolEnglish90Above Average
1High SchoolScience83Average
1Middle SchoolScience89Above Average
1High SchoolSocial Studies97Above Average
1Middle SchoolSocial Studies88Above Average
2High SchoolEnglish95Above Average
2Middle SchoolEnglish90Above Average
2High SchoolSocial Studies68Below Average
2Middle SchoolSocial Studies78Average
3High SchoolMath85Average
3Middle SchoolMath72Average
3High SchoolEnglish93Above Average
3Middle SchoolEnglish90Above Average
3High SchoolScience86Above Average
3Middle SchoolScience95Above Average
4High SchoolEnglish93Above Average
4Middle SchoolEnglish89Above Average
4High SchoolScience81Average
4Middle SchoolScience87Above Average
4High SchoolSocial Studies74Average
4Middle SchoolSocial Studies70Average

 

I want each studentID to have only 1 row for high school, 1 row for middle school, and the subjects to be in columns as in the table below. I have pivoted every way I can, and I have tried the DAX solution in my original post suggested by the very kind & helpful v-dineshya but I am not having success.

 

Is there a way to achieve this?

 

StudentIDLevelMathCategoryEnglishCategoryScienceCategorySocial StudiesCategory
1High School88Above Averagenullnull83Average97Above Average
1Middle Schoolnullnull90Above Average89Above Average88Above Average
2High Schoolnullnull95Above Averagenullnull68Below Average
2Middle Schoolnullnull90Above Averagenullnull78Average
3High School85Above Average93Average86Above Averagenullnull
3Middle School72Average90Above Average95Above Averagenullnull
4High Schoolnullnull93Above Average81Average74Average
4Middle Schoolnullnull89Above Average87Above Average70Average

 

Thank you in advance!

 

  • afaherty This can be easily done with Power Query no need to use DAX for data transformation.

     

    let
        Source = RawData,
        Unpivot = Table.UnpivotOtherColumns ( Source, { "StudentID", "Level", "Test" }, "A", "V" ),
        MergeCols = Table.CombineColumns (
            Unpivot,
            { "Test", "A" },
            Combiner.CombineTextByDelimiter ( "_", QuoteStyle.None ),
            "Merged"
        ),
        Pivot = Table.Pivot ( MergeCols, List.Distinct ( MergeCols[Merged] ), "Merged", "V", ( x ) => x{0}? )
    in
        Pivot

     

     

9 Replies

  • hello afaherty 

     

    you surely can do with DAX with some workaround (although this much easier with PQ).

     

    you need to create measure for each category (math, math category, english, english category, and so on).

    Math =
    MAXX(
        FILTER(
            'Table',
            'Table'[StudentID]=SELECTEDVALUE('Table'[StudentID])&&
            'Table'[Level]=SELECTEDVALUE('Table'[Level])&&
            'Table'[Test]="Math"
        ),
        'Table'[Score]
    )
    Math Category =
    MAXX(
        FILTER(
            'Table',
            'Table'[StudentID]=SELECTEDVALUE('Table'[StudentID])&&
            'Table'[Level]=SELECTEDVALUE('Table'[Level])&&
            'Table'[Test]="Math"
        ),
        'Table'[Category]
    )
    Copy the above DAXs for all studies and its category with change in [Test'] column value.
     
    Hope this will help.
    Thank you.
    • afaherty's avatar
      afaherty
      Icon for Helper V rankHelper V

      Thank you, thank you. The issue is that this is fake data and my real data has many more subjects other than math, english, science, and social studies.

      • Irwan's avatar
        Irwan
        Icon for Super User rankSuper User

        hello afaherty 

         

        if you are planning using DAX, then you need to re-define each subject either in measure or calculated table.

        you can see divyed 's solution also define each subject.

         

        if you dont want to define one by one, then you need to do this in PQ like ThxAlot and AntrikshSharma 's solution which is much easier in PQ for data transformation.

         

        Hope this will help.

        Thank you.

  • Hello afaherty ,

     

    You can use below dax to create a calculated table. 

     

    Pivoted_Report =
    SUMMARIZECOLUMNS(
        'Test'[StudentID],
        'Test'[Level],

        // === Math Columns ===
        "Math",
        CALCULATE(
            MAXX('Test', 'Test'[Score]),
            FILTER('Test', 'Test'[Test] = "Math")
        ),
        "Math Category",
        CALCULATE(
            MAXX('Test', 'Test'[Category]),
            FILTER('Test', 'Test'[Test] = "Math")
        ),

        // === English Columns ===
        "English",
        CALCULATE(
            MAXX('Test', 'Test'[Score]),
            FILTER('Test', 'Test'[Test] = "English")
        ),
        "English Category",
        CALCULATE(
            MAXX('Test', 'Test'[Category]),
            FILTER('Test', 'Test'[Test] = "English")
        ),

        // === Science Columns ===
        "Science",
        CALCULATE(
            MAXX('Test', 'Test'[Score]),
            FILTER('Test', 'Test'[Test] = "Science")
        ),
        "Science Category",
        CALCULATE(
            MAXX('Test', 'Test'[Category]),
            FILTER('Test', 'Test'[Test] = "Science")
        ),

        // === Social Studies Columns ===
        "Social Studies",
        CALCULATE(
            MAXX('Test', 'Test'[Score]),
            FILTER('Test', 'Test'[Test] = "Social Studies")
        ),
        "Social Studies Category",
        CALCULATE(
            MAXX('Test', 'Test'[Category]),
            FILTER('Test', 'Test'[Test] = "Social Studies")
        )
    )
     

    I hope this solves your issue.

     

    Did I answer your query ? Mark this as solution if this helps, you can say special thank you by giving Kudos :).

     

    Cheers.

    • afaherty's avatar
      afaherty
      Icon for Helper V rankHelper V

      Thank you, thank you. The issue is that this is fake data and my real data has many more subjects other than math, english, science, and social studies.

      • divyed's avatar
        divyed
        Icon for Super User rankSuper User

        Hello afaherty ,

         

        I got your point, You want to have a generic query like dynamic pivoting solution. If this is the case Power Query would be the best approach . I am pasting couple of option you can opt for :

         

        1. Power Query : the true generic one 

         

           

        let
        Source = #"Your Data Source Table Name",

        // Unpivot the Score and Category columns to create a single 'Value' column
        #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(
        Source,
        {"StudentID", "Level", "Test"},
        "Metric Type",
        "Value"
        ),

        // Create dynamic column name: [Test] for score, [Test] Category for category
        #"Added Final Pivot Column Name" = Table.AddColumn(#"Unpivoted Other Columns", "Pivot Column Name", each
        if [Metric Type] = "Score" then
        [Test]
        else
        [Test] & " Category"
        ),

        // Dynamic Pivot step: This is the generic part!
        #"Pivoted Column" = Table.Pivot(
        #"Added Final Pivot Column Name",
        List.Distinct(#"Added Final Pivot Column Name"[Pivot Column Name]),
        "Pivot Column Name",
        "Value",
        List.Max
        )
        in
        #"Pivoted Column"

         

        2. DAX : You can use external tool to generate repeating string

         

        Pivoted_Report =
        SUMMARIZECOLUMNS(
        'Test'[StudentID],
        'Test'[Level],
        "Math",
        CALCULATE( MAXX('Test', 'Test'[Score]), 'Test'[Test] = "Math" ),
        "Math Category",
        CALCULATE( MAXX('Test', 'Test'[Category]), 'Test'[Test] = "Math" ),

        "English",
        CALCULATE( MAXX('Test', 'Test'[Score]), 'Test'[Test] = "English" ),
        "English Category",
        CALCULATE( MAXX('Test', 'Test'[Category]), 'Test'[Test] = "English" )

        // ... repeat for all other subjects
        )

         

        I hope this helps .

         

        Cheers.

  • v-sgandrathi's avatar
    v-sgandrathi
    Icon for Community Support rankCommunity Support

    Hi afaherty,

     

    I wanted to check if you had the opportunity to review the information provided by ThxAlot  divyed  Irwan. Please feel free to contact us if you have any further questions. 
     

    Thank you and continue using Microsoft Fabric Community Forum.

  • AntrikshSharma's avatar
    AntrikshSharma
    Icon for Community Champion rankCommunity Champion

    afaherty This can be easily done with Power Query no need to use DAX for data transformation.

     

    let
        Source = RawData,
        Unpivot = Table.UnpivotOtherColumns ( Source, { "StudentID", "Level", "Test" }, "A", "V" ),
        MergeCols = Table.CombineColumns (
            Unpivot,
            { "Test", "A" },
            Combiner.CombineTextByDelimiter ( "_", QuoteStyle.None ),
            "Merged"
        ),
        Pivot = Table.Pivot ( MergeCols, List.Distinct ( MergeCols[Merged] ), "Merged", "V", ( x ) => x{0}? )
    in
        Pivot