Forum Discussion

TFRec's avatar
TFRec
Frequent Visitor
2 years ago
Solved

Nested (3 levels) TOPN using Power BI DAX not working for categories but working for other

I need to provide a matrix wherein I show the top 3 product codes, those codes' top 3 countries of origin, and those origin countries' top 3 destination countries.

I tried to use the below formula

 

 

Test Top 3 with multiple levels keep filter = 
VAR TOPNVALUE = 3
VAR TOPNCODE = TOPN(TOPNVALUE, ALLSELECTED(Table[Product_code]),[Sum Euros All Time])
VAR TOPNORIGINCOUNTRY = CALCULATETABLE(
    TOPN(TOPNVALUE, ALLSELECTED(Table[Origin_Country]),[Sum Euros All Time]),
    KEEPFILTERS(TOPNCODE)
)
VAR TOPNDESTINATIONCOUNTRY = CALCULATETABLE(
    TOPN(TOPNVALUE, ALLSELECTED(Table[Destination_Country]),[Sum Euros All Time]),
    KEEPFILTERS(TOPNCODE),
    KEEPFILTERS(TOPNORIGINCOUNTRY)
)


 
Return
CALCULATE(
    [SumEuros],
    KEEPFILTERS(TOPNCODE),
    KEEPFILTERS(TOPNORIGINCOUNTRY),
    KEEPFILTERS(TOPNDESTINATIONCOUNTRY)
)

 

 

It seems to work for most part, except that for some origin countries of some codes, for some reason it only shows one destination country.

I initially thought that maybe it was just that the origin in country only had one destination country, but no, it has multiple ones. As a matter of fact, for some reason, the total in the origin country (CR, 28.576 million) is the accurate total of the top 3 countries despite it only showing 1 top country.

The sum formulas referenced in the first DAX formula do not seem to be the issue from what I have tested, but just in case they are:

 

 

Sum Euros All Time = 
VAR time_period_min = MINX(ALLSELECTED(Table[Date]),Table[Date])
VAR time_period_max = MAXX(ALLSELECTED(Table[Date]),Table[Date])

Return CALCULATE(
            [SumEuros],
            ALL(Table[Date]),
            Table[Date]>=time_period_min,
            Table[Date]<=time_period_max
        )

 

 

 

 

 

SumEuros = SUM(Table[Sales])

 

 





 

8 Replies