Forum Discussion
Finding Best Product per Client
- Anonymous3 years ago
Hi PDRTXRA ,
You can try code as below to create calculated columns in 'Client' table.
First column = VAR _ADDCOLUMN = ADDCOLUMNS ( Sales, "Rank", RANKX ( FILTER ( Sales, Sales[ClientID] = EARLIER ( Sales[ClientID] ) ), Sales[SalesValue], , DESC, DENSE ) ) RETURN MAXX ( FILTER ( _ADDCOLUMN, AND ( [ClientID] = EARLIER ( Client[ClientID] ), [Rank] = 1 ) ), [ProductName] )Second column = VAR _ADDCOLUMN = ADDCOLUMNS ( Sales, "Rank", RANKX ( FILTER ( Sales, Sales[ClientID] = EARLIER ( Sales[ClientID] ) ), Sales[SalesValue], , DESC, DENSE ) ) RETURN MAXX ( FILTER ( _ADDCOLUMN, AND ( [ClientID] = EARLIER ( Client[ClientID] ), [Rank] = 2 ) ), [ProductName] )Third column = VAR _ADDCOLUMN = ADDCOLUMNS ( Sales, "Rank", RANKX ( FILTER ( Sales, Sales[ClientID] = EARLIER ( Sales[ClientID] ) ), Sales[SalesValue], , DESC, DENSE ) ) RETURN MAXX ( FILTER ( _ADDCOLUMN, AND ( [ClientID] = EARLIER ( Client[ClientID] ), [Rank] = 3 ) ), [ProductName] )Result is as below.
Here I create a virtual table in my code, if you don't want to add a rank column in 'Sales' table, you can use the workaround above. Or you can add a rank column firstly in Sales table and then column will be easier.
Rank = RANKX ( FILTER ( Sales, Sales[ClientID] = EARLIER ( Sales[ClientID] ) ), Sales[SalesValue], , DESC, DENSE )First column 1 = CALCULATE ( MAX ( Sales[ProductName] ), FILTER ( Sales, AND ( [ClientID] = EARLIER ( Client[ClientID] ), Sales[Rank1] = 1 ) ) )Second column and Third column are in the same logic.
Best Regards,
Rico ZhouIf this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Actually, this is finding the product where the client has the transaction with the most value.
What I want is to find the product where the client has spent more money. Meaning, I can have a client that bought 1 Boots for 20 but bough 10 Shirts for 5. Meaning he spent 20 on Boots and 50 on Shirts. In that case, with this Solution, I'm getting Boots where I should get Shirts. And just to finish, I would need to filter the sales table at the same time.