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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
MrBenn
Helper II
Helper II

Creating a new table from DAX displaying only highest values in a category

I need to create a table using DAX from an existing table as below.  The new table should only contain the highest score in each area.  Staff may have tied highest scores in each area and staff do not have the same areas or number of areas.  Please could someone help create the DAX for this?

 

 ScoreAreaIncluded in new table?
Person 16AYes
Person 14BYes
Person 15CYes
Person 14ANo
Person 14BNo

 

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

I had created a small pbix to play around but now I closed pbi as I thought the problem was solved, so I can't test but if the previous formula was

FilteredTable =
SUMMARIZE(filter(YourTable;YourTable[Included]="Yes");YourTable[Area];YourTable[Score];"Max personid";Max(YourTable[Personid]))

and you need also to group for person, should be

FilteredTable =
SUMMARIZE(filter(YourTable;YourTable[Included]="Yes");YourTable[PersonId]YourTable[Area];YourTable[Score];"Max ID";Max(YourTable[ID]))

 

View solution in original post

11 REPLIES 11
MrBenn
Helper II
Helper II

Source table:

 IDSeriesTypeScore
Person 18001AA5
Person 18002AA5
Person 18003BA5
Person 18004BA4
Person 28005AA6
Person 28006AA5

 

Required table:

 IDSeriesTypeScore
Person 18002AA5
Person 18004BA4
Person 28005AA6

 

Summarising the table would be fine, however I like your idea of keeping the original table with the "Yes" or "No" columns and then being able to create the measures using that column as a filter.

Anonymous
Not applicable

I had created a small pbix to play around but now I closed pbi as I thought the problem was solved, so I can't test but if the previous formula was

FilteredTable =
SUMMARIZE(filter(YourTable;YourTable[Included]="Yes");YourTable[Area];YourTable[Score];"Max personid";Max(YourTable[Personid]))

and you need also to group for person, should be

FilteredTable =
SUMMARIZE(filter(YourTable;YourTable[Included]="Yes");YourTable[PersonId]YourTable[Area];YourTable[Score];"Max ID";Max(YourTable[ID]))

 

This is perfect - thank you so much for your help

MrBenn
Helper II
Helper II

The  calculated column would be a much better solution, thanks.  However I can't get it to work as I forgot to mention there are other columns at play - a time series column 'series' and a 'type'.  the 'person' gets scores for all series and types, so that:

 

Quarter 1

 scoreareaseriestype(included)
Person 15AQuarter 1ProjectionY
Person 15AQuarter 1ProjectionN
Person 16AQuarter 2ProjectionY
Person 15AQuarter 2Projection N
Person 15AQuarter 2TargetY
Person 15AQuarter 2TargetN

 

I also need to exclude tied results, which I don't think the original suggestion does - e.g if person 1 gets a high score of 6, this is only represent in one row, rather than counting as 'Y' for any rows with the same highest score.

 

I've tried the below but without success.  Where am I going wrong?

 

Included =

VAR thisArea=mytable[area]

VAR thisSeries=mytable[series]

VAR thisType=mytable[type]

RETURN

IF(RANKX(FILTER(mytable,mytable [disc_code]=thisDisc&&mytable[series]=thisSeries&&mytable [type]=thisType),mytable[score])=1,"Y","N")

 

 

Any help greatly appreciated.

Anonymous
Not applicable

hi

 

regarding your formula to use other columns to group, it's correct, maybe clean up the spaces, so it should work.

 

Included =
VAR thisArea = mytable[area]
VAR thisSeries = mytable[series]
VAR thisType = mytable[type]
RETURN
    IF (
        RANKX (
            FILTER (
                mytable,
                mytable[disc_code] = thisDisc
                    && mytable[series] = thisSeries
                    && mytable[type] = thisType
            ),
            mytable[score]
        ) = 1,
        "Y",
        "N"
    )

 

Regarding the ties, i'm not sure how you want to handle a case like this:

 

image.png

This person has two IDENTICAL rows, how could you define which one is Y and which one is N?

You can keep my solution and in this way BOTH of these rows will be "Y".

Then your filtered table will have to use DISTINCT, that will remove all duplicated rows

 

 

FilteredTable = distinct(filter(YourTable;YourTable[Included]="Yes"))

 

 

Brilliant - thank you! I'm almost there with it - with regards to ties, it doesn't matter which is "Y" and "N". I also have a unique ID column to differentiate between them.  If I could ask one last thing - how might in corporate the unique ID so that only the tie with the highest ID number is "Y"?

 

Many thanks for all of your help with this!

Anonymous
Not applicable

So then is a different question, as you put all "Person 1" and they looked like all the same person.

So yuor result (simplified, removing other grouping columns) at the moment is this

 

image.png

 

but in this example you would like to have only the second, third and fourth rows (so for each Area, highest score and highest person id)? So it's not anymore a DISTINCT as the person id will be different. You need to group and return only the max, so I replicate @FrankAT  and use


FilteredTable =
SUMMARIZE(filter(YourTable;YourTable[Included]="Yes");YourTable[Area];YourTable[Score];"Max personid";Max(YourTable[Personid]))

which will generate
 
image.png
which looks what you need

 

Sorry I don't think I'm being very clear. "Person 1" is the same person and against each person is a set of results each with an ID - your previous  solution is very close to what I need, but I need to be able to disregard ties within a group by using an ID field (let's say it's an ID of the scores) or index column. i.e. identical scores in the same series with the same type and against the same person are calculated 'Y' for the highest ID.

 

 IDSeriesTypeScoreIncluded
Person 18003AA5Y
Person 18002AA5N

 
I hope that makes sense

 

Thanks again

Anonymous
Not applicable

can you show what is the expected result? It's easier than going iteratively 🙂

FrankAT
Community Champion
Community Champion

Hi,

give this a try:

 

12-02-_2020_16-06-51.png

 

Regards FrankAT

Anonymous
Not applicable

I like to approach these jobs with a step by step method so it's easier to debug and to understand. 

1) create a calculated column that has a "yes" in your value if the row should be included in the final table. To do so

 

Included =
VAR thisArea=YourTable[Area]
RETURN
IF(
    RANKX(filter(YourTable;YourTable[Area]=thisArea);YourTable[Score])=1;
"Yes";
"No")

So this one will Rank all of the items of same area from 1 to N, putting the highest with 1, the second with 2 etc.
Then if RANK 1 is means that it's the highest so it will set YES.

Here's the result:
 
 

image.png

 

Now you just have to create a new table


FilteredTable = filter(YourTable;YourTable[Included]="Yes")
 
Please MARK ACCEPT Solution if accepted

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.