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
nadavsn
Advocate I
Advocate I

DAX - filtering the result of group by function

Hi all

I have a dataset (named IMP_UGE_DATA) with 2 relevant columns:  IMP_UGE_DATA[FAILED] which is a text represent an error code and  IMP_UGE_DATA[Total] which always have the value 1. 

I create a measure that counts the number of rows with the most frequent FAILED :

maxFailureCount =
VAR CountByFailureCode =
    GROUPBY (
        IMP_UGE_DATA,
        IMP_UGE_DATA[FAILED],
        "countFail", SUMX ( CURRENTGROUP (), 'IMP_UGE_DATA'[Total] )
    )
RETURN
    MAXX ( CountByFailureCode, [countFail] )

Now I want to create a new measure that returns the most frequent FAILED.

I tried to use the CountByFailureCode since it supposes to return a table but I can not get the FAILED column. 

how can I see the columns of the CountByFailureCode? 

any suggestion how can I get the most frequent FAILED? 

Thank you in advance for any help attempt. 

 

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi! Referencing columns in table variables is quite hard in DAX for some reason. But some iterator functions can do it. So if you don't want to use a calculated table for this, try someting like this:

MaxFailed =
VAR CountByFailureCode =
    SUMMARIZE (
        'IMP_UGE_DATA',
        [FAILED],
        "countFail", COUNTROWS ( 'IMP_UGE_DATA' )
    )
VAR RankFailure =
    ADDCOLUMNS (
        CountByFailureCode,
        "Rank", RANKX ( CountByFailureCode, [countFail] )
    )
RETURN
    CALCULATE ( MAXX ( FILTER ( RankFailure, [Rank] = 1 ), [FAILED] ) )

View solution in original post

6 REPLIES 6
Anonymous
Not applicable

Perhaps you can create a calculated table like this:

FailCountTable =
VAR CountByFailureCode =
    SUMMARIZE (
        'IMP_UGE_DATA';
        IMP_UGE_DATA[FAILED];
        "countFail"; COUNTROWS ( 'IMP_UGE_DATA' )
    )
RETURN
    ADDCOLUMNS (
        CountByFailureCode;
        "Rank"; RANKX ( CountByFailureCode; [countFail];; DESC )
    )

 

Then filter on Rank = 1 to get the top error code/codes.
I changed to SUMMARIZE and COUNTROWS instead of GROUPBY and SUMX. Then you don't need the Total column.

@Anonymous , @Greg_Deckler Thank you 

I tried the following DAX: 

FailCountTable =
VAR CountByFailureCode =
SUMMARIZE (
'IMP_UGE_DATA',
[FAILED],
"countFail", COUNTROWS ( 'IMP_UGE_DATA' )
)
var RankFailure =
ADDCOLUMNS (
CountByFailureCode,
"Rank", RANKX ( CountByFailureCode,[countFail])
)
return CALCULATE(filter(RankFailure, [Rank]=1),RankFailure[FAILED])
 
but I get an error "Cannot find table 'RankFailure'."
How can I approach to a column in a table var? what am I missing? 
 
Anonymous
Not applicable

Hi! Referencing columns in table variables is quite hard in DAX for some reason. But some iterator functions can do it. So if you don't want to use a calculated table for this, try someting like this:

MaxFailed =
VAR CountByFailureCode =
    SUMMARIZE (
        'IMP_UGE_DATA',
        [FAILED],
        "countFail", COUNTROWS ( 'IMP_UGE_DATA' )
    )
VAR RankFailure =
    ADDCOLUMNS (
        CountByFailureCode,
        "Rank", RANKX ( CountByFailureCode, [countFail] )
    )
RETURN
    CALCULATE ( MAXX ( FILTER ( RankFailure, [Rank] = 1 ), [FAILED] ) )

It does work. 

I did not understand why we needed the maxx when we only have one filtered row.

Is it a workaround solution to return the column?

Anonymous
Not applicable

It's a workaround because the X funktions can access columns like that in variables. Also notice that if two or more codes have the same number of failures, the max (in alphabetical order) will be returned. MIN the other way around...

I don't know why it's not possible to write for example VALUES(Variable[Column]). Perhaps just not implemented yet in DAX.

Greg_Deckler
Super User
Super User

Just create a table and return CountByFailureCode as the output. I think generally you would perhaps use ADDCOLUMNS to add a RANKX.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

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.

Top Solution Authors