Starting December 3, join live sessions with database experts and the Microsoft product team to learn just how easy it is to get started
Learn moreGet certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now
Hi,
I need to calculate the birthday of the people in my dataset from their social security number. This is a 10-digit number where the first 6 digits date of birth. for example, 051289XXXX would be 5th of December and (probably) 1989.
However because only 2 digits are used to describe year, I need to use the following 4 digits to determine century of birth.
This can be determined as follows
In excel I would do something like this. But how do is express this in PowerBi advanced editor?
Public Function CprToDato(cpr As String) As Date
Dim bytCent As Byte
Dim bytSevdig As Byte
Dim bytCprYear As String
If Not IsNull(cpr) Then
bytSevdig = Mid(cpr, 8, 1)
bytCprYear = Mid(cpr, 5, 2)
Select Case bytSevdig
Case 0 To 3
bytCent = 19
Case 4, 9
If bytCprYear <= 36 Then
bytCent = 20
Else
bytCent = 19
End If
Case 5 To 8
If bytCprYear <= 36 Then
bytCent = 20
ElseIf bytCprYear >= 58 Then
bytCent = 18
End If
End Select
CprToDato = Left(cpr, 2) & "-" & Mid(cpr, 3, 2) & "-" & bytCent & bytCprYear
End If
End Function
Thanks!
Solved! Go to Solution.
check this.
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjA1NLKwNI2IiFCKjQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"social security number" = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"social security number", type text}}),
#"Extract Day" = Table.AddColumn(#"Changed Type", "Day", each Number.FromText(Text.Start([social security number], 2)), type number),
#"Extract Month" = Table.AddColumn(#"Extract Day", "Month", each Number.FromText(Text.Middle([social security number], 2, 2)), type number),
#"Extract Year" = Table.AddColumn(#"Extract Month", "Year", each Number.FromText(Text.Middle([social security number], 4, 2)), type number),
#"7th" = Table.AddColumn(#"Extract Year", "7th", each Number.FromText(Text.Middle([social security number], 6, 1)), type number),
Century = Table.AddColumn(#"7th", "century", each if [7th] <= 3
then 1900
else if ([7th] = 4 or [7th] = 9) and [Year] <= 36
then 2000
else if ([7th] = 4 or [7th] = 9) and [Year] > 36
then 1900
else if ([7th] = 5 or [7th] = 😎 and [Year] <= 36
then 2000
else if ([7th] = 5 or [7th] = 😎 and [Year] >= 58
then 1800
else 0, type number),
Birthdate = Table.AddColumn(Century, "Birthdate", each #date([century]+[Year],[Month],[Day]), type date)
in
Birthdate
Marcus Wegener works as Full Stack Power BI Engineer at BI or DIE.
His mission is clear: "Get the most out of data, with Power BI."
twitter - LinkedIn - YouTube - website - podcast - Power BI Tutorials
check this.
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjA1NLKwNI2IiFCKjQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"social security number" = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"social security number", type text}}),
#"Extract Day" = Table.AddColumn(#"Changed Type", "Day", each Number.FromText(Text.Start([social security number], 2)), type number),
#"Extract Month" = Table.AddColumn(#"Extract Day", "Month", each Number.FromText(Text.Middle([social security number], 2, 2)), type number),
#"Extract Year" = Table.AddColumn(#"Extract Month", "Year", each Number.FromText(Text.Middle([social security number], 4, 2)), type number),
#"7th" = Table.AddColumn(#"Extract Year", "7th", each Number.FromText(Text.Middle([social security number], 6, 1)), type number),
Century = Table.AddColumn(#"7th", "century", each if [7th] <= 3
then 1900
else if ([7th] = 4 or [7th] = 9) and [Year] <= 36
then 2000
else if ([7th] = 4 or [7th] = 9) and [Year] > 36
then 1900
else if ([7th] = 5 or [7th] = 😎 and [Year] <= 36
then 2000
else if ([7th] = 5 or [7th] = 😎 and [Year] >= 58
then 1800
else 0, type number),
Birthdate = Table.AddColumn(Century, "Birthdate", each #date([century]+[Year],[Month],[Day]), type date)
in
Birthdate
Marcus Wegener works as Full Stack Power BI Engineer at BI or DIE.
His mission is clear: "Get the most out of data, with Power BI."
twitter - LinkedIn - YouTube - website - podcast - Power BI Tutorials
565-21-3304
Thank you! This solved my problem.
Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.
User | Count |
---|---|
87 | |
87 | |
84 | |
66 | |
49 |
User | Count |
---|---|
127 | |
109 | |
93 | |
70 | |
67 |