Forum Discussion

andimohr's avatar
andimohr
New Member
2 years ago
Solved

Custom Ranking for Sorting

Our aim is to get the price for each ID for the most recent date. As we have different types, we want to weight the type accordingly to get the most relevant price if more than one type occurs on th...
  • andimohr's avatar
    2 years ago

    I fixed it - here is the simple solution: so the issue is just on simple KEEPFILTERS in the right place. Check that in VAR PriceForReturn. I chose to publish a lengthy version of the code with additional VARS to make it more readable for entry level DAX enthusiasts - like me 🙂

    Measure = 
    
    VAR SelectedId =
        SELECTEDVALUE('contracts'[RefId])
    
    VAR IdSubset =
        ADDCOLUMNS (
            CALCULATETABLE (
                'contracts',
                'contracts'[Type] IN { "Change Milestone", "Milestone", "Change Project", "Project" }
                    && NOT ISBLANK ( 'contracts'[EK] )
                    && 'contracts'[CreatedAtDate] <= MAX ( 'contracts_PersonSkillMap'[CreatedAtDate] )
                    && 'contracts'[RefId] = SelectedMitarbeiter
            ),
            "RankBy",
            COMBINEVALUES(" ",
                FORMAT ( 'contracts'[CreatedAtDate], "yyyyMMdd" ),
                SWITCH (
                    'contracts'[Type],
                    "Change Milestone", "4",
                    "Change Project", "3",
                    "Project", "2",
                    "Contract", "1",
                    "0"
                ), 'contracts'[Price]
            ),      
            "Price Value",
            'contracts'[Price]
        )
        
    VAR RankByForFilter = MAXX(IdSubset,[Rankby]) 
    
    VAR PriceForReturn = 
        MAXX(
            FILTER( 
                KEEPFILTERS(IdSubset),
                [RankBy] = RankByForFilter
            ),
            [Price Value]
        )
        
    RETURN 
        PriceForReturn


    When you are new to DAX, coming from SQL, you have a lot to learn. Up to now I only considered that context is a big thing in DAX. Now I learned how essential - and at the same time tricky - context is.

    Open is if I really need to add "Price Value" to the IdSubset... or if I could just return