cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
sar7aan
New Member

Get Max of a column grouping by another columns

I have a table similar to this:

sar7aan_0-1668419717441.png

all what I want is to add a new measure to a table in power bi that returns the maximum "Value" grouped by "Project" and "Category" columns 

1 ACCEPTED SOLUTION
v-xiaosun-msft
Community Support
Community Support

Hi @sar7aan 

 

I suggest that you can use CALCULATE function and EARLIER function to create a calculated column, then you will get the expected result.

Sample data:

vxiaosunmsft_0-1669168848590.png

Create a column.

Column =
CALCULATE (
    MAX ( 'table1'[VALUE] ),
    FILTER (
        'table1',
        'table1'[Project] = EARLIER ( 'table1'[Project] )
            && 'table1'[Category] = EARLIER ( 'table1'[Category] )
    )
)

Final output:

vxiaosunmsft_1-1669168890142.png

 

I attach my sample below for your reference.

 

Best Regards,
Community Support Team _ xiaosun

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

5 REPLIES 5
v-xiaosun-msft
Community Support
Community Support

Hi @sar7aan 

 

I suggest that you can use CALCULATE function and EARLIER function to create a calculated column, then you will get the expected result.

Sample data:

vxiaosunmsft_0-1669168848590.png

Create a column.

Column =
CALCULATE (
    MAX ( 'table1'[VALUE] ),
    FILTER (
        'table1',
        'table1'[Project] = EARLIER ( 'table1'[Project] )
            && 'table1'[Category] = EARLIER ( 'table1'[Category] )
    )
)

Final output:

vxiaosunmsft_1-1669168890142.png

 

I attach my sample below for your reference.

 

Best Regards,
Community Support Team _ xiaosun

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

@sar7aan , please try this measure:

 

 

MaxValueByProjectAndCategory = 
    CALCULATE(
        MAX('Table1'[Value])
        ,ALL('Table1'[Date])
    )

This yeilds these results:

EylesIT_0-1668420801912.png

 

 

Thanks EylesIT,
But the table above is just for example. 
Say that I have a table of 20 columns and I want to get the max of one column grouping by another two columns ...it won't be practical to exclude all the other 17 columns .... is there another way?

FreemanZ
Community Champion
Community Champion

In such case, typically a table would be created with the code below:
 
MaxTable =
ADDCOLUMNS(
    SUMMARIZE (Data, Data[Project], Data[Category]),
    "MaxValue",
    CALCULATE (MAX(Data[Value]))
)

 

With SUMMARIZE, we could include and summarize columns as many as needed. Columns from other table could also be included, as long as they are from a table on the one side of the relationship with Data table. 

Anonymous
Not applicable

@sar7aan , Could you please post a sample data table with the 20 columns and what the expected output should be, please?

 

When you say "it won't be practical to exclude all the other 17 columns" do you mean that you want to display all 20 columns in the table visual, but you still want this measure to only group by the Product and Category per row? If that is the case, then try this version of the measure:

 

 

MaxValueByProjectAndCategory = 
    VAR vProject = SELECTEDVALUE('Table1'[Project])
    VAR vCategory = SELECTEDVALUE('Table1'[Category])
    RETURN
    CALCULATE(
        MAX('Table1'[Value])
        ,ALL('Table1')
        ,'Table1'[Project] = vProject
        ,'Table1'[Category] = vCategory
    )

 

 

Helpful resources

Announcements
PBI Sept Update Carousel

Power BI September 2023 Update

Take a look at the September 2023 Power BI update to learn more.

Learn Live

Learn Live: Event Series

Join Microsoft Reactor and learn from developers.

Dashboard in a day with date

Exclusive opportunity for Women!

Join us for a free, hands-on Microsoft workshop led by women trainers for women where you will learn how to build a Dashboard in a Day!

MPPC 2023 PBI Carousel

Power Platform Conference-Power BI and Fabric Sessions

Join us Oct 1 - 6 in Las Vegas for the Microsoft Power Platform Conference.

Top Solution Authors
Top Kudoed Authors