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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
JefJacobs
New Member

Find the nth largest value from multiple columns ("fields") for each row ("observation")

Hi all. Newbie to DAX here. This issue is puzzling me. I know how to do it in Excel, but there must be a way in DAX/PBI. 

Let's assume I have a table where each row represents a unique student in a school. So, each row represents a unique student, e.g., Student1, Student2, ... StudentN. Assume there different subjects that each learners participates in. Subjects are the columns, so e.g,. History, Math, Biology, etc. For every subject a student has a test score out of 100 (the data).

 

I can find the Max score for each learner. But what I need is a new column where the second lowest score out of each of the subjects is presented. Bonus points if a second new column is generated that returns the column name (i.e., Subject name) related to the second lowest score is presented. I'm not concerned if there are two or more subjects with the same second lowest score. 

 

NameBiologyMathHistoryAccountingDAX2ndLowScore2ndLowSubject
Student1556575252555Bilogy
Student2252627374726Math
Student3456723671223History
Student4764523458545Math
1 ACCEPTED SOLUTION
eliasayyy
Memorable Member
Memorable Member

hello @JefJacobs ,

lets say this is your data

annonymous1999_0-1692874477214.png


it looks like your table is pivoted so lets reverse it 

select all the subjects like so

annonymous1999_1-1692874536882.png


press on transform in the ribbon and choose unpivot columns

annonymous1999_2-1692874569102.png


your results will be this

annonymous1999_3-1692874589895.png

 

now load your data and lets go back to dax

it looks like you want to find the second lowest grade for each student 

start by creating a new calculated column called rank grades

 

Rank Grades = 
RANKX(FILTER('Table','Table'[Name]=EARLIER('Table'[Name])),'Table'[Value],,DESC,Dense)

 


this measure will rank your grades by student id

result

annonymous1999_4-1692874839908.png


now create other columns 

 

Second low Grade = 
CALCULATE(
    MAX('Table'[Value]),ALLEXCEPT('Table','Table'[Name]),'Table'[Rank Grades] = 2)

 



and 

 

Second Grade Subject = 
VAR _c = CALCULATE(
    COUNT('Table'[Value]),ALLEXCEPT('Table','Table'[Name]),'Table'[Rank Grades] = 2)
    return
IF(_c =1 , CALCULATE(MAX('Table'[Subject]),ALLEXCEPT('Table','Table'[Name]),'Table'[Rank Grades] = 2),
CONCATENATEX(FILTER('Table','Table'[Name]=EARLIER('Table'[Name]) && 'Table'[Rank Grades] = 2),'Table'[Subject]," || ")
)

 


final result

annonymous1999_5-1692875475577.png

if you want to repivot it just do a new table 

Pivot = 
SUMMARIZE(
    'Table',
    'Table'[Name],
    "Accounting" , CALCULATE(SUM('Table'[Value]),'Table'[Subject] = "Accounting"),
    "Math" , CALCULATE(SUM('Table'[Value]),'Table'[Subject] = "Math"),
    "Biology" , CALCULATE(SUM('Table'[Value]),'Table'[Subject] = "Biology"),
    "History" , CALCULATE(SUM('Table'[Value]),'Table'[Subject] = "History"),
    "Second Lowest Grade" , MAX('Table'[Second low Grade]),
    "Subject Lowest" , MAX('Table'[Second Grade Subject])
)

annonymous1999_6-1692875827585.png

 



View solution in original post

3 REPLIES 3
eliasayyy
Memorable Member
Memorable Member

hello @JefJacobs ,

lets say this is your data

annonymous1999_0-1692874477214.png


it looks like your table is pivoted so lets reverse it 

select all the subjects like so

annonymous1999_1-1692874536882.png


press on transform in the ribbon and choose unpivot columns

annonymous1999_2-1692874569102.png


your results will be this

annonymous1999_3-1692874589895.png

 

now load your data and lets go back to dax

it looks like you want to find the second lowest grade for each student 

start by creating a new calculated column called rank grades

 

Rank Grades = 
RANKX(FILTER('Table','Table'[Name]=EARLIER('Table'[Name])),'Table'[Value],,DESC,Dense)

 


this measure will rank your grades by student id

result

annonymous1999_4-1692874839908.png


now create other columns 

 

Second low Grade = 
CALCULATE(
    MAX('Table'[Value]),ALLEXCEPT('Table','Table'[Name]),'Table'[Rank Grades] = 2)

 



and 

 

Second Grade Subject = 
VAR _c = CALCULATE(
    COUNT('Table'[Value]),ALLEXCEPT('Table','Table'[Name]),'Table'[Rank Grades] = 2)
    return
IF(_c =1 , CALCULATE(MAX('Table'[Subject]),ALLEXCEPT('Table','Table'[Name]),'Table'[Rank Grades] = 2),
CONCATENATEX(FILTER('Table','Table'[Name]=EARLIER('Table'[Name]) && 'Table'[Rank Grades] = 2),'Table'[Subject]," || ")
)

 


final result

annonymous1999_5-1692875475577.png

if you want to repivot it just do a new table 

Pivot = 
SUMMARIZE(
    'Table',
    'Table'[Name],
    "Accounting" , CALCULATE(SUM('Table'[Value]),'Table'[Subject] = "Accounting"),
    "Math" , CALCULATE(SUM('Table'[Value]),'Table'[Subject] = "Math"),
    "Biology" , CALCULATE(SUM('Table'[Value]),'Table'[Subject] = "Biology"),
    "History" , CALCULATE(SUM('Table'[Value]),'Table'[Subject] = "History"),
    "Second Lowest Grade" , MAX('Table'[Second low Grade]),
    "Subject Lowest" , MAX('Table'[Second Grade Subject])
)

annonymous1999_6-1692875827585.png

 



Thank you very much! In my actual file I have 100k+ students and 50 subjects. So, a bit of writing to do. But this will work. Will the pivoting and repivoting affect my other calculated columns? Or should I just redo my other measures after repivoting? 

if you inpivot then yes it might affect your other calculations becasue you are making all subjects into one column

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.