Forum Discussion
Dynamic Age Calculation Based on User Selected Cutoff Date
- 8 years ago
Hi Anonymous
Unfortunately as Yuliana mentioned, calculated columns don't respond to any filters within the report. Calculated columns are populated at report refresh, in an 'unfiltered' filter context.
For what you're trying to do, you would need to use something like a Dynamic Segmentation pattern (see DAX Patterns page).
- Create an 'Age' table containing all possible Ages you might want to filter on.
- Include the Age[Age] column in your visual.
- Create a Member Count By Age measure following a Dynamic Segmentation type pattern:
Member Count By Age = IF ( ISFILTERED ( Age[Age] ), VAR SelectedCutoff = MAX ( Cutoff[cutoff_date] ) RETURN CALCULATE ( COUNTROWS ( 'Member' ), FILTER ( VALUES ( 'Member'[date_of_birth] ), VAR AgeCalculated = IF ( 'Member'[date_of_birth] <= SelectedCutoff, TRUNC ( YEARFRAC ( 'Member'[date_of_birth], SelectedCutoff ) ) ) RETURN CONTAINS ( VALUES ( Age[Age] ), Age[Age], AgeCalculated ) ) ), COUNTROWS ( 'Member' ) )(I modified the pattern slightly)
Note that this measure will return a simple count of Members if no Ages are filtered on.
Also, ages are only computed if date_of_birth <= Selected cutoff_date.
Here is a sample pbix demonstrating this.
https://www.dropbox.com/s/5p1faipgycwz5zo/Age%20Dynamic%20Segmentation.pbix?dl=1
Regards,
Owen
Hi Anonymous,
I'm stumped because I understand that I'd need to use a column in order to be able to drop the value in either the Legend or Details fields (won't accept measures). I just don't understand how I'd create the calculated column that calculates their age based on the slicer selection.
To be clear, below is the desired visual, which right now is only showing their current age (based on date the report is run) and is NOT showing the age based on the cutoff_date slicer selection.
Please be aware of that calculated column returns static values since it is initialized. And it won't be dynamically changed based on slicer selection.
Regards,
Yuliana Gu