简体   繁体   English

Powerpivot DAX度量:将“ if then”相加

[英]Powerpivot DAX measure: sum “if then”

I am looking at some intercompany accounts receivables (AR) data. 我正在查看一些公司间应收帐款(AR)数据。 Let's assume, I have a couple companies that have transactions with each other. 假设,我有两家公司彼此进行交易。 All but one of those companies use the same accounting system where I can retrieve the data from. 除了其中一家公司以外,所有公司都使用相同的会计系统,从中可以检索数据。 For the odd one out company, I do not have access to AR data of intercompany transactions. 对于单打独斗的公司,我无法访问公司间交易的AR数据。 As a proxy, I take the accounts payables (AP) data from the other companies. 作为代理,我从其他公司获取应付账款(AP)数据。 My aim is to prepare a report showing the total AR data for all companies (built from the AR data of the of companies on the central accounting system and of the AP data that those companies have booked against the one company) 我的目的是准备一份报告,显示所有公司的总应收账款数据(根据中央会计系统上公司的应收账款数据以及这些公司针对一个公司预定的应付账款数据构建)

I now have three tables in Powerpivot, "table AR" holding AR data (company code, AR account numbers and the amount) and "table AP" holding accounts payable data and a "look-up table" holding the company codes, the respective AR codes and a column telling me wether the company is on the central accounting system or not (this column simply holds the information "direct" or "indirect" - reflecting the way I gather the data) 我现在在Powerpivot中有三个表,“表AR”保存AR数据(公司代码,AR帐号和金额),“表AP”保存应付账款数据,“查找表”保存公司代码,分别AR代码和一列告诉我公司是否在中央会计系统中(此列仅包含“直接”或“间接”信息-反映了我收集数据的方式)

Whenever the company code is linked to "direct" in the look-up table, my measure should sum up the amounts from the AR table filtered by column "company code". 只要公司代码在查找表中链接到“直接”,我的措施就应该汇总来自AR表的,按“公司代码”列过滤的金额。 When the company code is "indirect", the measure should sum up the amounts from the AP table filtered by column "AP number". 当公司代码为“间接”时,该度量应汇总按“ AP号”列过滤的AP表中的金额。

The idea would be to build a DAX measure that sums up the AR amounts along these lines: 这个想法是建立一个DAX度量,将这些金额的AR金额相加:

If company(in "look-up table") = "direct" then use the company code and sum all amounts in "table AR" when match in column "company code" of "table AR". 如果company(在“查找表”中)=“ direct”,则在“表AR”的“公司代码”列中匹配时,使用公司代码并汇总“表AR”中的所有金额。

Else if company("look-up table") = "indirect" then use AP-account-number from "look-up table" and sum all amounts in "table AP" when match column "AR account no." 否则,如果company(“ lookup-table”)=“ indirect”,则使用“ lookup table”中的AP帐号,并在匹配“ AR account no”列时将“ table AP”中的所有金额相加。

Maybe the picture below can clarify my idea a bit further. 也许下面的图片可以进一步阐明我的想法。

I'd be very grateful for some ideas how I can build a DAX measure for the above. 我非常感谢我能为上述内容建立DAX度量的一些想法。

Thanks a lot! 非常感谢!

在此处输入图片说明

This is a great use of PowerPivot and although there was something about your dummy data that confused me a little, this should answer your question. 这是PowerPivot的绝佳用途,尽管您的虚拟数据有些让我有些困惑,但这应该可以回答您的问题。

I'm assuming that your AR and AP tables have relationships to your lookup table on company code and AR acc no respectively. 我假设您的AR和AP表分别与company codeAR acc no上的查找表有关系。 This means that both columns must be unique in the real world! 这意味着这两个列在现实世界中必须是唯一的!

In theory you could write this as one mega measure but that isn't great practice - I'll break this down into 4 measures. 从理论上讲,您可以将其写为一个大尺度,但这不是一个好习惯-我将其分解为4个尺度。

Firstly a measure for summing the amounts in each of the AR and AP tables: 首先,要对每个AR和AP表中的金额求和:

[AR Amount] = SUM(AR[amount])
[AP Amount] = SUM(AP[amount])

This next measure does the IF portion and also deals with situations where the company code has more than one value (ie totals which would cause an error). 接下来的一项措施是处理IF部分,还处理公司代码具有多个值(即会导致错误的总计)的情况。 The key here is that you can't put a 'naked' column reference in a measure so you use VALUES on the access column: 这里的关键是您不能在度量中放置“裸”列引用,因此您在访问列上使用VALUES

[Raw Measure] =
               IF (
                   HASONEVALUE ( lookup[company code] ),
                   IF (
                       VALUES ( lookup[Access] ) = "Direct",
                       [AR Amount],
                       [AP Amount]
                       ),
                    BLANK ()
                   )

The problem here is your totals - maybe you care and maybe you don't but the measure above would make them blank! 这里的问题是您的总数-也许您在乎,也许您不在乎,但是上面的措施将使它们空白! The following measure iterates over the company codes and then sums up the values for the totals (would work for subtotals too if you have them): 以下度量将迭代公司代码,然后将总计的值相加(如果有小计,也将有用):

[Iterated Measure] = SUMX(VALUES(lookup[company code]), [Raw Measure])

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

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