Forum Discussion

cathoms's avatar
cathoms
Icon for Responsive Resident rankResponsive Resident
1 year ago
Solved

Add index or rank number based on groups in Power Query

Hi! I'm helping someone with a Power BI report. We need to report the next 12 available appointment slots by slot length. Most slots are 15 minutes, but the end-user wants to group adjacent open 15 m...
  • danextian's avatar
    1 year ago

    Hi cathoms 

    You will need to use the Group By multiple times and create an index column within one of the tables created by grouping.

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjRT0lEyMNE3NNQ3MjAyVTA0sTIwAAoZmir4ZuaVlqQWK8XqRCsZG6EpM7YyMcVUZm5OkTJjA6KUmRAwDbsXyFdmiMVthmZoAWeJ1TRcyog0zYSgaYZYgheLMmNi3GZoiumHWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [PROV_ID = _t, SLOT_BEGIN_TIME = _t, SLOT_LENGTH_MINUTES = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"PROV_ID", Int64.Type}, {"SLOT_BEGIN_TIME", type datetime}, {"SLOT_LENGTH_MINUTES", type text}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"PROV_ID", "SLOT_BEGIN_TIME"}, {{"Group", each _, type table [PROV_ID=nullable number, SLOT_BEGIN_TIME=nullable datetime, SLOT_LENGTH_MINUTES=nullable text]}}),
        #"Grouped Rows1" = Table.Group(#"Grouped Rows", {"PROV_ID"}, {{"Group", each _, type table [PROV_ID=nullable number, SLOT_BEGIN_TIME=nullable datetime, Group=table]}}),
        #"Added Custom" = Table.AddColumn(#"Grouped Rows1", "Group With Index", each Table.AddIndexColumn([Group], "Index", 1, 1)),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Group"}),
        #"Expanded Group With Index" = Table.ExpandTableColumn(#"Removed Columns", "Group With Index", {"SLOT_BEGIN_TIME", "Group", "Index"}, {"SLOT_BEGIN_TIME", "Group", "Index"}),
        #"Expanded Group" = Table.ExpandTableColumn(#"Expanded Group With Index", "Group", {"SLOT_LENGTH_MINUTES"}, {"SLOT_LENGTH_MINUTES"}),
        #"Changed Type1" = Table.TransformColumnTypes(#"Expanded Group",{{"SLOT_BEGIN_TIME", type datetime}, {"SLOT_LENGTH_MINUTES", type text}, {"Index", Int64.Type}})
    in
        #"Changed Type1"