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

Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM. Register now.

Reply
afaherty
Helper V
Helper V

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!

 

1 ACCEPTED SOLUTION
AntrikshSharma
Super User
Super User

@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

 

AntrikshSharma_0-1760201761349.png

 

View solution in original post

9 REPLIES 9
AntrikshSharma
Super User
Super User

@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

 

AntrikshSharma_0-1760201761349.png

 

v-sgandrathi
Community Support
Community 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.

ThxAlot
Super User
Super User

Easy enough,

ThxAlot_0-1760043600970.png



Expertise = List.Accumulate(


        {Days as from Today},


        {Skills and Knowledge},


        (Current, Everyday) => Current & Day.LearnAndPractise(Everyday)


)



divyed
Super User
Super User

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")
    )
)
 
divyed_0-1759984886938.png

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.

LinkedIn : https://www.linkedin.com/in/neeraj-kumar-62246b26/

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.

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.

LinkedIn : https://www.linkedin.com/in/neeraj-kumar-62246b26/
Irwan
Super User
Super User

hello @afaherty 

 

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

Irwan_0-1759963939755.png

 

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.

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.

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.

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.