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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
somnath6309
Helper I
Helper I

Top categories and subcategories using Calculate Function

we want to produce a dashboard that shows the category and subcategory of products that sold more than twice the average sales amount. 
The folloiwng code produces the table : 

BestCategories =
VAR Subcategories =
     ALL ( 'Product'[Category], 'Product'[Subcategory] )
     VAR AverageSales =
      AVERAGEX (
              Subcategories,
             SUMX ( RELATEDTABLE ( Sales ), Sales[Quantity] * Sales[Net Price] )
               )
VAR TopCategories =
           FILTER (
                 Subcategories,
                     VAR SalesOfCategory =
                     SUMX ( RELATEDTABLE ( Sales ), Sales[Quantity] * Sales[Net Price] )
RETURN
SalesOfCategory >= AverageSales * 2
)
RETURN
TopCategories
However, is it possible to write a shorter and more efficient code using CALCULATE and Filter context ?

I have attached the Model View of the data set and the Out Put of the Code for reference.

Regards,
Somnath6309

somnath6309_0-1698761084774.png

somnath6309_2-1698761127998.png

 



 

 

 

1 REPLY 1
Anonymous
Not applicable

Hi @somnath6309 ,

 

You can use the following DAX code:

BestCategories =
CALCULATETABLE (
    SUMMARIZE (
        Sales,
        'Product'[Category],
        'Product'[Subcategory],
        "Total Sales", SUM ( Sales[Quantity] * Sales[Net Price] )
    ),
    FILTER (
        ALLSELECTED ( 'Product'[Category], 'Product'[Subcategory] ),
        [Total Sales] >= 2 * AVERAGE ( Sales[Quantity] * Sales[Net Price] )
    )
)

Hope it helps.

 

Best Regards,

Stephen Tao

 

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

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.