Search code examples
powerbidaxpowerbi-desktop

Get the unique customers with the highest Item value in Table Visualization in Power BI


I am struglling to getting top 25 unique customers when filtering the table using Alert_Id. Basically, I have these columns in table which you can find below. The goal is to show top 25 unique customers based on highest value. The Item can be repeated but name has to be unique. I have tried so many different things but nothing seems to be working as expected because of Multiple customers have used multiple items and hence I am getting duplicate rows. The table has to be dymic because whenever user filters the table using Alert_id it should return those top unique customers that associated with Alert_id(Alert_id is a single selection). So whenever user select their Alert_id that table should display their data. I have tried below measure, First I created calculated column to break the tie for price because many Item shares the same price:

max price = Table[PRICE] + RAND()

Then I created another column to get max price for the customer name:

MAX column for table = CALCULATE(MAX('Table'[max price]), ALLEXCEPT(Table, Table[CUSTOMER_NAME]))

Then I created calculated table using these columns:

SELECTCOLUMNS(
    FILTER(Table, Table[max price]=Table[MAX column for table]), "Name" ,Table[CUSTOMER_NAME],"Item",Table[ITEM], "PRICE",Table[MAX column for table], "Alert_ID", Table[ID],  "DATE", Table[REQ_DATE], "ITEM_COUNT", Table[PK])

But, this is giving me all unique customers with the MAX value and I am getting blank table when I filter with Alert_ID even thought it has data but the customers are not with the MAX value. Basically, It's not dynamically capturing max values for each customer_name when filter is applied. And, I if there are multiple rows with same customer name which can have same exact value then I would choose any random row without considering which ITEM it is. I just want top 25 unique customers for one Alert_ID.

Here is the sample data, enter image description here

Here is expected output if I select Alert_ID = 123 from filter and it can be different when I select different Alert_ID.

enter image description here

FYI: I have tried topn with max price and even with RANKX but no luck. I always endedup having multiple customers.

Any help or lead will be highly appreciated!


Solution

  • I was able to figure out how to get unique values. Here is the solution that worked for me. First, I created calculated column with my price column and RAND function to break the ties:

    sum value = Table[PRICE] + RAND()

    Then, I have created one measure that calculates the rank:

    rank with table = RANKX(CALCULATETABLE(VALUES('Table'[ITEM]), ALLSELECTED('Table'[ITEM])),CALCULATE(SUM(Table[sum value])), ,DESC, Dense )

    Then I applied the filter on NAME column to get top 25 based on sum value calculated column. Also, dragged my measure on filters pane and applied the filter where Rank with table = 1.

    That's how I got unique names with highest valued ITEM.