COUNTROWS but only for the last date, ignoring filter context

GarC

New Member
Joined
Nov 9, 2011
Messages
15
I expect this is actually quite easy but I'm new to PowerPivot/PowerBI and am struggling a little.

Below is a simplified dataset of a table named Daily. The customer numbers are the same each day and each passes through stages on the way to being "Complete". When they are complete, they stay complete and will not be removed. The project is over when all Build Flags are complete.


Run DateCityCustomerNo.Build Flag
01/10/2016London1Not Started
01/10/2016London2Not Started
01/10/2016Paris3Not Started
01/10/2016Paris4Not Started
01/10/2016Paris5Not Started
02/10/2016London1Not Started
02/10/2016London2Not Started
02/10/2016Paris3In Progress
02/10/2016Paris4In Progress
02/10/2016Paris5In Progress
03/10/2016London1In Progress
03/10/2016London2In Progress
03/10/2016Paris3In Progress
03/10/2016Paris4In QC
03/10/2016Paris5In QC
04/10/2016London1In Progress
04/10/2016London2In QC
04/10/2016Paris3In QC
04/10/2016Paris4Completed
04/10/2016Paris5Completed

<tbody>
</tbody>

I have various reports which work fine but am having difficulty creating a table which contains a COUNTROWS but only for the last date in the model.
I want to ensure that the date does not respond to filter context which seems to be the problem.

COUNTROWSNot StartedIn ProgressIn QCCompleted
London0110
Paris0012

<tbody>
</tbody>

I feel I am very close but am inexperienced with FILTER() and ALL().

I have a few measures which I feel are not far off but none are quite right. A little guidance would be appreciated.

This is a convoluted way to just COUNTROWS and responds to filter context. I know it needs ALL() but haven't got it right yet.
Code:
CountRows_OnlyOnLastDate_v1 = CALCULATE(COUNTROWS(Daily),FILTER(Daily,Daily[run date]=LASTDATE(Daily[run date])))

This expression is returning all data. It doesn't respond to filter context (good) but isn't pre-filtering to the LASTDATE (bad).
Code:
CountRows_OnlyOnLastDate_v2 = CALCULATE(COUNTROWS(Daily),FILTER(ALL(Daily[Run Date]),Daily[Run Date]=LASTDATE(Daily[Run Date])))

This third attempt is also close. While it pulls the data for the last date, if there is no value for the last date, it looks for the next highest max date and brings that in. It also responds to filter context.
Code:
CountRows_OnlyOnLastDate_v3 = CALCULATE(COUNTROWS(Daily),FILTER(ALL(Daily[run date]),[run date]=MAX(Daily[run date])))

Any idea...
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Hi GarC,

I think you want a measure like this:
Code:
=
CALCULATE (
    COUNTROWS ( Daily ),
    CALCULATETABLE ( LASTDATE ( Daily[Run Date] ), ALL ( Daily ) )
)

  1. CALCULATETABLE ( LASTDATE ( Daily[Run Date] ), ALL ( Daily ) ) returns the last Run Date with all filters on the Daily table removed. LASTDATE returns a 1x1 table (rather than a scalar), so this can be used as a filter argument for the outer CALCULATE.
  2. The outer CALCULATE leaves all filters intact except Run Date (due to step 1 above).
  3. You may want to add +0 to the end of the measure if you prefer to see zeros rather than blanks when there are zero rows.

This produced the table you were looking for in a test model at my end. Hopefully it does the trick for you!

Owen :)
 
Upvote 0
Hi Owen. Thank you for the response. I will be home later and will try your solution.

Gar.
 
Upvote 0
Hi Owen

Thank you for your solution. It worked very well with my sample set and I look forward to testing at work on Monday.

I also added an additional filter condition which other users might find useful...

Rich (BB code):
Measure:= CALCULATE([CountRows],Daily[BuildFlag]="Build Complete",CALCULATETABLE(LASTDATE(Daily[run date]),ALL(Daily)))
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,720
Members
448,986
Latest member
andreguerra

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top