在将RANKX函数与模型中特定表的列组合时,我遇到了笛卡尔积问题。
我使用Power BI Desktop的2.53.4954.621版本连接到内部部署数据仓库。我的模型中有四个表,Sales,Region,Sales Goal和Calendar。
所有表都加入了Sales,因此我将为该实体提供相关的表结构。
Sales:
rowid
date
rep_id
region_id
sales
discount
company number
RepRegionKey (merging of rep_id and region_id)
以下是我的表格之间定义的关系
1. From Sales (date) to Calendar (Date), *:1, Single direction
2. From Sales (region_id) to Region (id), *:1, Both directions
3. From Sales (RepRegionKey) to Sales Goal (RepRegionKey), *:1, Both directions
我也在Sales表上定义了一些度量。我不确定哪些可能很重要,所以我会将它们全部列出来。
年初至今销售额 = CALCULATE(SUM('Sales'[sales]) - SUM('Sales'[discount]))
YE销售目标 =
CALCULATE(SUM('Sales Goal'[sales_goal]))
$ YTD目标与年初至今的销售额 =
CALCULATE([YTD Sales] - [YE Sales Goal])
年初至今的目标与年初至今的销售额 = DIVIDE([$ YTD Goal vs YTD Sales],[YE Sales Goal])
差异等级 = RANKX(ALL('Sales Goal'[rep_id]),CALCULATE([% YTD Goal vs YTD Sales]),,,DENSE)
当我将 Diff Rank 度量添加到包含' Region' [region_desc](或任何' Region'除了company_number之外的字段,表中只有一个值。
在下面的图片中观察region_desc字段如何与%YTD目标与YTD销售度量和平共享一个表(图中最左边的表),在该度量上构建RANX功能。此外,在中间表中,您会看到RANKX函数在没有region_desc字段的情况下正常工作。但是,在右侧的最终表中,组合RANKX和region_desc字段时会出现笛卡尔积。
我希望看到每个Rep的准确排名,如下:
我的关系是否不正确,或者我是否需要调整RANKX定义?
这些是我尝试过的定义,有不同程度的失败:
---没有REGION_DESC的工作
Diff Rank = RANKX(ALL('Sales Goal'[rep_id]),CALCULATE([% YTD Goal vs YTD Sales],ALLSELECTED('Region'[region_desc])),,,DENSE)
Diff Rank = RANKX(ALL('Sales Goal'[rep_id]),CALCULATE([% YTD Goal vs YTD Sales],ALL(Region[region_desc])),,,DENSE)
Diff Rank = RANKX(ALL('Sales Goal'[rep_id]),CALCULATE([% YTD Goal vs YTD Sales],FILTER(ALL(Region[region_desc]),[region_desc]<>"BUBBA")),,,DENSE)
Diff Rank = RANKX(ALL('Sales Goal'[rep_id]),CALCULATE([% YTD Goal vs YTD Sales],ALLEXCEPT('Region','Region'[region_desc])),,,DENSE)
---不使用REGION_DESC
Diff Rank = RANKX('Sales Goal',CALCULATE([% YTD Goal vs YTD Sales],ALLEXCEPT('Region','Region'[region_desc])),,,DENSE)
Diff Rank = RANKX('Sales Goal',CALCULATE([% YTD Goal vs YTD Sales],ALLSELECTED('Region'[region_desc])),,,DENSE)
Diff Rank = RANKX('Sales Goal',CALCULATE([% YTD Goal vs YTD Sales],ALL('Region'[region_desc])),,,DENSE)
Diff Rank = RANKX('Sales Goal',CALCULATE([% YTD Goal vs YTD Sales],FILTER(ALL(Region[region_desc]),[region_desc]<>"BUBBA")),,,DENSE)
Diff Rank = RANKX(DISTINCT('Sales Goal'[rep_id]),CALCULATE([% YTD Goal vs YTD Sales]),,,DENSE)
Diff Rank = RANKX(VALUES('Sales Goal'[rep_id]),CALCULATE([% YTD Goal vs YTD Sales]),,,DENSE)
提前谢谢!