DAX PERCENTILE.INC 按计算列中的类别

DAX PERCENTILE.INC by category in calculated column

我想在计算列中使用 PERCENTILE.INC 按类别计算四分位数 (ntile)。我的问题是这个问题的变体:

如果要按类别计算结果,如何修改问题的接受解?

Bucket = 
VAR N = 4
VAR Percentiles =
    ADDCOLUMNS (
        GENERATESERIES ( 1, N ),
        "Percentile", PERCENTILE.INC ( Table1[Col1], [Value] / N )
    )
RETURN
    MINX ( FILTER ( Percentiles, Table1[Col1] <= [Percentile] ), [Value] )

我试过了,但结果不是我想要的值:

VAR N = 4
VAR Percentiles =
    ADDCOLUMNS(
        CROSSJOIN( VALUES( Tab[Category] ), GENERATESERIES( 1, N ) ),
        "Percentile",
            PERCENTILEX.INC(
                VAR Category = 'Tab'[Category] RETURN FILTER( Tab, 'Tab'[Category] = Category ),
                Tab[Quantity] * 1.0,
                [Value] / N
            )
    )
RETURN
    MINX(
        FILTER(
            Percentiles,
            Tab[Quantity] <= [Percentile]
                && 'Tab'[Category] = [Category]
        ),
        [Value]
    )

编辑。

示例数据:

Table = 
DATATABLE (
    "No", INTEGER,
    "Category", STRING,
    "Quantity", DOUBLE,
    {
        {  1 , "apple"  , 1 },
        {  2 , "apple"  , 5 },
        {  3 , "apple"  , 1 },
        {  4 , "apple"  , 4 },
        {  5 , "apple"  , 1 },
        {  6 , "apple"  , 2 },
        {  7 , "apple"  , 5 },
        {  8 , "apple"  , 4 },
        {  9 , "banana" , 9 },
        { 10 , "banana" , 7 },
        { 11 , "banana" , 6 },
        { 12 , "banana" , 4 },
        { 13 , "banana" , 5 },
        { 14 , "banana" , 7 },
        { 15 , "banana" , 8 },
        { 16 , "banana" , 9 }
    }
)

这是我在上一个主题的评论中建议的解决方案。

Bucket =
VAR N = 4
VAR Percentiles =
    ADDCOLUMNS (
        GENERATESERIES ( 1, N ),
        "Percentile",
            VAR K = [Value] / N
            RETURN
                CALCULATE (
                    PERCENTILE.INC ( Table1[Col1], K ),
                    ALLEXCEPT ( Table1, Table1[Category] )
                )
    )
RETURN
    MINX ( FILTER ( Percentiles, Table1[Col1] <= [Percentile] ), [Value] )

我设法将我的解决方案付诸实践:

Bucket by Category = 
VAR N = 4
VAR Percentiles =
    ADDCOLUMNS(
        CROSSJOIN(
            SELECTCOLUMNS( VALUES( 'Table'[Category] ), "TheCategory", 'Table'[Category] ), // Change column name to new name
            GENERATESERIES( 1, N )
        ),
        "Percentile",
            PERCENTILEX.INC(
                VAR Category = 'Table'[Category] RETURN FILTER( 'Table', 'Table'[Category] = Category ),
                'Table'[Quantity] * 1.0,
                [Value] / N
            )
    )
RETURN
    MINX(
        FILTER(
            Percentiles,
            'Table'[Quantity] <= [Percentile]
                && 'Table'[Category] = [TheCategory] // Here we can reference the new name
        ),
        [Value]
    )

事实证明,唯一的解决方法是将 Percentiles table [Category] ​​的列名称更改为其他名称。因此,当我们引用新名称 [TheCategory] ​​时,很明显我们引用的是百分位数 table 而不是 'Table'。此解决方案可能未优化,因此我欢迎任何其他解决方案。