Forum Discussion

QZ's avatar
QZ
Icon for Helper I rankHelper I
4 years ago
Solved

How to create new records base on row data(date format)?

In Power BI desktop, get original excel data as bellow:

Employee IDStart Date of BiztripEnd Date of Biztrip
A01Jul 6, 2022 12:00:00 AMJul 8, 2022 12:00:00 AM
A02Jul 4, 2022 12:00:00 AMJul 6, 2022 12:00:00 AM

Maybe after processed(DAX or M?), can I get bellow format data base on original excel file? If yes, how to do it?

Employee IDDate of BiztripDay of leave
A012022/7/61
A012022/7/71
A012022/7/81
A022022/7/41
A022022/7/51
A022022/7/61

If you've had any luck with this issue and can shed some light, it would by much appreciated.

  • Hi,

    This M code works

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcjQwVNJRMjDTNTDXNTIwMgJxLOCcWB2QCrCgCbIKhPLYWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Employee ID" = _t, #"Start Date of Biztrip" = _t, #"End Date of Biztrip" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Employee ID", type text}, {"Start Date of Biztrip", type date}, {"End Date of Biztrip", type date}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Date", each {Number.From([Start Date of Biztrip])..Number.From([End Date of Biztrip])}),
        #"Expanded Date" = Table.ExpandListColumn(#"Added Custom", "Date"),
        #"Changed Type1" = Table.TransformColumnTypes(#"Expanded Date",{{"Date", type date}}),
        #"Removed Columns" = Table.RemoveColumns(#"Changed Type1",{"Start Date of Biztrip", "End Date of Biztrip"})
    in
        #"Removed Columns"

    Hope this helps.

9 Replies

  • Hi,

    This M code works

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcjQwVNJRMjDTNTDXNTIwMgJxLOCcWB2QCrCgCbIKhPLYWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Employee ID" = _t, #"Start Date of Biztrip" = _t, #"End Date of Biztrip" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Employee ID", type text}, {"Start Date of Biztrip", type date}, {"End Date of Biztrip", type date}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Date", each {Number.From([Start Date of Biztrip])..Number.From([End Date of Biztrip])}),
        #"Expanded Date" = Table.ExpandListColumn(#"Added Custom", "Date"),
        #"Changed Type1" = Table.TransformColumnTypes(#"Expanded Date",{{"Date", type date}}),
        #"Removed Columns" = Table.RemoveColumns(#"Changed Type1",{"Start Date of Biztrip", "End Date of Biztrip"})
    in
        #"Removed Columns"

    Hope this helps.

  • Thank you for your rapid response!

    I'm new in M language, suppose the orininal table name is A, after processed base on table A, I want to create a new table B, may I know how to do it in M language?

    • Ashish_Mathur's avatar
      Ashish_Mathur
      Icon for Super User rankSuper User

      You are welcome.  Share the download link of your PBI file and i will share the M code in that file.

      • QZ's avatar
        QZ
        Icon for Helper I rankHelper I

        Put the pbix file to OneDrive, I'm not sure you can access or not, anyway, pls try it. Thanks.
        Click here