RANKX doesn't work once filter is applied to visual

58 Views Asked by At

I have been looking all over the internet but can't find a solution that works for me. Hopefully someone here knows how to solve this problem.

I have a dataset with a year, country, product and sales, as well as a key, which is the (first three letters of the) country appended by the first letter of the product. Within the same year, this key is unique; however, the same key can appear over multiple years.

Years: 2021, 2022, 2023 | Countries: USA, China, Germany, Australia, Brazil | Products: Phone, Tablet, Computer | Sales: Integer value | Key (example): USAP, CHIP, CHIT, GERC, ...

The data

Next up, I have three additional tables:

CurrentDate (to filter the current year)

PreviousDate (to filter one of the previous years)

Then I have a table 'Xtox' with the entries 0to0, 0to1, 1to0, 1to1 and "All (except 0to0)". This is to filter on just the product-country combinations that were either available (1) of not (0) at PreviousDate and CurrentDate.

I have following measures

CurrentDateSales = SUMX(FILTER(Sheet1, Sheet1[Year] == CurrentDate[CurrentDate Value]), Sheet1[Sales])

PreviousDateSales = SUMX(FILTER(Sheet1, Sheet1[Year] == PreviousDate[PreviousDate Value]), Sheet1[Sales])

Xtox = IF(ISBLANK([PreviousDateSales]), IF(ISBLANK([CurrentDateSales]), "0to0", "0to1"), IF(ISBLANK([CurrentDateSales]), "1to0", "1to1"))

XtoxFilter = IF ([Xtox Value] == "All (except 0to0)", IF(Sheet1[Xtox] <> "0to0", 1, 0), IF(Sheet1[Xtox] == [Xtox Value], 1, 0))

Filters

Next, I make a matrix with the country as the row headers and PreviousDateSales and CurrentDateSales as the values.

To apply the Xtox filter, I add the key column as a second level to the row headers and drag the 'XtoxFilter' measure in the 'Filters on this visual' pane, requiring it to be equal to 1.

XtoxFilter

Next, I add the following rank measures to the matrix as values

PreviousSalesRank = RANKX(ALLSELECTED(Sheet1[Country]), [PreviousDateSales])
CurrentSalesRank = RANKX(ALLSELECTED(Sheet1[Country]), [CurrentDateSales])

Unfortunately, this gives a one in each row. Country and key

Changing key by product in the rows works, however in my real (confidential) problem, I need it to work based on this keycolumn. Country and Product as rows

How do I need to change the PreciousDateRank (and same for CurrentDateRank) such that it works with 'Country and key' as the rows of the matrix?

Massive thanks in advance if anyone can help me here. I am looking forward to your solutions!

I tried

RANKX(ALLSELECTED(Sheet1[Country]), [CurrentDateSales],,DESC,Dense)
VAR keyRank = RANKX(ALLSELECTED(Sheet1[KEY]), [CurrentDateSales],,DESC,Dense)

SWITCH(
        TRUE(),
        ISINSCOPE(Sheet1[KEY]),
        keyRank, 
        ISINSCOPE(Sheet1[Country]),
        RANKX(
            ALLSELECTED(Sheet1[Country]),
            [CurrentDateSales]
        )
    )

VAR currentCountry = SELECTEDVALUE(Sheet1[Country])
VAR table1 =
SUMMARIZE(
    CALCULATETABLE(
        FILTER(
            ALLSELECTED(Sheet1),
            Sheet1[Year] == CurrentDate[CurrentDate Value] && Sheet1[XtoxFilter] == 1
        )
    ),
    Sheet1[Country],
    "CountryMV",
    CALCULATE(
        SUM(
            Sheet1[Sales]
        )
    )
)

VAR table2 = 
    ADDCOLUMNS(
        table1,
        "rank",
        RANKX( table1, [CountryMV],,DESC,Dense)
    )
    
RETURN
    SUMX(FILTER(table2, [Country] == currentCountry), [rank])

This final one works for "All (except 0to0)" and "0to1", but unfortunately not for "1to1" and "1to0".

0

There are 0 best solutions below