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

DAX for grouping value

Hi, 

 

I have been trying to create a dax code that would regroup my data like below:

IDStatusFREQ IDStatus
101173150Compliant1 101173150Compliant
101173547Compliant2 101173547Compliant
101173547First2 101173547Compliant
101173549Compliant2 101173549Not Compliant
101173549Not Compliant2 101173549Not Compliant
101173571Not Compliant1 101173571Not Compliant
101173572Not Compliant2 101173572Not Compliant
101173572Last2 101173572Not Compliant
101173702Compliant1 101173702Compliant
101173706Compliant2 101173706Not Compliant
101173706Not Compliant2 101173706Not Compliant
101173815Compliant2 101173815Not Compliant
101173815Not Compliant2 101173815Not Compliant
101173847Not Compliant1 101173847Not Compliant
101173868Compliant3 101173868Not Compliant
101173868Not Compliant3 101173868Not Compliant
101173868First3 101173868Not Compliant

 

 

The scenario is that when there are multiple values for one ID I would prioritise "not compliant" status if "not compliant" is not there i would take "compliant" over "first" or "last" status.

 

1 ACCEPTED SOLUTION
Icey
Community Support
Community Support

Hi @Anonymous ,

 

There is a small error in the expression provided by @amitchandakPlease modify it as follows:

New column = 
VAR _nc =
    COUNTX (
        FILTER ( 'Table', [ID] = EARLIER ( [ID] ) && [Status] = "Not Compliant" ),
        [ID]
    )
VAR _c =
    COUNTX (
        FILTER ( 'Table', [ID] = EARLIER ( [ID] ) && [Status] = "Compliant" ),
        [ID]
    )
VAR _f =
    COUNTX (
        FILTER ( 'Table', [ID] = EARLIER ( [ID] ) && [Status] = "First" ),
        [ID]
    )
RETURN
    SWITCH (
        TRUE (),
        NOT ( ISBLANK ( _nc ) ), "Not Compliant",
        NOT ( ISBLANK ( _c ) ), "Compliant",
        NOT ( ISBLANK ( _f ) ), "First",
        "Last"
    )

 

Or try this:

Column = 
VAR _nc =
    COUNTROWS (
        FILTER ( 'Table', [ID] = EARLIER ( [ID] ) && [Status] = "Not Compliant" )
    )
VAR _c =
    COUNTROWS (
        FILTER ( 'Table', [ID] = EARLIER ( [ID] ) && [Status] = "Compliant" )
    )
VAR _f =
    COUNTROWS ( 
        FILTER ( 'Table', [ID] = EARLIER ( [ID] ) && [Status] = "First" ) )
RETURN
    SWITCH (
        TRUE (),
        NOT ( ISBLANK ( _nc ) ), "Not Compliant",
        NOT ( ISBLANK ( _c ) ), "Compliant",
        NOT ( ISBLANK ( _f ) ), "First",
        "Last"
    )
Measure =
VAR _nc =
    COUNTROWS (
        FILTER (
            ALLSELECTED ( 'Table' ),
            [ID] = MAX ( [ID] )
                && [Status] = "Not Compliant"
        )
    )
VAR _c =
    COUNTROWS (
        FILTER (
            ALLSELECTED ( 'Table' ),
            [ID] = MAX ( [ID] )
                && [Status] = "Compliant"
        )
    )
VAR _f =
    COUNTROWS (
        FILTER ( ALLSELECTED ( 'Table' ), [ID] = MAX ( [ID] ) && [Status] = "First" )
    )
RETURN
    SWITCH (
        TRUE (),
        NOT ( ISBLANK ( _nc ) ), "Not Compliant",
        NOT ( ISBLANK ( _c ) ), "Compliant",
        NOT ( ISBLANK ( _f ) ), "First",
        "Last"
    )

COLUMN.JPGMEASURE.JPG

 

 

Best Regards,

Icey

 

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

2 REPLIES 2
Icey
Community Support
Community Support

Hi @Anonymous ,

 

There is a small error in the expression provided by @amitchandakPlease modify it as follows:

New column = 
VAR _nc =
    COUNTX (
        FILTER ( 'Table', [ID] = EARLIER ( [ID] ) && [Status] = "Not Compliant" ),
        [ID]
    )
VAR _c =
    COUNTX (
        FILTER ( 'Table', [ID] = EARLIER ( [ID] ) && [Status] = "Compliant" ),
        [ID]
    )
VAR _f =
    COUNTX (
        FILTER ( 'Table', [ID] = EARLIER ( [ID] ) && [Status] = "First" ),
        [ID]
    )
RETURN
    SWITCH (
        TRUE (),
        NOT ( ISBLANK ( _nc ) ), "Not Compliant",
        NOT ( ISBLANK ( _c ) ), "Compliant",
        NOT ( ISBLANK ( _f ) ), "First",
        "Last"
    )

 

Or try this:

Column = 
VAR _nc =
    COUNTROWS (
        FILTER ( 'Table', [ID] = EARLIER ( [ID] ) && [Status] = "Not Compliant" )
    )
VAR _c =
    COUNTROWS (
        FILTER ( 'Table', [ID] = EARLIER ( [ID] ) && [Status] = "Compliant" )
    )
VAR _f =
    COUNTROWS ( 
        FILTER ( 'Table', [ID] = EARLIER ( [ID] ) && [Status] = "First" ) )
RETURN
    SWITCH (
        TRUE (),
        NOT ( ISBLANK ( _nc ) ), "Not Compliant",
        NOT ( ISBLANK ( _c ) ), "Compliant",
        NOT ( ISBLANK ( _f ) ), "First",
        "Last"
    )
Measure =
VAR _nc =
    COUNTROWS (
        FILTER (
            ALLSELECTED ( 'Table' ),
            [ID] = MAX ( [ID] )
                && [Status] = "Not Compliant"
        )
    )
VAR _c =
    COUNTROWS (
        FILTER (
            ALLSELECTED ( 'Table' ),
            [ID] = MAX ( [ID] )
                && [Status] = "Compliant"
        )
    )
VAR _f =
    COUNTROWS (
        FILTER ( ALLSELECTED ( 'Table' ), [ID] = MAX ( [ID] ) && [Status] = "First" )
    )
RETURN
    SWITCH (
        TRUE (),
        NOT ( ISBLANK ( _nc ) ), "Not Compliant",
        NOT ( ISBLANK ( _c ) ), "Compliant",
        NOT ( ISBLANK ( _f ) ), "First",
        "Last"
    )

COLUMN.JPGMEASURE.JPG

 

 

Best Regards,

Icey

 

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

amitchandak
Super User
Super User

@Anonymous , a New column like

 

New column =
var _nc = countx(filter(table, [ID] =earlier([ID]), [Status] ="Not Compliant"),[ID])
var _c = countx(filter(table, [ID] =earlier([ID]), [Status] ="Compliant"),[ID])
var _f = countx(filter(table, [ID] =earlier([ID]), [Status] ="First"),[ID])
return
Switch( true(),
not(isblank(_nc)) , "Not Compliant",
not(isblank(_c)) , "Compliant",
not(isblank(_f)) , "First",
"Last"
)

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.