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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
susheel1347
Helper III
Helper III

Top N states within TOP N category

Hi All,

I am trying to achieve Top 2 states within Top 2 categories. Below is the sample excel data and expected output in image below. I am trying to do the same in Power BI but I couldn't do it. I got the Top 2 categories on the filter pane but I couldn't get Top 2 states from the Top 2 categories using DAX. Can you please help me on DAX?

 

 

StateCategoryCount
AZA9
FLA66
MNA51
IAA57
MOA1
COA47
NVA21
VAA16
WAA40
AZB57
FLB28
MNB74
IAB18
MOB18
COB3
NVB67
VAB3
WAB61
AZC14
FLC22
MNC20
IAC37
MOC45
COC53
NVC72
VAC15
WAC14

 

susheel1347_0-1620346303121.png

 

Thanks for your help.

2 ACCEPTED SOLUTIONS
mahoneypat
Employee
Employee

Please try this measure expression in your visual.  Note that it looks like the top two categories are A and B from your sample data.  Maybe I misunderstood your desired logic, and you can adapt the measure expression below if incorrect.

 

Top 2 of Top 2 =
VAR vCategoryTable =
    ADDCOLUMNS (
        ALL ( TopTwo[Category] ),
        "cCount"CALCULATE ( SUM ( TopTwo[Count] )ALL ( TopTwo[State] ) )
    )
VAR vTop2Cats =
    TOPN ( 2vCategoryTable, [cCount], DESC )
VAR vStateTable =
    ADDCOLUMNS (
        ALL ( TopTwo[State] ),
        "cStateCount"CALCULATE ( SUM ( TopTwo[Count] )vTop2Cats )
    )
VAR vTop2States =
    TOPN ( 2vStateTable, [cStateCount], DESC )
RETURN
    CALCULATE (
        SUM ( TopTwo[Count] ),
        KEEPFILTERS ( vTop2Cats ),
        KEEPFILTERS ( vTop2States )
    )

 

mahoneypat_0-1620347586971.png

 

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

Hi All,

Below is what I have updated & it works for filtering. Just for reference. Thanks @mahoneypat 

 

Top 2 of Top 2 =
VAR vCategoryTable =
ADDCOLUMNS (
ALL ( TopTwo[Category] ),
"cCount", CALCULATE ( SUM ( TopTwo[Count] ), FILTER(ALL ( TopTwo[State] ),TopTwo[Category]<>BLANK() && TopTwo[Category]<>"D" ))
)
VAR vTop2Cats =
TOPN ( 2, vCategoryTable, [cCount], DESC )
VAR vStateTable =
ADDCOLUMNS (
ALL ( TopTwo[State] ),
"cStateCount", CALCULATE ( SUM ( TopTwo[Count] ), vTop2Cats )
)
VAR vTop2States =
TOPN ( 2, vStateTable, [cStateCount], DESC )
RETURN
CALCULATE (
SUM ( TopTwo[Count] ),
KEEPFILTERS ( vTop2Cats ),
KEEPFILTERS ( vTop2States )
)

View solution in original post

4 REPLIES 4
mahoneypat
Employee
Employee

Glad you got it worked out.  Thanks for posting final expression.

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


mahoneypat
Employee
Employee

Please try this measure expression in your visual.  Note that it looks like the top two categories are A and B from your sample data.  Maybe I misunderstood your desired logic, and you can adapt the measure expression below if incorrect.

 

Top 2 of Top 2 =
VAR vCategoryTable =
    ADDCOLUMNS (
        ALL ( TopTwo[Category] ),
        "cCount"CALCULATE ( SUM ( TopTwo[Count] )ALL ( TopTwo[State] ) )
    )
VAR vTop2Cats =
    TOPN ( 2vCategoryTable, [cCount], DESC )
VAR vStateTable =
    ADDCOLUMNS (
        ALL ( TopTwo[State] ),
        "cStateCount"CALCULATE ( SUM ( TopTwo[Count] )vTop2Cats )
    )
VAR vTop2States =
    TOPN ( 2vStateTable, [cStateCount], DESC )
RETURN
    CALCULATE (
        SUM ( TopTwo[Count] ),
        KEEPFILTERS ( vTop2Cats ),
        KEEPFILTERS ( vTop2States )
    )

 

mahoneypat_0-1620347586971.png

 

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Hi @mahoneypat ,

Thanks for the reply. Yes you are correct, It should be A & B. I was using Random values in the excel so the charts did not show up correctly. The above solution works 🙂 Thanks. But I need to exclude some of the categories ( Blank(),D,..) before calculating this measure. How can I exclude in the above measure. Please help.  

Hi All,

Below is what I have updated & it works for filtering. Just for reference. Thanks @mahoneypat 

 

Top 2 of Top 2 =
VAR vCategoryTable =
ADDCOLUMNS (
ALL ( TopTwo[Category] ),
"cCount", CALCULATE ( SUM ( TopTwo[Count] ), FILTER(ALL ( TopTwo[State] ),TopTwo[Category]<>BLANK() && TopTwo[Category]<>"D" ))
)
VAR vTop2Cats =
TOPN ( 2, vCategoryTable, [cCount], DESC )
VAR vStateTable =
ADDCOLUMNS (
ALL ( TopTwo[State] ),
"cStateCount", CALCULATE ( SUM ( TopTwo[Count] ), vTop2Cats )
)
VAR vTop2States =
TOPN ( 2, vStateTable, [cStateCount], DESC )
RETURN
CALCULATE (
SUM ( TopTwo[Count] ),
KEEPFILTERS ( vTop2Cats ),
KEEPFILTERS ( vTop2States )
)

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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