Forum Discussion

tgjones43's avatar
tgjones43
Helper IV
7 years ago
Solved

Add Column query

Hi all

 

I have the following two columns and would like to create the third column, in the Query Editor. Each value in Column A occupies 1 or more rows. The values in Column B are random 8 digit numbers which are not necessarily in ascending numerical order (e.g. the value for row 2 is smaller than the value for row 1, however, where a value in Column A occupies more than one row (e.g. value 3), the corresponding values in column B are in ascending numerical order.

 

I assume there is probably a fairly simple solution to this, without using Column B. Thank you! 

 

Column AColumn BRequired Column
1217703561
2217703281
3217703251
3217703452
3217703463
4217703551
4217703572
5217703291
5217703302
5217703903
  • Hi tgjones43 ,

     

    We can insert index by catgoary by this way. Please refer to the M code as below.

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bc3LDcAwCAPQXXzOgU8ozSwo+6/RppWgqnJDTzaOAKNB2J3UjvtkzBaQRDkTtdA22BfKH9dPfbDXUNU/6Fm3GhqZLFTaJAe9Q/MC", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Column A" = _t, #"Column B" = _t, #"Required Column" = _t]),
        Partition = Table.Group(Source, {"Column A"}, {{"Partition", each Table.AddIndexColumn(_, "Index",1,1), type table}}),
        #"Expanded Partition" = Table.ExpandTableColumn(Partition, "Partition", {"Index"}, {"Partition.Index"})
    in
        #"Expanded Partition"

     

    Please find the pbix as attached.

     

    Regards,

    Frank

9 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    hello tgjones43 

    what is your third column "Required column " for. is it a calculated or data column.

     

    • tgjones43's avatar
      tgjones43
      Helper IV

      Hi Anonymous just a data column. I am going to merge it with another column that contains the word 'Survey', so that the column will say Survey 1, Survey 2, etc.

      • Anonymous's avatar
        Anonymous
        Not applicable

        tgjones43 

        so you want column b to be compleatly random? how did you enter in data initialy? 

         

        Basicaly what is happening is the data is asociation the column B values with the repeated numbers, you need something to distinguish the repeated numbers in column A i would try creating an index column see if that does anything . 

         

        best regards,

        Collin

  • v-frfei-msft's avatar
    v-frfei-msft
    Community Support

    Hi tgjones43 ,

     

    We can insert index by catgoary by this way. Please refer to the M code as below.

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bc3LDcAwCAPQXXzOgU8ozSwo+6/RppWgqnJDTzaOAKNB2J3UjvtkzBaQRDkTtdA22BfKH9dPfbDXUNU/6Fm3GhqZLFTaJAe9Q/MC", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Column A" = _t, #"Column B" = _t, #"Required Column" = _t]),
        Partition = Table.Group(Source, {"Column A"}, {{"Partition", each Table.AddIndexColumn(_, "Index",1,1), type table}}),
        #"Expanded Partition" = Table.ExpandTableColumn(Partition, "Partition", {"Index"}, {"Partition.Index"})
    in
        #"Expanded Partition"

     

    Please find the pbix as attached.

     

    Regards,

    Frank

    • tgjones43's avatar
      tgjones43
      Helper IV

      Thanks v-frfei-msft, that helps a lot.

       

      I wonder if you would be able to propose a solution to a slightly more complex indexing query.

       

      I have the following 2 columns and require the third. Each value in column A occupies several rows due to having several different values in column B. I want the required column to basically count each occurence of the word 'Purpose' in column B in order for each value of column A. And the final step of the M code needs to bring back all rows and columns (my dataset is much bigger than the example I am presenting). Is this possible?

       

      Thank you!

       

      Column AColumn BRequired Column
      1Altitude 
      1Slope 
      1Purpose1
      2Altitude 
      2Slope 
      2Purpose1
      2Purpose2
      3Altitude 
      3Slope 
      3Purpose1
      3Purpose2
      3Purpose3
      • tgjones43's avatar
        tgjones43
        Helper IV

        It might help that in my dataset there is a third column (Column C) that provides an 8 digit number for all occurences of the word 'Purpose' in Column B:

         

        Column AColumn BColumn CRequired Column
        1Altitudenullnull
        1Slopenullnull
        1Purpose217703561
        2Altitudenullnull
        2Slopenullnull
        2Purpose217703251
        2Purpose217703452
        3Altitudenullnull
        3Slopenullnull
        3Purpose217703291
        3Purpose217703302
        3Purpose217703903