Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

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

 

 

  • Anonymous's avatar
    Anonymous
    6 years ago

    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]))

     

11 Replies

  • Anonymous's avatar
    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:
     
     

     

    Now you just have to create a new table


    FilteredTable = filter(YourTable;YourTable[Included]="Yes")
     
    Please MARK ACCEPT Solution if accepted
  • Anonymous's avatar
    Anonymous
    Not applicable

    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's avatar
      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:

       

      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"))

       

       

      • Anonymous's avatar
        Anonymous
        Not applicable

        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's avatar
    Anonymous
    Not applicable

    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's avatar
      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]))

       

      • Anonymous's avatar
        Anonymous
        Not applicable

        This is perfect - thank you so much for your help