简体   繁体   English

Excel PowerPivot DAX计算字段

[英]Excel PowerPivot DAX Calculated Field

I think I've got a relatively easy problem here on my hands, just having trouble getting it to work. 我认为我手头有一个相对简单的问题,只是无法正常工作。 Let me preface this by saying I'm new to DAX. 首先,我要说我是DAX的新手。

Consider the following PowerPivot Data Model : 考虑以下PowerPivot数据模型:

在此处输入图片说明

It consists of a Sales Records table, which is joined to a Date lookup table, as well as a "Daily Expenses" table. 它由一个Sales Records表和一个“ Daily Expenses”表组成,该表与日期查找表连接在一起。 The "Daily Expenses" table stores a single value which represents the averaged cost of running a specific trading location per day. “每日费用”表存储单个值,该值表示每天运行特定交易地点的平均成本。

It's extremely easy to produce a PivotTable that provides me with the total Sales Amount per store per [insert date period]. 生成数据透视表非常容易,该数据透视表可以为我提供每个[插入日期期间]每家商店的总销售额。 What I'm after is a DAX formulation that will calculate the profit per store per day - ie. 我需要的是DAX公式,该公式将计算每家商店每天的利润-即 Total Sales minus DailyExpenses (operating cost): 总销售收入减去每日费用(运营费用):

在此处输入图片说明

In theory, this should be pretty easy, but I'm confused about how to utilise DAX row context. 从理论上讲,这应该很容易,但是我对如何利用DAX行上下文感到困惑。

Failed attempts: 尝试失败:

Profit:=CALCULATE(SUM(SalesInformation[SaleAmount] - DailyStoreExpenses[DailyExpense])) 利润:= CALCULATE(SUM(SalesInformation [SaleAmount]-DailyStoreExpenses [DailyExpense]))

Profit:=CALCULATE(SUM(SalesInformation[SaleAmount] - DailyStoreExpenses[DailyExpense]), DailyStoreExpenses[StoreLocation]) 利润:= CALCULATE(SUM(SalesInformation [SaleAmount]-DailyStoreExpenses [DailyExpense]),DailyStoreExpenses [StoreLocation])

Profit:=SUM(SalesInformation[SaleAmount] - RELATED(DailyStoreExpenses[DailyExpense]) 利润:= SUM(SalesInformation [SaleAmount]-相关(DailyStoreExpenses [DailyExpense])

etc 等等

Any help appreciated. 任何帮助表示赞赏。

Zam, unfortunately your failed attempts are not close :-) 扎姆,很遗憾,您的失败尝试没有结束:-)

The answer, and best practice, is use a 4th table called 'Stores' which contains a unique record per store - not only is this useful for bringing together data from your two fact tables but it can contain additional info about the stores which you can use for alternative aggregations eg Format, Location etc. 答案和最佳实践是使用名为“商店”的第四个表,该表在每个商店中包含唯一的记录-这不仅有助于将两个事实表中的数据汇总在一起,而且还可以包含有关商店的其他信息,您可以用于替代聚合,例如格式,位置等。

You should create a relationship between each of the Sales and Expenses tables and the Store table and then use measures like: 您应该在每个“销售和费用”表与“商店”表之间创建一个关系,然后使用类似的措施:

[Sales] = SUM(SalesInformation[SaleAmount])
[Expenses] = SUM(DailyStoreExpenses[DailyExpense])
[Profit] = [Sales] - [Expenses]

Provided you have the Date and Store tables correctly linked to the two 'Fact' tables (ie Sales and Expenses) then the whole thing should line up nicely. 如果您已将Date和Store表正确链接到两个“事实”表(即Sales和Expenses),那么整个过程应该会很好地对齐。

Edit: 编辑:

If you want to roll this up into weeks, years etc. and you have no kind of relationship between expenses and the calendar then you'll need to adjust your expenses measure accordingly: 如果您要将其累加到几周,几年之内,并且支出与日历之间没有任何关系,则需要相应地调整支出度量:

[Expenses] = SUM(DailyStoreExpenses[DailyExpense]) * COUNTROWS(DateTable)

This will basically take the number of days in that particular filter context and multiply the expenses by it. 在特定的过滤条件下,这基本上需要花费几天的时间,然后再乘以费用。

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

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