Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.
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
)
)
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)
User | Count |
---|---|
54 | |
23 | |
19 | |
18 | |
17 |
User | Count |
---|---|
91 | |
87 | |
47 | |
28 | |
22 |