Forum Discussion

PDRTXRA's avatar
PDRTXRA
Helper I
3 years ago
Solved

Finding Best Product per Client

Hello. I have two tables. One with clients. One with sales.   I want to create 3 columns in my clients table.   First column: Name of the product in which they spent the most money. Second colu...
  • Anonymous's avatar
    Anonymous
    3 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 Zhou

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.