从两个不同的表计算 where 类型子句

Calculate where type clause from two different tables

我已经有一段时间没有写 DAX 了,我很难把它放在一起,我希望有人能提出建议。

我有:

数量Table(比一月份多了很多个月):

+----------+-----------+----------+
| Location |   Date    | LaborQty |
+----------+-----------+----------+
| NY       | 1/3/2017  | 41.024   |
| NY       | 1/4/2017  | 33.836   |
| NY       | 1/5/2017  | 20.431   |
| NY       | 1/6/2017  | 35.544   |
| NY       | 1/7/2017  | 0        |
| NY       | 1/9/2017  | 33.337   |
| NY       | 1/10/2017 | 41.799   |
| NY       | 1/11/2017 | 70.469   |
| NY       | 1/12/2017 | 35.514   |
| NY       | 1/13/2017 | 31.573   |
| NY       | 1/15/2017 | 0        |
| NY       | 1/16/2017 | 22.041   |
| NY       | 1/17/2017 | 30.518   |
| NY       | 1/18/2017 | 47.576   |
| NY       | 1/19/2017 | 29.53    |
| NY       | 1/20/2017 | 18.155   |
| NY       | 1/21/2017 | 0        |
| NY       | 1/23/2017 | 31.284   |
| NY       | 1/24/2017 | 27.695   |
| NY       | 1/25/2017 | 38.907   |
| NY       | 1/26/2017 | 16.289   |
| NY       | 1/27/2017 | 30.976   |
| NY       | 1/28/2017 | 0        |
| NY       | 1/30/2017 | 21.434   |
| NY       | 1/31/2017 | 16.49    |
+----------+-----------+----------+...etc

评分Table:

+----------+-----------+------------+-----------+---------+-----------+--------+
| Location | DateFrom  |   DateTo   | MonthFrom | MonthTo | RateType  | Amount |
+----------+-----------+------------+-----------+---------+-----------+--------+
| NY       | 1/1/2017  | 6/30/2017  |         1 |       6 | LaborRate | 129.7  |
| NY       | 7/1/2017  | 9/30/2017  |         7 |       9 | LaborRate | 129.8  |
| NY       | 10/1/2017 | 12/31/2017 |        10 |      12 | LaborRate | 129.9  |
| DC       | 1/1/2017  | 6/30/2017  |         1 |       6 | LaborRate | 130.1  |
| DC       | 7/1/2017  | 9/30/2017  |         7 |       9 | LaborRate | 130.5  |
| DC       | 10/1/2017 | 12/31/2017 |        10 |      12 | LaborRate | 130.7  |
+----------+-----------+------------+-----------+---------+-----------+--------+

当月期望的输出类型 (例如 LaborQty x LaborRate)

+-------+----------+-----------+------------+
| Month | LaborQty | LaborRate |   Result   |
+-------+----------+-----------+------------+
|     1 | 674.22   | 129.74    | 87473.3    |
|     2 | 350      | 129.74    | 45409      |
|     3 | 375      | 129.74    | 48652.5    |
|     4 | 400      | 129.74    | 51896      |
|     5 | 380      | 129.74    | 49301.2    |
|     6 | 500      | 129.74    | 64870      |
|     7 | 550      | 129.76    | 71368      |
|     8 | 600      | 129.76    | 77856      |
|     9 | 675      | 129.76    | 87588      |
|    10 | 700      | 129.98    | 90986      |
|    11 | 780      | 129.98    | 101384.4   |
+-------+----------+-----------+------------+

我要写的内容:

一种 DAX 度量,将输出类似于结果列中显示的金额。如果我在哪里编写一个 linq 查询来选择正确的汇率,它将看起来像这样:

LaborRate = db.Rates
            .Where(a => a.DateFrom <= SelectedDate & a.DateTo >= SelectedDate & a.RateType == "LaborRate")
            .Select(a => a.Amount).Sum();

我尝试了 CALCULATE、SUM、SUMX、FILTER、RELATED 的组合,但我无法让它工作。任何建议将不胜感激。最简单的方法是什么?

我通过执行以下操作实现了这一目标。

  1. Month = MONTH(Qty[Date]) 作为计算列添加到您的 Qty table。
  2. Qty table 中创建一个 TotalLaborQty 度量作为 SUM(Qty[LaborQty])
  3. Rates table 中定义一个 LaborRate 度量,详情如下。
  4. Result 度量定义为 [TotalLaborQty] * [LaborRate]
  5. 将它们设置在矩阵中,行中有 Qty[Location]Qty[Month],三个度量作为值。

LaborRate =
    VAR SelectedMonth = SELECTEDVALUE(Qty[Month])
    VAR SelectedLocation = SELECTEDVALUE(Qty[Location])
    RETURN CALCULATE(SUM(Rates[Amount]),
            FILTER(ALL(Rates),
                Rates[MonthFrom] <= SelectedMonth &&
                Rates[MonthTo] >= SelectedMonth &&
                Rates[Location] = SelectedLocation))