Forum Discussion

rodfernandez's avatar
rodfernandez
Helper I
4 years ago
Solved

Create new column based on 3 other columns

Hi everyone, First of all, sorry for my poor english ðŸ˜… I'm new to Power Query and I'm a bit stuck trying to create the following rule. I have the following table, with the first 4 columns and I...
  • edhans's avatar
    4 years ago

    Here you go rodfernandez 

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fZNNbsQwCIWvMsp6RuLHxA675BrRbNr736FAnGashkqQBfqEIe+x7xNOz2ldV/tSfSG8CAgfyAqkIlbFyPdzn+gfsnbyESRnpMVIlpyU5tVIJyUji4J08ug55yR7T450smakKHm/Eulky0nGTh6vLxk5K+K50VegCDlLZdgJU5mqlnou1dumQjXleq7V2VSqpoWHxTDVajn0jxG+DzZTC0FL6Wxvm8mFpATntL1tJphF8R8vkYFmilnIifYJMslQzNtWnSPjBHygbdscXS7U7aLx4se1YI4KDUcQXvuLVr+syzMHyhlqjZfB3OGgBGUZNCBJ0GLHxaNpab5lmwIew344keovy3CxoNQUxwOjlqMyXljseYcWiza4gOEWRR8WlkFaxgw1vaCjNsD7Bw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Row = _t, ID = _t, Time = _t, Task = _t, #"NEED THIS" = _t]),
        #"Changed Type with Locale" = Table.TransformColumnTypes(Source, {{"Time", type datetime}}, "en-BM"),
        #"Grouped Rows" = 
            Table.Group(
                #"Changed Type with Locale", 
                {"ID", "Task"}, 
                {
                    {"First Group Index", each Table.AddIndexColumn(_, "First Index", 1, 1, Int64.Type )},
                    {"All Rows", each _, type table [Row=nullable text, ID=nullable text, Time=nullable datetime, Task=nullable text, NEED THIS=nullable text]}}, GroupKind.Local),
        #"Grouped Rows1" = 
            Table.Group(
                #"Grouped Rows", 
                {"ID", "Task"}, 
                {
                    {"Second Group Index", each Table.AddIndexColumn(_, "Second Index", 1, 1, Int64.Type)}
                }
            ),
        #"Expanded Second Group Index" = Table.ExpandTableColumn(#"Grouped Rows1", "Second Group Index", {"First Group Index", "All Rows", "Second Index"}, {"First Group Index", "All Rows", "Second Index"}),
        #"Expanded First Group Index" = Table.ExpandTableColumn(#"Expanded Second Group Index", "First Group Index", {"Time", "NEED THIS", "First Index"}, {"Time", "NEED THIS", "First Index"}),
        #"Added Letter" = 
            Table.AddColumn(
                #"Expanded First Group Index", 
                "Letter", 
                each 
                    if [First Index] = 1  and [Second Index] = 1 then [Task]
                    else if [First Index] = 1 then [Task] &  Character.FromNumber(96+[Second Index]) 
                    else null,
                type text
                ),
        #"Removed Other Columns" = Table.SelectColumns(#"Added Letter",{"ID", "Task", "Time", "NEED THIS", "Letter"})
    in
        #"Removed Other Columns"

     

     

    It returns this. I used null. You could replace that with "" if you want, which is empty. 

    What I did was:

    1. Grouped by task and *D, but used GroupKind.Local so it would break the groups by the repeating tasks. So not all 2s were grouped together. Only Groups of 2s were.
    2. Then I grouped the entire thing again by the ID and task.
    3. Each grouping added an index, and preserved all rows.
    4. Then I expanded carefully the indexes and all rows.
    5. Then used a formula to find where the groups were 1 for first index and something for the second. If the first, just returned the task. For the 2nd and following, added b, c, d, etc.

    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.