Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Rank orders by value with multiple lines per order

Hello community, hope you can help me out ...    I've been trying to create a ranking of sales orders where some of them can have multiple line in the fact table since a given order can contain mul...
  • daxer-almighty's avatar
    daxer-almighty
    5 years ago

    Anonymous

     

    You can try this one. I suspect it's now doing what you wanted...

     

    Sales BG2 = 
    var CurrentlyVisibleGroups = DISTINCT( 'Order Groups'[Group] )
    var CurrentlyVisibleOrders = DISTINCT( SalesOrders[OrderNo] )
    var OrdersInCurrentlyVisibleGroups =
        CALCULATETABLE(
            var AllOrders = DISTINCT( SalesOrders[OrderNo] )
            return
            FILTER(
                CurrentlyVisibleOrders,
                var OrderRank = 
                    RANKX(
                        AllOrders,
                        [Sales],,
                        DESC
                    )
                var OrderGroup =
                    MAXX(
                        FILTER(
                            'Order Groups',
                            'Order Groups'[Min] < OrderRank
                            && 
                            OrderRank <= 'Order Groups'[Max]
                        ),
                        'Order Groups'[Group]
                    )
                return
                    OrderGroup in CurrentlyVisibleGroups
            ),
            ALLEXCEPT( SalesOrders, 'Date' ),
            // This directive should let you only rank
            // against orders that are relative to
            // all the visible products, visible
            // somewhere in your visual, not necessarily
            // just in the cell being evaluated.
            ALLSELECTED( 'Products' )
        )
    var Result =
        CALCULATE(
            [Sales],
            OrdersInCurrentlyVisibleGroups
        )
    return
        Result

     

    Bear in mind that 'Products' must be a dimension connected to your fact table 'SalesOrders'. And you should never slice in the UI by the columns of your fact tables. Only via dimensions. If you don't follow this rule... you'll be in trouble sooner or later. I tell you today.