DAX公式以其他条件找到第二个最小值 [英] DAX formula to find second minimum with extra criteria

查看:82
本文介绍了DAX公式以其他条件找到第二个最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,涉及两个表,一个表具有一些成本(表停止在价值上,我已在期望值中输入了我希望在我的显式度量中看到的值),一个问题是按集群对城市进行排序.

I have a problem that involves two tables, one with some costs (table stops at value, I have put in Expected Value what I want to see in my explicit measure), one with cities sorted by clusters.

我需要创建一个显式度量,以返回给定聚类和费用的第二个最小值.

I need to create an explicit measure that returns the second minimum value for a given cluster and Expense.

费用表

<身体>
日期城市费用期望值
2020 巴黎 1 1
2020 柏林 1 1
2020 伦敦 2 1
2020 纽约 0 0
2020 巴黎加热 1 4
2020 柏林加热 4 4
2020 伦敦加热 12 4
2020 纽约加热 7 7

集群表

<身体>
城市集群
巴黎欧洲
伦敦欧洲
柏林欧洲
纽约美国

让我们以[欧洲]群集中[电气]的期望值为例.我得到1,因为两个最低值是1.对于加热,我得到了4,因为第二个最小值是4.(出于示例的目的,纽约返回零,但集群将仅包含一个城市.)

Let's take the expected value for [Electricity] in [Europe] Cluster. I get a 1 as two lowest values are 1. For Heating, I get a 4 as second minimum value is a 4. (New York returns zero for the purpose of example but clusters will not contain only one city.)

现在,我有这段代码没有考虑Expense类型和Cluster:

For now, I have this bit of code which doesn't take into account the Expense type and Cluster :

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

有什么主意我应该如何编辑代码以包含上面的两个规范?

Any idea how I should edit the code to include the two specifications above ?

谢谢!

推荐答案

这很棘手,因为与最小值有关.解决方案是对具有最小值的行进行计数,如果多于一个返回最小值,否则将第二个变为最小值

This is tricky, because of ties for the min value. The solution is to count the rows with the minimum value and if more than one return the minimum, else the second to minimum

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 )

这篇关于DAX公式以其他条件找到第二个最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆