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
Artm
Frequent Visitor

Calculate dynamic change of age and age grouping

Hello everyone,

Its seems simple but I dont have a clue how to get it work.

I have a table with ID, Name, Birth Date, Age, Start Date, End Date data, and additional table like Age (numbers from 0-100), and Date (calendar).

table.JPG

 

 

 

 

 

 

 

 

 

What I need to get is the ages of only actual employees on a given month of given year.

For example in 2012 I have only one employee and I want to see only one employee in a table.

Plus I want a graph showing age range (>30, 30-40, 40+)

I got separate measures for this tasks but I cannot combine them to work not separatly but in connection.

 

Measure for age calculation:

Count By Age = 
IF (
    ISFILTERED ( Age[Age] ),
    VAR MaxDate =
        MAX ( 'Date'[Date] )
    RETURN
        CALCULATE (
            COUNTROWS ( HeadReport ),
            FILTER (
                VALUES ( HeadReport[Birth Date] ),
                VAR AgeCalculation =
                    IF (
                        HeadReport[Birth Date] <= MaxDate,
                        TRUNC ( YEARFRAC ( HeadReport[Birth Date], MaxDate ) )
                    )
                RETURN
                    CONTAINS ( VALUES ( Age[Age] ), Age[Age], AgeCalculation )
            )
        ),
    COUNTROWS ( HeadReport )
)

Measure for actual employees calculation:

Actual Employees = 
VAR MaxDate = MAX ( 'Date'[Date] )

VAR EmpCnt =
    CALCULATE (
        COUNTROWS (
            CALCULATETABLE ( Headreport, HeadReport[Start Date]<= MaxDate, ALL ( 'Date' ) )

                                ),
                              (ISBLANK ( HeadReport[End Date]) || HeadReport[End Date] > MaxDate)
                       )
RETURN
IF ( ISBLANK ( EmpCnt ), 0, EmpCnt)

Measure for Age range:

Age range = IF(HeadReport[Count By Age]>40,"40+",
            IF(HeadReport[Count By Age]>=30&&HeadReport[Count By Age]<=40,"30-40",
            "Less 30"))

Any help would be greatly appreciated.

 

Example pbix:

https://www.dropbox.com/s/8k1triyxisq9a93/Age%20calculation.pbix?dl=0

1 ACCEPTED SOLUTION

Hi @Artm ,

 

First of all I believe that for your calculations you don't need the additional columns and table for age, you can setup a table with the date ranges and add two measure to calculate the two fields you need.

 

  • Add the following disconnected table to your model:

 

ID Group Min Max
1 <30 0 29
2 30-40 30 40
3 40+ 41 999

 

  • Create the following two measures:
Number Employess by date = 
    COUNTROWS ( 
    FILTER (
        ALL ( HeadReport[ID]; HeadReport[Start Date]; HeadReport[End Date] );
        (HeadReport[Start Date] <= max ( 'Date'[Date] )
            && HeadReport[End Date] == BLANK ())  ||(HeadReport[Start Date] <= max ( 'Date'[Date] )
            && HeadReport[End Date] >= MAX ( 'Date'[Date] ))
    )
)
Age range Calculation = 
CALCULATE (
    COUNTROWS (
        FILTER (
            SUMMARIZE (
                ALL (
                    HeadReport[ID];
                    HeadReport[Start Date];
                    HeadReport[End Date];
                    HeadReport[Birth Date]
                );
                HeadReport[ID];
                HeadReport[Birth Date];
                HeadReport[Start Date];
                HeadReport[End Date];
                "@Age"; DATEDIFF ( HeadReport[Birth Date]; MAX ( 'Date'[Date] ); YEAR )
            );
            [@Age] <= max ( 'Age Periods'[Max] )
                && [@Age] >= MAX ( 'Age Periods'[Min] )
                && (
                    (
                        HeadReport[Start Date] <= max ( 'Date'[Date] )
                            && HeadReport[End Date] == BLANK ()
                    )
                        || (
                            HeadReport[Start Date] <= max ( 'Date'[Date] )
                                && HeadReport[End Date] >= MAX ( 'Date'[Date] )
                        )
                )
        )
    )
)

Check the result below (top table is your calculations down is using the two measure as you can see result is the same.

Untitled.png

See PBIX file attach (without all the additional columns/table that are not needed for the calculation) compared it with your file and please tell me if ti works).

Regards,

MFelix

 


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



View solution in original post

4 REPLIES 4
Anonymous
Not applicable

Thanks maguilarBI, but its not actually what I expected.

I updated a little bit my data (add a range column to Age table) and got the following:

All ranges.JPG

As you can see everything almost works but it shows me all employees or all ranges.

I cant find out the proper filter data to sort it.

 

Updated pbix:

https://www.dropbox.com/s/8k1triyxisq9a93/Age%20calculation.pbix?dl=0

Hi @Artm ,

 

First of all I believe that for your calculations you don't need the additional columns and table for age, you can setup a table with the date ranges and add two measure to calculate the two fields you need.

 

  • Add the following disconnected table to your model:

 

ID Group Min Max
1 <30 0 29
2 30-40 30 40
3 40+ 41 999

 

  • Create the following two measures:
Number Employess by date = 
    COUNTROWS ( 
    FILTER (
        ALL ( HeadReport[ID]; HeadReport[Start Date]; HeadReport[End Date] );
        (HeadReport[Start Date] <= max ( 'Date'[Date] )
            && HeadReport[End Date] == BLANK ())  ||(HeadReport[Start Date] <= max ( 'Date'[Date] )
            && HeadReport[End Date] >= MAX ( 'Date'[Date] ))
    )
)
Age range Calculation = 
CALCULATE (
    COUNTROWS (
        FILTER (
            SUMMARIZE (
                ALL (
                    HeadReport[ID];
                    HeadReport[Start Date];
                    HeadReport[End Date];
                    HeadReport[Birth Date]
                );
                HeadReport[ID];
                HeadReport[Birth Date];
                HeadReport[Start Date];
                HeadReport[End Date];
                "@Age"; DATEDIFF ( HeadReport[Birth Date]; MAX ( 'Date'[Date] ); YEAR )
            );
            [@Age] <= max ( 'Age Periods'[Max] )
                && [@Age] >= MAX ( 'Age Periods'[Min] )
                && (
                    (
                        HeadReport[Start Date] <= max ( 'Date'[Date] )
                            && HeadReport[End Date] == BLANK ()
                    )
                        || (
                            HeadReport[Start Date] <= max ( 'Date'[Date] )
                                && HeadReport[End Date] >= MAX ( 'Date'[Date] )
                        )
                )
        )
    )
)

Check the result below (top table is your calculations down is using the two measure as you can see result is the same.

Untitled.png

See PBIX file attach (without all the additional columns/table that are not needed for the calculation) compared it with your file and please tell me if ti works).

Regards,

MFelix

 


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



Artm
Frequent Visitor

Hi MFelix

Yes, thats what I actually need. Thanks for your help and explanation. Really appreciate it.

 

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.