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
raj777karthik
Employee
Employee

TOPN dax function as variable

Need to show top 5 services in table visual.

 

Service NameCountTotal Count
Apple43346
Samsumg67346
mi80346
oppo84346
google72346

 

Measure written we not able to do sum the count of all Services,

Thread_ = VAR A=TOPN(5,SUMMARIZE(VALUES('Stack Tags'),'Stack Tags'[Service],'Stack Tags'[Thread__],"SUM_",DISTINCTCOUNT('Stack Tags'[Thread__])),'Stack Tags'[Thread__],DESC)
Return
CALCULATE(SUMX(A,SUM('Stack Tags'[Thread__])))
@
1 ACCEPTED SOLUTION

Hi @raj777karthik ,

 

Please replace measure with new column and try again. 

vyadongfmsft_0-1663135144587.png

 

vyadongfmsft_1-1663135265103.png

Best regards

Yadong Fang

View solution in original post

14 REPLIES 14
raj777karthik
Employee
Employee

Hi,

Thanks for this approach,but we need to do this in existing table considering relationship.If i create table function then only few slicer interacting and not all.

Hi @raj777karthik ,

 

If you don't want to create a new table, pleaser try following DAX to sort [Thread_] column:

 

Rank = RANKX('Stack Tags','Stack Tags'[Thread__],'Stack Tags'[Thread__],DESC)

 

vyadongfmsft_0-1663049557811.png

 

Calculate the sum of the top 5:

 

Total count = CALCULATE(SUM('Stack Tags'[Thread__]),FILTER(ALL('Stack Tags'),'Stack Tags'[Rank]<=5))

 

 

Add the [Rank] column to Filters to filter out the top5: 

vyadongfmsft_2-1663049775893.png

 

Best regards,

Yadong Fang

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

In this since my service oppo has 84 count the rank is taking all these for 1 and second service mi  has 80 count ,rank is taken as 85 .And for third its taking as rank 165.

Hi @raj777karthik ,

 

Please try following DAX to create three measures:

Count = COUNT('Stack Tags'[Service]) 

Rank = RANKX(ALLSELECTED('Stack Tags'),[Count],,DESC,Dense)

Total count = CALCULATE([Count],FILTER(ALL('Stack Tags'),'Stack Tags'[Rank]<=5))

vyadongfmsft_0-1663055030070.png

You will get result you want:

vyadongfmsft_1-1663055113241.png

Best regards,

Yadong Fang

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

 

In Rank measure,im getting values 1 and 2.

 

raj777karthik_0-1663057324232.png

 

Hi @raj777karthik ,

 

Can you tell me how the RANK measure did you write?

 

Best regards,

Yadong Fang

Same as you have mentioned.Created measure for this.

Rank = RANKX(ALLSELECTED('Stack Tags'),[Count],,DESC,Dense)

Hi @raj777karthik ,

 

OK, I know the reason of the problem.

 

Please try following DAX:

Rank = IF(HASONEVALUE('Stack Tags'[Service]),
    RANKX(ALL('Stack Tags'[Service]),[Count],,DESC,Dense)
)

Best regards,

Yadong Fang

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

 

Hi,

Thanks Rank is working fine as expected,but the total count measure is returning all the values.

Total count = CALCULATE([Count],FILTER(ALL('Stack Tags'),'Stack Tags'[Rank]<=5))

raj777karthik_0-1663062886882.png

 

Hi @raj777karthik ,

 

Can you tell me how the [Count] measure did you write?

 

It's better to share with me a screenshot of the original data (hide sensitive information).

 

Best regards

Yadong Fang

Measure written:

Count = COUNT('Stack Tags'[Service])
Rank = IF(HASONEVALUE('Stack Tags'[Service]),
    RANKX(ALL('Stack Tags'[Service]),[Count],,DESC,Dense)
)
Total count = CALCULATE([Count],FILTER(ALL('Stack Tags'),'Stack Tags'[Rank]<=5))
 
raj777karthik_0-1663134047996.png

 

Hi @raj777karthik ,

 

Please replace measure with new column and try again. 

vyadongfmsft_0-1663135144587.png

 

vyadongfmsft_1-1663135265103.png

Best regards

Yadong Fang

Thanks Yadong.Its working fine.

v-yadongf-msft
Community Support
Community Support

Hi @raj777karthik ,

 

This is my test table:

vyadongfmsft_3-1663035306014.png

 

Please try following DAX to create a new table:

New Table = TOPN(5,SUMMARIZE('Stack Tags','Stack Tags'[Service],'Stack Tags'[Thread__]),'Stack Tags'[Thread__],DESC)

vyadongfmsft_4-1663035365370.png

 

Then create a measure to calculate total count:

Total count = CALCULATE(SUMX('New Table',CALCULATE(SUM('New Table'[Thread__]))),ALL('New Table'))

 

You will get result you want:

vyadongfmsft_5-1663035523344.png

If I misunderstand your demands, please feel free to contact us in time.

 

Best regards,

Yadong Fang

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

 

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.