Forum Discussion
How to make distinct count value with filter as constant
- 4 years ago
Hey Georgia_H ,
I changed the denominator and the division measure in my example. Please be aware that I only used two metrics inside both measures to create a virtual table.The denominator now is more or less a constant:
denominator = var metricsToCount = {"PCM01" , "PCM23"} var metricsCount = COUNTROWS( metricsToCount ) return metricsCountYou might expand the definition of the denominator like so
denominator check = var metricsToCount = {"PCM01" , "PCM23"} var metricsCount = COUNTROWS( metricsToCount ) return IF( FIRSTNONBLANK( 'Table'[Metrics_ID] , 'Table'[Metrics_ID] ) in metricsToCount , metricsCount , BLANK() )
The division measure now looks like this:division = var metricsToCount = {"PCM01" , "PCM23"} var metricsCount = COUNTROWS( metricsToCount ) return AVERAGEX( 'Table' , if( FIRSTNONBLANK( 'Table'[Metrics_ID] , 'Table'[Metrics_ID] ) in metricsToCount , DIVIDE( 'Table'[Avg Compliance %] , metricsCount ) , BLANK() ) ) * 100The table visual looks like this:
Of course, it's possible to create a table from the 12 metrics, then you do not have to repeat the definition in both measures, but if they are just used in two measures I would probably use the measure definition.
As the metric is used inside your table, it will contribute to the current filter context, but the current filter context will not be expanded, for this reason the DISTINCTCOUNT will return 1.
Hopefully, this will help to tackle your challengeRegards,
Tom
Hey Georgia_H ,
I changed the denominator and the division measure in my example. Please be aware that I only used two metrics inside both measures to create a virtual table.
The denominator now is more or less a constant:
denominator =
var metricsToCount = {"PCM01" , "PCM23"}
var metricsCount = COUNTROWS( metricsToCount )
return metricsCount
You might expand the definition of the denominator like so
denominator check =
var metricsToCount = {"PCM01" , "PCM23"}
var metricsCount = COUNTROWS( metricsToCount )
return
IF( FIRSTNONBLANK( 'Table'[Metrics_ID] , 'Table'[Metrics_ID] ) in metricsToCount
, metricsCount
, BLANK()
)
The division measure now looks like this:
division =
var metricsToCount = {"PCM01" , "PCM23"}
var metricsCount = COUNTROWS( metricsToCount )
return
AVERAGEX(
'Table'
, if( FIRSTNONBLANK( 'Table'[Metrics_ID] , 'Table'[Metrics_ID] ) in metricsToCount
, DIVIDE( 'Table'[Avg Compliance %] , metricsCount )
, BLANK()
)
) * 100
The table visual looks like this:
Of course, it's possible to create a table from the 12 metrics, then you do not have to repeat the definition in both measures, but if they are just used in two measures I would probably use the measure definition.
As the metric is used inside your table, it will contribute to the current filter context, but the current filter context will not be expanded, for this reason the DISTINCTCOUNT will return 1.
Hopefully, this will help to tackle your challenge
Regards,
Tom
- Georgia_H4 years ago
Helper I
Hi TomMartens
Just a quick question, instead of hardcoding the list of Metrics in the var below, possible to check through the list of Metrics_ID that present in the table? Reason being is that some new Metrics_ID may not have data for any particular period hence, they should not be counted as 1 as my constant denominator. With this syntax, i am always getting the count of Metrics_ID hardcoded. Thank you.
var metricsToCount = {"PCM01" , "PCM23"}- TomMartens4 years ago
Super User
Hey Georgia_H ,
It's difficult to provide a solution that suits your needs if you do not describe your requirements completely. Make sure that the sample data you provide represents your data model (tables, relationships, calculated columns, and measures). Consider creating a pbix file that contains sample data, upload the pbix file to onedrive or dropbox and share the link. if you are using Excel to create the sample data instead of the manual input method share the xlsx as well.
Starting with the question on how to have- DISTiNCTCOUNT overcome the current filter context,
- to there are metrics available that should not be considered and now
- if metrics that should be considered have no values throughout any period (there is no period in your sample data) these metrics should be removed from the table.
It's not a good idea to change the direction of the initial question as this can confuse other users as these users will not consider this thread to find an answer to their problem.
It's better to start a new thread instead.
The general approach to finding a solution for your problem might look like this (I'm pretty sure that I miss important information):- define a table with all possible metrics (maybe a dimension table in your star schema data model)
- reduce the table by removing metrics based on rules like "a metric that has no values in any period" has to be removed, maybe this rule will transform to no value in the current year
- use the found table to count the denominator and inside the division.
In regards to your remark that you will get the count of metrics hardcoded if you used this approach:
var metricsToCount = {"PCM01" , "PCM23"}At the time of providing my solution I was inspired by your approach from further down this thread without knowing that metrics might be excluded due to business rules:
,,, in {"PCM01","PCM03","PCM04","PCM09","PCM10","PCM19","PCM20","PCM23","PCM33","PCM34","PCM35","PCM36"} ...Regards,
Tom