DAX 公式使用额外条件查找第二个最小值

DAX formula to find second minimum with extra criteria

我有一个涉及两个 table 的问题,其中一个有一些成本(table 在价值处停止,我已经在我的明确衡量中输入了我想看到的期望值),一个按集群排序的城市。

我需要创建一个明确的度量,returns 给定集群和费用的第二个最小值。

费用Table

Date City Expense Value Expected Value
2020 Paris Electricity 1 1
2020 Berlin Electricity 1 1
2020 London Electricity 2 1
2020 New York Electricity 0 0
2020 Paris Heating 1 4
2020 Berlin Heating 4 4
2020 London Heating 12 4
2020 New York Heating 7 7

集群Table

City Cluster
Paris Europe
London Europe
Berlin Europe
New York America

我们来计算 [Europe] 集群中 [Electricity] 的预期值。我得到 1,因为两个最低值是 1。对于加热,我得到 4,因为第二个最小值是 4。 (为了举例,纽约 returns 为零,但集群不会只包含一个城市。)

目前,我有这段代码没有考虑费用类型和集群:

2ndMin:=minX(
        topN(countrows(ALL(Costs))-1;ALL(Costs);Costs[Value])
        ;Costs[Value])

知道我应该如何编辑代码以包含上面的两个规范吗?

谢谢!

这很棘手,因为最小值并列。解决方案是计算具有最小值的行,如果超过一个 return 最小值,否则倒数第二个

SecondMin =
VAR CurrentCluster =
    SUMMARIZE( 'Costs', 'Cluster'[Cluster] )
VAR CurrentExpense =
    VALUES( Costs[Expense] )
VAR CostsPerClusterAndExpense =
    CALCULATETABLE(
        Costs,
        CurrentCluster,
        CurrentExpense,
        REMOVEFILTERS( 'Cluster' ),
        REMOVEFILTERS( 'Costs' )
    )
VAR MinValue =
    MINX( CostsPerClusterAndExpense, Costs[Value] )
VAR Min2Value =
    MINX(
        FILTER( CostsPerClusterAndExpense, Costs[Value] > MinValue ),
        Costs[Value]
    )
VAR Result =
    IF(
        COUNTROWS( FILTER( CostsPerClusterAndExpense, Costs[Value] = MinValue ) ) > 1,
        MinValue,
        Min2Value
    )
RETURN
    IF( NOT ISEMPTY( Costs ), Result + 0 )