Forum Discussion

cfindling's avatar
cfindling
Regular Visitor
4 years ago
Solved

Age on a specific date from DOB

I have a vaccine table that has a vaccine administered date, then I have a person table that has the person's date of birth.  I need to know the age of my patient on the date the vaccine was administ...
  • Anonymous's avatar
    Anonymous
    4 years ago

    Hi cfindling ,

     

    I agreed with AlexisOlson 's suggestion to use DATEDIFF() to calculate the Age:

    Measure = DATEDIFF(MAX('person'[date of birth]),MAX('vaccine'[vaccine administered date]),YEAR)
    Column = 
    var _vaccine=LOOKUPVALUE('vaccine'[vaccine administered date],[person ],[person])
    return DATEDIFF([date of birth],_vaccine,YEAR)

     

    If it does not work, you may try to do it in Power Query, below is the whole M syntax:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Hcu5CQAxDATAXjY2rLUnfCi8pwuh/tuwUTowmXgwYBGLJupCjcTbdDudUsvX4kGblDf9hzSneOZC1QY=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [person = _t, #"date of birth" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"person", type text}, {"date of birth", type date}}),
        #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"person"}, vaccine, {"person "}, "vaccine", JoinKind.LeftOuter),
        #"Expanded vaccine" = Table.ExpandTableColumn(#"Merged Queries", "vaccine", {"vaccine administered date"}, {"vaccine administered date"}),
        #"Inserted Age" = Table.AddColumn(#"Expanded vaccine", "Age", each [vaccine administered date] - [date of birth]),
        #"Calculated Total Years" = Table.TransformColumns(#"Inserted Age",{{"Age", each Duration.TotalDays(_) / 365, type number}}),
        #"Rounded Down" = Table.TransformColumns(#"Calculated Total Years",{{"Age", Number.RoundDown, Int64.Type}})
    in
        #"Rounded Down"

     

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