Forum Discussion

glycine76's avatar
glycine76
Frequent Visitor
3 years ago
Solved

Need help on dealing with category hierarchy count

Hello, 

 

Let's say I have a data structure like this

 

Level 1        Level 2          Status

A                    A1              Approved

A                   A2                 Approved

A                 A3                  Approved

B                    B1             Review

B                     B2              Approved

B                   B3                Review

C                  C1                Approved

C                 C2                Approved

C                C3                       Approved

 

Only when all items in level 2 are approved, level 1 items are approved. So how do I write a measure to count how many level 1 items are approved? 

Thanks for the help in advance!

  • Hey glycine76 ,

     

    this measure:

    Measure = 
    COUNTX(
        VALUES( 'Status'[Lvl1] )
        , var noOfRowsLvl2 = 
            COUNTROWS(
                CALCULATETABLE(
                    VALUES( 'Status'[Lvl2] )
                    , ALL( 'Status'[Lvl2] )
                    , ALL( 'Status'[Status] )
                )
            )
        var noOfRowsApproved = 
            COUNTROWS(
                    CALCULATETABLE(
                        FILTER( 
                            'Status'
                            , 'Status'[Status] = "Approved"
                        )
                        , ALL( 'Status'[Lvl2] )
                        , ALL( 'Status'[Status] )
                    )
                )
        return
        IF( noOfRowsLvl2 = noOfRowsApproved , 1 , BLANK() )
    )

    Allows creating this table visual:

    Please be aware that I enabled the option "Show items with no data" on the column Lvl1, this option does not affect the measure, but it helps to understand my sample data, and why Lvl1 = B is not considered "complete".

    Hopefully, this provides what you are looking for.

     

    Regards,

    Tom

    Use the table tool when you are providing simple sample data otherwise it will eat up time from people trying to help:

  • Hi glycine76 ,

     

    If you are using matrix, please try:

    Measure =
    IF (
        ISINSCOPE ( 'Table'[Level 2] ),
        MAX ( 'Table'[Status] ),
        IF (
            COUNTROWS ( FILTER ( 'Table', [Status] = "Approved" ) )
                = COUNTROWS ( 'Table' ),
            "Approved",
            "Review"
        )
    )
    

    Final output:

    Best Regards,

    Jianbo Li

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

4 Replies

  • Hey glycine76 ,

     

    this measure:

    Measure = 
    COUNTX(
        VALUES( 'Status'[Lvl1] )
        , var noOfRowsLvl2 = 
            COUNTROWS(
                CALCULATETABLE(
                    VALUES( 'Status'[Lvl2] )
                    , ALL( 'Status'[Lvl2] )
                    , ALL( 'Status'[Status] )
                )
            )
        var noOfRowsApproved = 
            COUNTROWS(
                    CALCULATETABLE(
                        FILTER( 
                            'Status'
                            , 'Status'[Status] = "Approved"
                        )
                        , ALL( 'Status'[Lvl2] )
                        , ALL( 'Status'[Status] )
                    )
                )
        return
        IF( noOfRowsLvl2 = noOfRowsApproved , 1 , BLANK() )
    )

    Allows creating this table visual:

    Please be aware that I enabled the option "Show items with no data" on the column Lvl1, this option does not affect the measure, but it helps to understand my sample data, and why Lvl1 = B is not considered "complete".

    Hopefully, this provides what you are looking for.

     

    Regards,

    Tom

    Use the table tool when you are providing simple sample data otherwise it will eat up time from people trying to help:

  • Hi glycine76 ,

     

    If you are using matrix, please try:

    Measure =
    IF (
        ISINSCOPE ( 'Table'[Level 2] ),
        MAX ( 'Table'[Status] ),
        IF (
            COUNTROWS ( FILTER ( 'Table', [Status] = "Approved" ) )
                = COUNTROWS ( 'Table' ),
            "Approved",
            "Review"
        )
    )
    

    Final output:

    Best Regards,

    Jianbo Li

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

  • Hi glycine76 ,

     

    Could you tell me if your problem has been solved? If it is, kindly Accept it as the solution. More people will benefit from it. Or if you are still confused about it, please provide me with more details about your table and your problem or share me with your pbix file after removing sensitive data.

     

    Refer to:

    How to provide sample data in the Power BI Forum

    How to Get Your Question Answered Quickly

     

    Best Regards,

    Jianbo Li

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