问题
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