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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Yiyi
Helper I
Helper I

Count of Media Sources which are contribute for 80 % item creations

Hi Experts,

 

I have such scenario: There is total item amount and each media sources contributing to the amount. I want to find the count of the media sources adding the top 80% of total item amount. With some help, I learnt how to create rank but I really struggled to get the count by using COUNTROW(). I really appreciate if you could have a look at my code and hopefully find out my error. 

 

Please help with your suggestion.

 

CountMedia_80%Items =
VAR Total_Unique_Item =
    CALCULATE (
        DISTINCTCOUNT ( 'Main Data'[HeadlineID] ),
        ALLSELECTED ( 'Main Data' )
    )
VAR ThreesholdPercentage = 0.8
VAR Current_Total_Unique_Item =
    DISTINCTCOUNT ( 'Main Data'[HeadlineID] )
VAR SummarizedTable =
    SUMMARIZE (
        ALLSELECTED ( 'Main Data' ),
        'Main Data'[Media],
        "Unique_Item_Count", DISTINCTCOUNT ( 'Main Data'[HeadlineID] )
    )
VAR ADDRANK =
    ADDCOLUMNS (
        SummarizedTable,
        "RANK",
            RANKX ( SummarizedTable, [Unique_Item_Count],, DESC, SKIP )
                + IF (
                    COUNTX (
                        FILTER (
                            SummarizedTable,
                            [Unique_Item_Count] = EARLIER ( [Unique_Item_Count] )
                        ),
                        [Unique_Item_Count]
                    ) > 1,
                    RANKX (
                        FILTER (
                            SummarizedTable,
                            [Unique_Item_Count] = EARLIER ( [Unique_Item_Count] )
                        ),
                        [Media],
                        ,
                        ASC,
                        DENSE
                    ) - 1
                )
    )
VAR CumulativeSum =
    SUMX (
        FILTER (
            ADDRANK,
            [RANK]
                <= SUMX ( FILTER ( ADDRANK, [Media] = MAX ( 'Main Data'[Media] ) ), [RANK] )
        ),
        [Unique_Item_Count]
    )
RETURN
    COUNTROWS (
        FILTER (
            CALCULATETABLE (
                ADDRANK,
                ADDCOLUMNS (
                    ADDCOLUMNS (
                        ADDRANK,
                        "CumulativeSum",
                            SUMX (
                                FILTER (
                                    ADDRANK,
                                    [RANK]
                                        <= SUMX ( FILTER ( ADDRANK, [Media] = MAX ( 'Main Data'[Media] ) ), [RANK] )
                                ),
                                [Unique_Item_Count]
                            )
                    ),
                    "Cumulativepercentage", DIVIDE ( [CumulativeSum], Total_Unique_Item )
                ),
                ALLSELECTED ( 'Main Data'[Media] )
            ),
            CumulativeSum <= ThreesholdPercentage
        )
    )
1 REPLY 1
Sahir_Maharaj
Super User
Super User

Hello @Yiyi,

 

Can you please try:

CountMedia_80%Items = 
VAR TotalItemAmount = SUM('Main Data'[Item Amount])
VAR OrderedMedia = 
    ADDCOLUMNS (
        VALUES('Main Data'[Media]),
        "TotalMediaAmount",
        CALCULATE(SUM('Main Data'[Item Amount]), ALL('Main Data'), 'Main Data'[Media] = EARLIER('Main Data'[Media]))
    )
VAR TopMedia = 
    TOPN(
        COUNTROWS(OrderedMedia),
        OrderedMedia,
        [TotalMediaAmount],
        DESC
    )
VAR TopMediaAmount = SUMX(TopMedia, [TotalMediaAmount])
RETURN
    COUNTROWS(TopMedia) + IF(TopMediaAmount < TotalItemAmount * 0.8, 1, 0)

Did I answer your question? Mark my post as a solution, this will help others!

If my response(s) assisted you in any way, don't forget to drop me a "Kudos" 🙂

Kind Regards,
Sahir Maharaj
Data Scientist | Data Engineer | Data Analyst | AI Engineer
P.S. Want me to build your Power BI solution?
➤ Lets connect on LinkedIn: Join my network of 13K+ professionals
➤ Join my free newsletter: Data Driven: From 0 to 100
➤ Website: https://sahirmaharaj.com
➤ Email: sahir@sahirmaharaj.com
➤ Want me to build your Power BI solution? Lets chat about how I can assist!
➤ Join my Medium community of 30K readers! Sharing my knowledge about data science and artificial intelligence
➤ Explore my latest project (350K+ views): Wordlit.net
➤ 100+ FREE Power BI Themes: Download Now
LinkedIn Top Voice in Artificial Intelligence, Data Science and Machine Learning

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.

Top Solution Authors