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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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?

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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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