PowerPivot Aggregated value based on groups

时光怂恿深爱的人放手 提交于 2019-12-13 04:35:16

问题


I have two date columns in one table

Coolumn Hears : Activity|City|Start_Date|End_Date

Row 1 : A1|C1|01/01/2014|05/01/2014

Row 2 : A1|C1|06/01/2014|07/01/2014

Row 3: A2|C2|06/01/2014|07/01/2014

Row 4: A3|C3|03/01/2014|04/01/2014

Expected output like - If user selects date range 02/01/2014 to 07/01/2014

Column Header

City | #StartCount| #EndCount

Row 1 : C1 | 1 | 2

Row 2 : C2 | 1 | 1

Row 2 : C3 | 1 | 1

Here #StartCount is - Need to grouped by City. - Its Count of Distinct Activity - It should consider date boundries as : All activity for which start date 'greater or equeal(>=)' Input Start date and (less than '<') End Date

Here #EndCount is - Need to grouped by City. - Its Count of Distinct Activity - It should consider date boundries as : All activity for which Ends date 'Less or equeal(<=)' Input End date and (Greater than '>') End Date

Could you please suggest me expression to be used for such case. Calculated measure or dax can be used..


回答1:


The key here is using disconnected slicers, because if you used the "regular" ones, then performing the COUNT calculations won't work because they simply filter out the rows that don't "fit".

I have used your data for creating 3 tables:

  • Source table - exactly the same as yours
  • Start table - containing single column called Start that includes the start dates from Source
  • End table - containing single column called End that includes the end dates from Source

Then I added two calculated fields:

Start Count

=CALCULATE(COUNTA(Source[City]),FILTER(Source,Source[Start]>=MIN('Start'[Start])))

End Count

=CALCULATE(COUNTA(Source[City]),FILTER(Source,Source[Start]<=MIN('End'[End])))

What these two commands do is that they count City rows that are not empty, and filter them by (using FILTER command) Start or End column. Read more about disconnected slicers here.

To get your report: create a new PowerPivot table, put City on rows and drag Start Count and End Count to values. Then add slicers for both Start and End tables. Selected your dates and you will get the results you need.

Attached is the screen of what I used (this will work in Excel 2013 as well as Excel 2010 with PowerPivot plugin):

Using disconnected slicer might be tricky sometimes -- but if you create them with newly available Excel Apps like CreateTimeDimension (for free) or pre-prepared DateTables, you should be just fine.

Hope this helps!



来源:https://stackoverflow.com/questions/22140988/powerpivot-aggregated-value-based-on-groups

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!