Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Calculating age and age grouping when data is polluted

Hi,

 

I have a dataset with the year of birth from customers and the transaction date.

The year of birth table is polluted since I got next to normal year of birth also 2, 560 and 2030 but also textual values.

Since we receive this data from other organizations and it is provided to me through live connection with a tabular model I can't correct or clean this data.

 

What I want to do is writing a measure that calculates the age of a customer on the moment the person did a transaction (to find out how many transaction (and average) are made by each age).

 

So I wrote the DAX query below. Only I have a few issues and some questions with that right now. Hopefully somebody can help we this.

1. In the calculating of the age I escape the textual values. But I do want to give them the age value "Onbekend". How do I do this?
2.  In the output I see that ages above 100 go to the category "Onbekend". How is this possible since my variable age_c isn't being called after the return?

3. I see some strange values in the output. 0000 converts to the age 0, 1907 to -1907, 1909 to -1909 (while 1908 gives me a correct value), 4 to -4, 5 tot -5 while 6, 7 and 8 give me the right age value again. How is this possible?

4. In my age categories everything goes like it should be, except for the age value 0 which is mapped to "Onbekend" instead of "0-4". 

5. Can this DAX query be written better/shorter e.g. by swapping if for switch?

 

Many thank!

 

Kind regards,

Hans

 

Age category =
VAR
Age = CALCULATE (
(YEAR(MAX(Fact_Transactie[TransactieDatum] ))) - MAX(DimLidHis[Geboortejaar]);
FILTER ( VALUES ( DimLidHis[Geboortejaar] ); NOT ( ISERROR ( DimLidHis[Geboortejaar] + 0 ) ) ) )
Var
Age_c = IF(
Age <= 100 && Age >= 0;
Age;
"Onbekend"
)
Return
IF(
Age >= 0 && Age < 4 && Age <> BLANK();
"0-4";
IF(
Age >= 4 && Age =< 10 && Age <> BLANK();
"4-10";
IF(
Age >= 11 && Age < 18 && Age <> BLANK();
"11-18";
IF(
Age >= 18 && Age <> BLANK() && ISNUMBER(Age);
"Volwassen";
"Onbekend"
)
)
)
)
1 ACCEPTED SOLUTION
v-cherch-msft
Employee
Employee

Hi @Anonymous 

 

I would suggest you create two measures.One measure is Age. The other measure is the Age category.If you need further help,please paste some data sample and expected output here or upload the .pbix file to OneDrive and post the link here. Do mask sensitive data before uploading.

How to Get Your Question Answered Quickly

 

Regards,

Cherie

Community Support Team _ Cherie Chen
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

2 REPLIES 2
v-cherch-msft
Employee
Employee

Hi @Anonymous 

 

I would suggest you create two measures.One measure is Age. The other measure is the Age category.If you need further help,please paste some data sample and expected output here or upload the .pbix file to OneDrive and post the link here. Do mask sensitive data before uploading.

How to Get Your Question Answered Quickly

 

Regards,

Cherie

Community Support Team _ Cherie Chen
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Anonymous
Not applicable

Hi,

 

In the DAX I already made 2 measures (one as a variable in the other).

Please see the examples below.

 

Customer_IDTransaction_Date

117-11-2018
718-11-2018
1519-11-2018
120-11-2018
120-11-2018
120-11-2018
421-11-2018
521-11-2018
621-11-2018
321-11-2018
321-11-2018
622-11-2018

 

Customer_IDYear of Birth

1O
2BA
32
4560
51890
61895
71900
81913
91931
101975
111990
121993
131998
142018
152028

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.