简体   繁体   English

具有Power View的DAX动态总计

[英]DAX Dynamic Total with Power View

I am attempting to graph, via Power View, the sum of all account balances at various points in time, so that I can see how the total is changing. 我试图通过Power View绘制不同时间点所有账户余额的总和,以便我可以看到总数是如何变化的。

I do not have transaction history (add / subtract), only 'Balance' at a specific point in time for each account. 我没有交易历史记录(加/减),只有每个帐户的特定时间点的“余额”。

A simplified example of my data is: 我的数据的简化示例是:

Account Date Balance
a111 01 January 2015 100
a111 01 February 2015 150
b222 01 March 2015 200
c333 01 March 2015 300
b222 01 May 2015 150
d444 01 June 2015 400

As far as I can tell, I need to create a measure to generate a dynamic rank per 'Account', ordered by latest date. 据我所知,我需要创建一个度量来生成每个“帐户”的动态排名,按最新日期排序。 I should then be able to create a second measure to SUM each account where the rank = 1. 然后,我应该能够创建第二个度量来SUM每个等级= 1的帐户。

There is a similar example of this issue discussed in the question PowerPivot DAX - Dynamic Ranking Per Group (Min Per Group) , however I cannot seem to get this to display how I want on a line graph in Power View. PowerPivot DAX问题中讨论了这个问题的类似例子- 每组动态排名(Min Per Group) ,但我似乎无法在Power View中显示我想要的线图。

What I want to display on the line graph is (letting the graph sort out the missing dates): 我想在折线图上显示的是(让图表整理缺少的日期):

Date Balance
01 Jan 2015 100 -- Account a111 only
01 Feb 2015 150 -- Account a111 only, but the new value
01 Mar 2015 650 -- Accounts a111 (latest), b222 and c333
01 May 2015 600 -- As above, but account b222 is updated
01 Jun 2015 1000 -- Latest values for all accounts

However, what I am currently seeing is: 但是,我目前看到的是:

Date Balance
01 Jan 2015 100 -- Sum of Balances matching this date
01 Feb 2015 150 -- As above
01 Mar 2015 500 -- As above
01 May 2015 150 -- As above
01 Jun 2015 400 -- As above

The problem, as far as I can tell, is that at each data point in the graph, the 'filter context' is reducing down to only the rows that match the date, whereas I need all rows on on before that date with a 'rank' of 1. 据我所知,问题在于,在图表中的每个数据点,“过滤器上下文”仅减少到与日期匹配的行,而我需要在该日期之前的所有行中使用'排名'1。

For reference, I am using Excel 2013 and the data is ultimately coming in via Power Query (pulling data from SharePoint), so I can work some magic there if necessary (ie generating a numeric 'group id' for use with DAX MAX() function, as that doesn't seem to like strings). 作为参考,我使用Excel 2013,数据最终通过Power Query(从SharePoint中提取数据)进入,所以如果有必要,我可以在那里工作(即生成一个数字'组ID'用于DAX MAX()功能,因为它似乎不喜欢字符串)。

I hope this is not to confusing, and thanks in advance for any help. 我希望这不要混淆,并提前感谢任何帮助。

The solution to this is to have a separate date dimension table so balance can be independently calculated on a per-month basis without respect to the fact table. 对此的解决方案是具有单独的日期维度表,因此可以在不考虑事实表的情况下每月独立地计算余额。

Let's assume you have a date table called Date. 假设您有一个名为Date的日期表。

So, first create the base measure 因此,首先创建基本度量

[Total Balance] := SUM( FactTable[Balance] )

Next we calculate the first month in the fact table to use as a floor for looking through our Date table values. 接下来,我们计算事实表中的第一个月,以用作查看Date表值的底线。

[First Ever Balance Date] := FIRSTDATE( ALL( FactTable[Date] ) )

And now we determine the last date with a balance for the date range between that first month and the current month in context: 现在我们在上下文中确定最后一个日期,其中包含第一个月和当前月份之间的日期范围:

[Last Balance Date] :=
LASTNONBLANK (
    DATESBETWEEN ( Date[Date], [First Ever Balance Date], MAX ( Date[Month] ) ),
    [Total Balance]
)

So take account a111 in the month of 5/1/2015 . 因此,请考虑2015年5月1日当月的a111 This measure will look in all the dates between 1/1/2015 (our [First Ever Balance Date] ) and 5/1/2015 ( the MAX Date[Month] ) , find the last row with a balance and return the corresponding Date[Date] ( 2/1/2015 ). 此度量将查看2015年1月1日 (我们的[First Ever Balance Date] )和2015年5 MAX Date[Month]MAX Date[Month] )之间的所有日期,查找余额的最后一行并返回相应的Date[Date]2015年2月1日 )。

This measure will return BLANK for any month before an account's initial balance. 该指标将在账户初始余额之前的任何月份返回BLANK。 Using BLANKS in date filtering doesn't work, so we should replace these BLANK values with the [First Ever Balance Date] to ensure the running balance for that account will still return 0. 在日期过滤中使用BLANKS不起作用,因此我们应该使用[First Ever Balance Date]替换这些BLANK值,以确保该帐户的运行余额仍将返回0。

[Last Balance Date Remove Blanks] :=
IF (
    ISBLANK ( [Last Balance Date] ),
    [First Ever Balance Date],
    [Last Balance Date]
)

Now we can calculate the current balance of any account for any month by looking up the balance as of the Last Balance Date Remove Blanks 现在,我们可以通过查看截至Last Balance Date Remove Blanks来计算任何月份的任何帐户的当前余额

[Last Balance] :=
CALCULATE (
    [Total Balance],
    DATESBETWEEN ( Date[Date], [Last Balance Date Remove Blanks], 
    [Last Balance Date Remove Blanks] )
)

And finally we use a SUMX across the accounts to add up their individual Last Balance amounts. 最后,我们在帐户中使用SUMX来累计各自的Last Balance金额。

[Account Total Balance] := SUMX( ALL( FactTable[Account] ), [Last Balance] )

Now in your pivot table you just put Date[Month] on the rows and [Account Total Balance] as the measure. 现在,您可以在数据透视表中将日期[月]放在行上,并将[帐户总余额]作为度量。 Note this will include months with no fact table rows like 4/1/2015 in your sample set. 请注意,这将包括样本集中没有事实表行(如4/1/2015)的月份。 If you wish to exclude these, add another intermediate measure to count your fact table rows: 如果要排除这些,请添加另一个中间度量以计算事实表行:

[Fact Table Row Count] := COUNTROWS ( FactTable )

And then create a measure to return BLANK() if [Fact Table Row Count] is BLANK(), and otherwise [Account Total Balance] 如果[Fact Table Row Count]为BLANK(),则创建一个返回BLANK()的度量,否则为[Account Total Balance]

[Account Balance Hide Rows] :=
IF (
    ISBLANK ( [Fact Table Row Count] ),
    BLANK (),
    [Account Balance]
)

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

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