简体   繁体   English

在 Power BI 中以 M 语言中的 each 执行子查询

[英]performing a subquery in an each in M language in Power BI

I am converting SQL in various flavors into Power BI.我正在将各种风格的 SQL 转换为 Power BI。 I have the following query/code which I am trying to represent in the M syntax:我有以下查询/代码,我试图用 M 语法表示:

while($row = $result->fetch_assoc())
{
    $rate = SELECT CAD FROM ExchangeRate WHERE date<='" . $row['d'] . "' ORDER BY date DESC LIMIT 1
    $monthlySalesUSD += $daily/$rate;
}

Basically, it looks up the exchange rate for the date of a particular set of transactions, then divides the sales figures for that day by the exchange rate for that day.基本上,它查找一组特定交易日期的汇率,然后将当天的销售额除以当天的汇率。

In Power BI Advanced Query, I have the following excerpt defined:在 Power BI Advanced Query 中,我定义了以下摘录:

    #"Added Custom1" = Table.AddColumn(#"Changed Type", "SalesUSD", each [Inserted Rounding] / List.First(Table.Column(Table.First(Table.SelectRows(Table.Sort(warehouse_ExchangeRate,{{"date", Order.Descending}}),each [date] <= #"Changed Type"[DATELASTFULFILLMENT])),"CAD"))),

#"Changed Type" Contains the daily transaction totals and the dates. #"Changed Type"包含每日交易总额和日期。 The totals are in [Inserted Rounding] and the dates are in [DATELASTFULFILLMENT] .总数在[Inserted Rounding] ,日期在[DATELASTFULFILLMENT] So basically, for each row in #"Changed Type" , I want to get the date, look up the exchange rate, then divide the sales by that rate.所以基本上,对于#"Changed Type"每一行,我想获取日期,查找汇率,然后将销售额除以该汇率。 The piece I cannot seem to get is how to pull in the date from the current row in the each.我似乎无法得到的部分是如何从每个中的当前行中提取日期。

I found the answer thanks to MarcelBeug pointing me in the right direction.由于MarcelBeug 为我指明了正确的方向,我找到了答案。 I found this post which led me to the fact that you can do lambdas in an add column.我发现这篇文章让我认识到您可以在添加列中执行 lambda 表达式。 This gave me the following steps:这给了我以下步骤:

#"Fixed Join" = Table.AddColumn(#"Inserted Date", "ExchangeRate",
        (Earlier) => Table.SelectRows(warehouse_ExchangeRate,
                     each [date] <= Earlier[Date])),
#"Expanded ExchangeRate" = Table.ExpandTableColumn(#"Fixed Join", "ExchangeRate", {"CAD"}, {"CAD"})

This gives me the closest match that is less than the date of the sale, which I can then just divide into the sale to give me the USD value, then select the columns I need from that.这为我提供了小于销售日期的最接近匹配,然后我可以将其划分为销售以给出美元价值,然后从中选择我需要的列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM