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

Sum of TOPN

Hey guys,

 

I've watched this youtube video back in a while and I wondered if would it be possible to have both the “Others” information AND the TOPN that is being filtered. For instance, in the image attached below I show what I aim to calculate, but I could not find a way to calculate it yet, so here I am 🙂 

 

Sem título.png

 

The measures is as follows

 

First off, a parameter is created:

TopN = GENERATESERIES ( 1, 20, 1 )

 

Then, the Others name was created and appended into the original table:

Product Names = UNION ( ALLNOBLANKROW ( 'Product'[Product Name] ), { "Others" } )

 

Afterwards, a visible row measure is created in order to only show those lines that are smaller than or equal to the value in the parameter previously created (this is set into the Filter panel of the table):

Visible Row = 
VAR Ranking = [Ranking]
VAR TopNValue = [TopN Value]
VAR Result =
    IF ( NOT ISBLANK ( Ranking ),
        ( Ranking <= TopNValue ) - ( Ranking = TopNValue + 1 ))
RETURN Result

 

The last step was to create the measure that computes the total for “Others”:

Sales Amt = 
VAR SalesOfAll =
    CALCULATE ( [Sales Amount],
        REMOVEFILTERS ( 'Product Names' ))
RETURN IF ( NOT ISINSCOPE ( 'Product Names'[Product Name] ),

        -- Calculation for a group of products 
        SalesOfAll,

        -- Calculation for one product name
        VAR ProductsToRank = [TopN Value]
        VAR SalesOfCurrentProduct = [Sales Amount]
        VAR IsOtherSelected =
            SELECTEDVALUE ( 'Product Names'[Product Name] ) = "Others"
        RETURN
            IF (
                NOT IsOtherSelected,
                
                -- Calculation for a regular product
                SalesOfCurrentProduct,

                -- Calculation for Others
                VAR VisibleProducts =
                    CALCULATETABLE (
                        VALUES ( 'Product' ),
                        ALLSELECTED ( 'Product Names'[Product Name] )
                    )
                VAR ProductsWithSales =
                    ADDCOLUMNS (
                        VisibleProducts,
                        "@SalesAmount", [Sales Amount]
                    )
                VAR SalesOfTopProducts =
                    SUMX (
                        TOPN (
                            ProductsToRank,
                            ProductsWithSales,
                            [@SalesAmount]
                        ),
                        [@SalesAmount]
                    )
                VAR SalesOthers = 
                    SalesOfAll - SalesOfTopProducts
                RETURN
                    SalesOthers))

 

 Now I ask: do someone knows how to calculate the TOPN filtered to show it along with the Others in the same table?

 

Thanks in advance!

 

Pbi file here 

1 ACCEPTED SOLUTION
SpartaBI
Community Champion
Community Champion

@Anonymous here is a link to download the file with the solution:
C0120 - Filter top 3 products with a row for others using DAX in Power BI.pbix

I took the file from their article and added the logic to also show the TopN filtered. 



2022-05-19 17_30_22-Re_ Need help on DAX function with measure vs colu... - Microsoft Power BI Commu.png


Full-Logo11.png

SpartaBI_3-1652115470761.png   SpartaBI_1-1652115142093.png   SpartaBI_2-1652115154505.png

Showcase Report – Contoso By SpartaBI

View solution in original post

6 REPLIES 6
SpartaBI
Community Champion
Community Champion

@Anonymous here is a link to download the file with the solution:
C0120 - Filter top 3 products with a row for others using DAX in Power BI.pbix

I took the file from their article and added the logic to also show the TopN filtered. 



2022-05-19 17_30_22-Re_ Need help on DAX function with measure vs colu... - Microsoft Power BI Commu.png


Full-Logo11.png

SpartaBI_3-1652115470761.png   SpartaBI_1-1652115142093.png   SpartaBI_2-1652115154505.png

Showcase Report – Contoso By SpartaBI

Hey Sparta, how are you doing?

 

Do you know how to incorporate an Excel file into the power bi.pbix file? I mean, there's no separate database, but only one attached to the file. Do you know how to do that?

 

I have a database that I want to make it to be “inside” my .pbix file, but I don't know how to do that.

 

To illustrate my point, take this image into consideration:

 

File.png

 

Thanks in advance.

Anonymous
Not applicable

This is exactly what I was looking for, buddy. Thanks a lot for the solution you provided

@Anonymous welcome 🙂

v-shex-msft
Community Support
Community Support

HI @Anonymous,

For your requirement, I'd like to suggest you create a variable with summarize function to summary table records with categories and calculations.

Then you can simply use the iterator function(e.g. sumx, averagex) and filter conditions to apply the second aggreated and filter effects on it.

Reference links:

Measure Totals, The Final Word - Microsoft Power BI Community

My Favorite DAX Feature: SELECTEDVALUE with SWITCH | Winston-Salem Power BI User Group (pbiusergroup.com)

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.
Anonymous
Not applicable

Thanks for your support, buddy 🤝

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.