在 Power BI 中使用 IF 和 Values() 的错误合计
Wrong Totals Using IF and Values() in Power BI
我正在努力获取与上一年的销售额和年初至今的销售额相关的新销售额和损失的销售额。我试图在 table 中显示它,然后用年份切片器过滤 table。
以下是我使用的公式:
SalesPY = CALCULATE(SUM(SalesData[Value]),SAMEPERIODLASTYEAR('Calendar'[DateKey]))
SalesYTD = TOTALYTD(SUM(SalesData[Value]), 'Calendar'[DateKey])
NewSalesUppdate = SUMX(VALUES(SalesData[CustomerName]),IF([SalesYTD] > 0 && [SalesPY] = 0, [SalesYTD]))
LostSalesUppdate = SUMX(VALUES(SalesData[CustomerName]),IF([SalesYTD] = 0 && [SalesPY] > 0, -[SalesPY]))
LostSalesOld = IF([SalesPY] > 0 && [SalesYTD] = 0, -[SalesPY])
NewSalesUppdate
公式可以正常工作并正确总结。然而,LostSalesUppdate
不起作用,尽管与 NewSalesUppdate
相比,其公式几乎相反。似乎 IF
声明永远不会成为现实。这很奇怪,因为 LostSalesOld
公式显示了正确的值,但没有显示总数。
感谢所有提示!
示例数据:
当前结果:
请注意客户 A 年初至今没有销售额。 LostSalesOld 显示销售额为 -85000,但总计未反映任何内容。 LostSalesUppdate 什么都不显示。
想要的结果:
现在丢失的销售列之一(无论哪个)对客户 A 都有价值,总计
关注 LostSalesUppdate,问题是上下文之一。你的衡量标准是 "For each customer name in the SalesData table, show me last year's sales negated if they had sales last year and none this year".
问题(我承认它很微妙)是因为今年客户 A 没有销售,客户 A 没有就此度量而言, 在 SalesData table 中。因此,公式的其余部分将被忽略。
我建议添加一个单独的 table 和客户列表,类似于您的日期 table(每个客户一行)。然后,更新您的 LostSalesUppdate 公式,这样它就不会从 SalesData 中提取 CustomerName,而是从您的新客户 table.
中提取
LostSalesUppdate =
SUMX (
VALUES ( Customer[CustomerName] ),
IF ( [SalesYTD] = 0 && [SalesPY] > 0, - [SalesPY] )
)
我正在努力获取与上一年的销售额和年初至今的销售额相关的新销售额和损失的销售额。我试图在 table 中显示它,然后用年份切片器过滤 table。 以下是我使用的公式:
SalesPY = CALCULATE(SUM(SalesData[Value]),SAMEPERIODLASTYEAR('Calendar'[DateKey]))
SalesYTD = TOTALYTD(SUM(SalesData[Value]), 'Calendar'[DateKey])
NewSalesUppdate = SUMX(VALUES(SalesData[CustomerName]),IF([SalesYTD] > 0 && [SalesPY] = 0, [SalesYTD]))
LostSalesUppdate = SUMX(VALUES(SalesData[CustomerName]),IF([SalesYTD] = 0 && [SalesPY] > 0, -[SalesPY]))
LostSalesOld = IF([SalesPY] > 0 && [SalesYTD] = 0, -[SalesPY])
NewSalesUppdate
公式可以正常工作并正确总结。然而,LostSalesUppdate
不起作用,尽管与 NewSalesUppdate
相比,其公式几乎相反。似乎 IF
声明永远不会成为现实。这很奇怪,因为 LostSalesOld
公式显示了正确的值,但没有显示总数。
感谢所有提示!
示例数据:
当前结果:
想要的结果:
关注 LostSalesUppdate,问题是上下文之一。你的衡量标准是 "For each customer name in the SalesData table, show me last year's sales negated if they had sales last year and none this year".
问题(我承认它很微妙)是因为今年客户 A 没有销售,客户 A 没有就此度量而言, 在 SalesData table 中。因此,公式的其余部分将被忽略。
我建议添加一个单独的 table 和客户列表,类似于您的日期 table(每个客户一行)。然后,更新您的 LostSalesUppdate 公式,这样它就不会从 SalesData 中提取 CustomerName,而是从您的新客户 table.
中提取LostSalesUppdate =
SUMX (
VALUES ( Customer[CustomerName] ),
IF ( [SalesYTD] = 0 && [SalesPY] > 0, - [SalesPY] )
)