Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Dynamic Age Groups - DAX

Hi,

I am trying to represent the age demographic(with dynamic groups) across a workforce in DAX and I am having some trouble trying to represent it in the form of a graph. 

 

I have the following data:

  1. Date Table 
  2. Employee Info
    1. Hire date
    2. Date of Birth
    3. Termination Date

 

IDDate of BirthHire DateTermination Date
130/3/20023/4/2013 
230/5/19903/4/20145/3/2020

 

3. Support Age Table 

Age BracketSort OrderMinMax
Under 181117.999999999
18-2421824.9999999
25-3432534.999999999

 

I have tried using a calculated column but realised that the age needs to be calculated based on the date selected, as opposed to the today's date.

 

How would I use a DAX calculation to calculate the age of active employees and then to represent it in an easy to read bar graph, with each category representing an age bracket?

  • Anonymous's avatar
    Anonymous
    5 years ago

    Hi Anonymous ,

     

    Try this measure.

    Count =
    SWITCH (
        MAX ( 'Table (2)'[Age Bracket] ),
        "18-24",
            CALCULATE (
                COUNTROWS ( 'Table' ),
                FILTER (
                    'Table',
                    YEAR ( TODAY () ) - 24
                        <= YEAR ( [Date of Birth] )
                        && YEAR ( [Date of Birth] )
                            <= YEAR ( TODAY () ) - 18
                )
            ),
        "25-34",
            CALCULATE (
                COUNTROWS ( 'Table' ),
                FILTER (
                    'Table',
                    YEAR ( TODAY () ) - 34
                        <= YEAR ( [Date of Birth] )
                        && YEAR ( [Date of Birth] )
                            <= YEAR ( TODAY () ) - 25
                )
            ),
        "Under 18",
            CALCULATE (
                COUNTROWS ( 'Table' ),
                FILTER ( 'Table', YEAR ( TODAY () ) - 18 >= YEAR ( [Date of Birth] ) )
            )
    )
    

     

     

    Best Regards,

    Stephen Tao

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

3 Replies

  • Anonymous , New mewasure

     

    age =

    Var _1 = if(isbalnk(max(Table[Termination Date]) ,selectedvalue(Date[Date]), min(selectedvalue(Date[Date]),Table[Termination Date]))
    return
    datediff([Hire Date], _1,month)/12

     

    Create an independent sequence table

     

    New Table =  addcolumns(generateseries(1,10,1), "end", [Value]+.999999)

     

    Not this will give range , create measures help from this table

     

    refer how to do using my video : https://youtu.be/CuczXPj0N-k

     

    or

    https://www.daxpatterns.com/dynamic-segmentation/
    https://radacad.com/grouping-and-binning-step-towards-better-data-visualization

     

  • Hi, Anonymous 

    Please check the below picture and the sample pbix file's link down below, whether it is what you are looking for.

    all measures are in the sample pbix file.

     

     

    https://www.dropbox.com/s/w3sl3bhq3f8utzh/leont3.pbix?dl=0 

     

     

    Hi, My name is Jihwan Kim.

    If this post helps, then please consider accept it as the solution to help other members find it faster, and give a big thumbs up.

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Anonymous ,

     

    Try this measure.

    Count =
    SWITCH (
        MAX ( 'Table (2)'[Age Bracket] ),
        "18-24",
            CALCULATE (
                COUNTROWS ( 'Table' ),
                FILTER (
                    'Table',
                    YEAR ( TODAY () ) - 24
                        <= YEAR ( [Date of Birth] )
                        && YEAR ( [Date of Birth] )
                            <= YEAR ( TODAY () ) - 18
                )
            ),
        "25-34",
            CALCULATE (
                COUNTROWS ( 'Table' ),
                FILTER (
                    'Table',
                    YEAR ( TODAY () ) - 34
                        <= YEAR ( [Date of Birth] )
                        && YEAR ( [Date of Birth] )
                            <= YEAR ( TODAY () ) - 25
                )
            ),
        "Under 18",
            CALCULATE (
                COUNTROWS ( 'Table' ),
                FILTER ( 'Table', YEAR ( TODAY () ) - 18 >= YEAR ( [Date of Birth] ) )
            )
    )
    

     

     

    Best Regards,

    Stephen Tao

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.