Forum Discussion

mrchips's avatar
mrchips
Regular Visitor
2 years ago
Solved

Create new Calculated Column for an index of Filtered Values

Hi,   I am trying to create a Calculated Column that creates an index based on the Filtered Values. I have an index on my Unfiltered Table and I'm using that to calculate the rank like below:   ...
  • DataInsights's avatar
    DataInsights
    2 years ago

    mrchips,

     

    Try these measures:

     

    Amount = SUM ( 'Table'[Amount] )
    Amount Within Budget = 
    // Show amount until cumulative sum of amount exceeds budget.
    VAR Budget =
        MAX ( 'Cost Budget'[Cost budget value] )
    VAR BaseTable =
        ALLSELECTED ( 'Table'[Category], 'Table'[Index] )
    VAR CalcTable =
        ADDCOLUMNS (
            BaseTable,
            "@Amount", [Amount],
            "@CumulativeSum",
                SUMX (
                    WINDOW ( 1, ABS, 0, REL, BaseTable, ORDERBY ( 'Table'[Index], ASC ) ),
                    [Amount]
                )
        )
    VAR FilterTable =
        FILTER ( CalcTable, [@CumulativeSum] <= Budget )
    VAR CategoryToInclude =
        SELECTCOLUMNS ( FilterTable, "Category", 'Table'[Category] )
    VAR Result =
        CALCULATE ( [Amount], KEEPFILTERS ( CategoryToInclude ) )
    RETURN
        Result

     

    You can adjust the Budget variable depending on how you obtain budget in your model.

     

    Sample data:

     

     

     

    Result:

     

    -----