Forum Discussion

Anon29's avatar
Anon29
Helper II
2 years ago
Solved

Group By needing help

Hi all, this has been doing my head in so hoping someone can help.   The data below is an example of one phone conversation and the order of the Agents talking is in order of the contact from each ...
  • edhans's avatar
    2 years ago

    Try this code Anon29 

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("lZM9D4MgEIb/imE2kQNEHUuHDk2njsYBGweTph+mDv33Reyg9a6B5SDk4fLe3Xt1zUSV8TITXEiWsr19JYd+uLqrbS/g3xSAixXnrEmDcOVisoXP46MbNrj22YXKwz+IuPwSx3HtOgYucNg4+GRvS7b0VYayU4E6R/qN0kS7UZYob/ccbWLu7yUMQGkmaGIsBD0dUofWWMZ0muPwsVuBIL/OQzSQViW8hOv469MfHOY9UNiWGTu0fbeiC9pPCDsJkcGZI/ZFzB7xc2w+", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Agent = _t, #"Talk ID" = _t, Talkorder = _t, #"Stat (seconds)" = _t]),
        #"Changed Type with Locale" = Table.TransformColumnTypes(Source, {{"Date", type date}}, "en-BS"),
        #"Changed Type" = Table.TransformColumnTypes(#"Changed Type with Locale",{{"Stat (seconds)", Int64.Type}}),
        #"Added TalkOrder Group" = Table.AddColumn(#"Changed Type", "TalkOrder Group", each Text.Start([Talkorder], 2), type text),
        #"Grouped Rows" = 
            Table.Group(
                #"Added TalkOrder Group", 
                {"Date", "Agent", "Talk ID", "TalkOrder Group"}, 
                {
                    {"Stat (Seconds)", each List.Sum([#"Stat (seconds)"]), type nullable number}, 
                    {
                        "TalkOrder ID", 
                        each 
                          let
                            varTalkOrderLen = Text.Length(_[Talkorder]{0}) -1
                          in
                          Text.Start(_[Talkorder]{0}, varTalkOrderLen)
                    }
               }
            ),
        #"Removed Other Columns" = Table.SelectColumns(#"Grouped Rows",{"Date", "Agent", "Talk ID", "TalkOrder ID", "Stat (Seconds)"}),
        #"Changed Type1" = Table.TransformColumnTypes(#"Removed Other Columns",{{"TalkOrder ID", type text}, {"Stat (Seconds)", Int64.Type}})
    in
        #"Changed Type1"

     

    It returns this:


    What the code does:

    1. I added a group ID that is just the first two characters to it groups by 41, 61, etc.
    2. Within the group, I find the length of the first Talk Order ID for that Talkorder ID grouping and subtract 1.
    3. I then truncate the talkorder ID by that length

    How to use M code provided in a blank query:
    1) In Power Query, select New Source, then Blank Query
    2) On the Home ribbon, select "Advanced Editor" button
    3) Remove everything you see, then paste the M code I've given you in that box.
    4) Press Done
    5) See this article if you need help using this M code in your model.