Forum Discussion
ddorhout
4 years agoFrequent Visitor
Measure calculation with filter
Hello all,
I am having issues with some calculation metrics. I would like to calulate the amount of (distinct) drivers, and if a license lets them to stay within their own city. Various events (drives) are listed, and a user can have multiple events. But once a user went out of their city, then the user should be counted as "no" in column within the "own city". So the "yes" should not be taken into account in the calculation.
It should look like this, based on the dataset below:
| has license | no license | ||
| within city | Yes | 1 | 1 |
| No | 2 | 1 |
Example dataset here:
| Driver | Has License? | Own city |
| x | 0 | Yes |
| x | 0 | No |
| y | 0 | Yes |
| z | 1 | No |
| a | 1 | Yes |
| a | 1 | No |
| b | 1 | Yes |
| b | 1 | Yes |
So you should be able to use a measure like the following to do this
Measure = // loop over all the drivers in the current context SUMX( VALUES('Table'[Driver]) , // get the current value from the [Own City], if we are not filtering by [Own City] this will be blank var ownCity = SELECTEDVALUE('Table'[Own city]) // if [Own City] is blank or "No" we should count this as 1 driver return if (isblank(ownCity ) || ownCity = "No", 1, // if [Own City] <> "No" then we check if there are any other no values // for this driver and if there are any we return blank if( CALCULATE(countrows('Table'),'Table'[Own city] = "No") > 0, blank(),1) ))
1 Reply
- d_gosbellSuper User
So you should be able to use a measure like the following to do this
Measure = // loop over all the drivers in the current context SUMX( VALUES('Table'[Driver]) , // get the current value from the [Own City], if we are not filtering by [Own City] this will be blank var ownCity = SELECTEDVALUE('Table'[Own city]) // if [Own City] is blank or "No" we should count this as 1 driver return if (isblank(ownCity ) || ownCity = "No", 1, // if [Own City] <> "No" then we check if there are any other no values // for this driver and if there are any we return blank if( CALCULATE(countrows('Table'),'Table'[Own city] = "No") > 0, blank(),1) ))