Power BI – 在DAX中查找与最接近日期相关联的值

huangapple go评论59阅读模式
英文:

Power BI - find value associated with closest date in DAX

问题

我有两个表格在Power BI中:

我想要能够将表格2添加到表格1,其中价格最接近,并且日期在表格2中较早(参见下面的示例):

创建关系会导致问题,因为这是一个多对多的关系,所以我尝试使用DAX来解决。我可以创建另一个表格,但实际上我想将其添加为表格1中的计算列,而不是创建一个新表格。目前我正在使用以下代码创建新表格 - 有没有办法将其过渡到表格1中的计算列?

_new_table = var _prices = SELECTCOLUMNS('Table2',"Item",'Table2'[Item]&"", "Start Date", 'Table2'[StartDate], "Price", 'Table2'[Price])

var _sales = SELECTCOLUMNS('Table1',"Sale Date",'Table1'[SaleDate],"Item",'Table1'[Item]&"")

var _compare = ADDCOLUMNS(NATURALLEFTOUTERJOIN(_sales,_prices),"Difference",IF(DATEDIFF([StartDate],[Posting Date],DAY)<0,999999999,DATEDIFF([StartDate],[SaleDate],DAY)))

var _min_days = MINX(_compare, [Difference])

var _final = FILTER(_compare,[Difference]=_min_days || ISBLANK([Difference]))

return _final

英文:

I have two tables in Power BI:

Power BI – 在DAX中查找与最接近日期相关联的值

and

Power BI – 在DAX中查找与最接近日期相关联的值

I want to be able to be able to add Table 2 to Table 1 where the price is the closest, but earlier date from Table 2 (see below):

Power BI – 在DAX中查找与最接近日期相关联的值

Creating a relationship causes problems, as it is a many-many relationship, so instead I've been trying to do it using DAX. I can create another table, but I actually want to add it as a calculated column to table 1 rather than create a new table. Currently this is the code I am using to create a new table - any ideas on how to transition this to a calculated column in table 1?

<code> _new_table = var _prices = SELECTCOLUMNS('Table2',"Item",'Table2'[Item]&"","Start Date", 'Table2'[StartDate], "Price", 'Table2'[Price])

var _sales = SELECTCOLUMNS('Table1',"Sale Date",'Table1'[SaleDate],"Item",'Table1'[Item]&"")

var _compare = ADDCOLUMNS(NATURALLEFTOUTERJOIN(_sales,_prices),"Difference",IF(DATEDIFF([StartDate],[Posting Date],DAY)<0,999999999,DATEDIFF([StartDate],[SaleDate],DAY)))

var _min_days = MINX(_compare, [Difference])

var _final = FILTER(_compare,[Difference]=_min_days || ISBLANK([Difference]))

return _final</code>

答案1

得分: 1

这可以在PowerQuery或DAX中执行。如果您从支持查询折叠的数据源中提取数据(例如SQL Server),我更喜欢在PowerQuery中解决,但最终您可以尝试每种方法,看看哪种与您的数据更适合。

PowerQuery解决方案

  1. 首先,请确保在销售表上有一个主键。如果没有,请创建一个索引列。
  2. 然后,在项目列上执行左外连接(合并表)。
  3. 过滤左侧日期(销售表)大于或等于右侧日期(定价表)的行。
  4. 按右侧日期(定价表)升序对表进行排序,并缓冲您的表(Table.Buffer())。
  5. 基于主键或索引列删除重复项。

DAX解决方案

您也可以在DAX中执行基本相同的操作。在这种情况下,请将您的定价表连接到产品维度表,作为单独的事实表。

'Sales Fact Table' <- 'Product Dimension' -> 'Pricing Fact Table'

'Sales Fact Table'上创建以下列:

CALCULATE(
    LASTNONBLANKVALUE('Pricing Fact Table'[Date], 'Pricing Fact Table'[Price]),
    FILTER(
        RELATEDTABLE('Pricing Fact Table'),
        'Pricing Fact Table'[Date] <= 'Sales Fact Table'[Date]
    )
)

这是如何工作的:

RELATEDTABLE(...): 通过使用产品维度关系将定价事实表缩小到相关行。

'Pricing Fact Table'[Date] <= 'Sales Fact Table'[Date]: 销售时间之前或等于生效日期的定价。

LASTNONBLANKVALUE(...): 将定价事实表按生效日期排序,并返回最后一个价格值(受先前筛选的影响)。

英文:

This can be performed in PowerQuery or DAX. If you are pulling from a source that allows query folding (e.g., SQL Server), I prefer solving in PowerQuery, but ultimately you can try each and see which works better with your data.

PowerQuery Solution

  1. First, ensure you have a primary key on your sales table. If not, create an index column.
  2. Then, perform a left-outer join (Merge Tables) of your tables on the item column.
  3. Filter where the left date (sales table) is greater than or equal to the right date (pricing table).
  4. Sort the table by the right date (pricing table) ascending and buffer your table (Table.Buffer()).
  5. Remove duplicates based on your primary key(s) or index column.

DAX Solution

You can do essentially the same behavior in DAX as well. In this case, connect your pricing table to your product dimension table as a separate fact table.

&#39;Sales Fact Table&#39; &lt;- &#39;Product Dimension&#39; -&gt; &#39;Pricing Fact Table&#39;

Create the following column on the &#39;Sales Fact Table&#39;:

CALCULATE(
    LASTNONBLANKVALUE( &#39;Pricing Fact Table&#39;[Date], &#39;Pricing Fact Table&#39;[Price] ),
    FILTER(
        RELATEDTABLE( &#39;Pricing Fact Table&#39; ),
        &#39;Pricing Fact Table&#39;[Date] &lt;= &#39;Sales Fact Table&#39;[Date]
    )
)

Here's how this works:

RELATEDTABLE(...): Narrows the pricing fact table to the relevant rows by using the product dimension relationships.

&#39;Pricing Fact Table&#39;[Date] &lt;= &#39;Sales Fact Table&#39;[Date]: Pricing effective at or before time of sale.

LASTNONBLANKVALUE(...): Sorts the pricing fact table by the effective date and returns the last price value (subject to the prior filtering).

huangapple
  • 本文由 发表于 2023年6月6日 04:11:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76409698.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定