ROW_NUMBER() PARTITION BY ORDER BY 等效项的 DAX 表达式

You*_*tan 6 sql row-number dax powerbi

我有一个这样的SQL语句:

(ROW_NUMBER() OVER (PARTITION BY a.[market], [MEASURE_TYPE] 
                    ORDER BY AM, REP, ORDER_KEY)) AS ORDER_KEY
Run Code Online (Sandbox Code Playgroud)

我想写一个DAX来实现上面的SQL语句。

Kos*_*kai 3

这在 DAX 中并不像在 SQL 中那么简单。这是一个例子:

Order Key Within Partition = 
VAR CurrentMarket = [Market]
VAR CurrentMeasureType = [MeasureType]
VAR CurrentAM = [AM]
VAR CurrentREP = [REP]
VAR CurrentOrderKey = [OrderKey]

VAR CurrentPartition = FILTER (
    a, -- the table name
    [Market] = CurrentMarket
    && [MeasureType] = CurrentMeasureType
)

RETURN SUMX (
    CurrentPartition,
    IF (
        ISONORAFTER (
            CurrentAM, [AM], ASC,
            CurrentREP, [REP], ASC,
            CurrentOrderKey, [OrderKey], ASC
        ),
        1
    )
)
Run Code Online (Sandbox Code Playgroud)

结果

编辑: Power Query 会更好地实现这一点。

let
    /* Steps so far */
    Source = ...,
    ...
    a = ...,
    /* End of steps so far */

    /* Add steps below to add Order Key Within Partition column */
    Partitions = Table.Group(
        a,
        {"Market", "MeasureType"}, {"Partition", each _}
    )[Partition],
    AddedOrderKeys = List.Transform(
        Partitions,
        each Table.AddIndexColumn(
            Table.Sort(_, {"AM", "REP", "OrderKey"}),
            "Order Key Within Partition",
            1
        )
    ),
    Result = Table.Combine(AddedOrderKeys)
in
    Result
Run Code Online (Sandbox Code Playgroud)