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
Anonymous
Not applicable

Group by one column and get average from another column

Hello all

I have following table

DAX1.png

and I want to get with DAX avg for each character, so result should be this

DAX2.png

unfornatelly GROUP BY is not working as I expect..can anyone help me to understand where I am wrong?

 

Error2.pngError1.png

and what should be correct formula ?

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Anonymous 

 

Sure, the SUMMARIZE() function can instead of the GROUPBY():

MEASURE 2 =
VAR _vtable =
    SUMMARIZE (
        ALLSELECTED ( 'Table' ),
        'Table'[Char],
        "_AVG", AVERAGE ( 'Table'[Value] )
    )
RETURN
    MAXX ( FILTER ( _vtable, [Char] = SELECTEDVALUE ( 'Table'[Char] ) ), [_AVG] )

vzhengdxumsft_0-1719450731517.png

As my testing, measure2 runs faster than measure and seems to use less memory.


About ALLSELECTED() function, it's not necessary in table, in measure, the ALLSELECTED() function is often used.

This link can be help:

Understand the Filter Context and How to Control i... - Microsoft Fabric Community

 

The _AVGTable returns a table:

vzhengdxumsft_1-1719451239560.png

[Char] = SELECTEDVALUE ( 'Table'[Char] ) //The [Char] is the [Char] column in _AVGTable, the SELECTEDVALUE ( 'Table'[Char] ) returns the [Char] in Original Table, this function is to get the right outcome from the virtual table _AVGTable.

 

Best Regards

Zhengdong Xu
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

3 REPLIES 3
Anonymous
Not applicable

Hi @Anonymous 

 

The GROUPBY() function returns a table, so you can get the right outcome in calculated table:

vzhengdxumsft_0-1719278226668.pngvzhengdxumsft_1-1719278275226.png

Or you can change the measure into this:

MEASURE =
VAR _AVGTable =
    GROUPBY (
        ALLSELECTED ( 'Table' ),
        'Table'[Char],
        "_AVG", AVERAGEX ( CURRENTGROUP (), 'Table'[Value] )
    )
RETURN
    MAXX ( FILTER ( _AVGTable, [Char] = SELECTEDVALUE ( 'Table'[Char] ) ), [_AVG] )

The result is as follow:

vzhengdxumsft_2-1719278413340.png

 

Best Regards

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

Anonymous
Not applicable

thanks, and 3 more mini questions 🙂 before I click on Accept as a solution
1) can I use summarize instead of group by ? is better?

2) why are used functions

    -ALLSELECTED in table ? is it neccessary ?
     -MAXX (when FILTER is iterator of the filtered table)

    -  [Char] = SELECTEDVALUE ( 'Table'[Char] ) ? Sorry I dont get it, because this condition will be always true

 

sorry to bother you, but I want to understand the logic of it 😞

Anonymous
Not applicable

Hi @Anonymous 

 

Sure, the SUMMARIZE() function can instead of the GROUPBY():

MEASURE 2 =
VAR _vtable =
    SUMMARIZE (
        ALLSELECTED ( 'Table' ),
        'Table'[Char],
        "_AVG", AVERAGE ( 'Table'[Value] )
    )
RETURN
    MAXX ( FILTER ( _vtable, [Char] = SELECTEDVALUE ( 'Table'[Char] ) ), [_AVG] )

vzhengdxumsft_0-1719450731517.png

As my testing, measure2 runs faster than measure and seems to use less memory.


About ALLSELECTED() function, it's not necessary in table, in measure, the ALLSELECTED() function is often used.

This link can be help:

Understand the Filter Context and How to Control i... - Microsoft Fabric Community

 

The _AVGTable returns a table:

vzhengdxumsft_1-1719451239560.png

[Char] = SELECTEDVALUE ( 'Table'[Char] ) //The [Char] is the [Char] column in _AVGTable, the SELECTEDVALUE ( 'Table'[Char] ) returns the [Char] in Original Table, this function is to get the right outcome from the virtual table _AVGTable.

 

Best Regards

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

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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