Forum Discussion

KuntalSingh's avatar
KuntalSingh
Helper V
2 years ago
Solved

Need Help on Excel power query editor by using M code

I have two column Unique key and Reference Key 3

Reference key 3 colunm have some blank rows and need unique key colunm value where in reference key is blank by using M code.

 

Unique key                                             Reference Key 3

7595735GST/KNP/2022/003                 41204070
2798337127983371                              41154547
2798337127983371                              41154529
279917281408                                      41097136
27958936A-092                                    41063446
27959015MAU                                     41062015
279568921408                                     41059635
27550294VNSDO/04/23-24                40909663
27759530RTS/1254/21-22                  40856544
27659853LV/2122/UP/112                  40837604
28883364F20000024538
28883364F20000024544
28883336F20000022361
28883364F20000022650
28883364F20000024537
28883364F20000022894
28883364F20000024210
28883364F20000024211
28883364F20000022893
28883336F20000027768
28883336F20000027691
28883336F20000027769
28883336F20000027770
28883336F20000027692

  • BA_Pete's avatar
    BA_Pete
    2 years ago

     

    Ok. This code takes the highest [Reference ky 3] value to fill in the blanks:

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jZDLCsIwFER/Rbou9Wbueym4duG29P9/w9S0IJhaV4GcORMm8zzgcb88b1dEgjPKfg7jIGAJLjQs46/YCd5a9B0rmoCbUQo2lsnRY62WJioTCIzQUIRVoikm7CJ2nGmNlaBrC6vrcebUdmo2xcfbbjW3212y2qHsaIudrE4OQQSjYS9af4u7+P/LTpH0HPlytKzLlhc=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Unique Key" = _t, #"Reference key 3" = _t]),
        repBlankNull = Table.ReplaceValue(Source,"",null,Replacer.ReplaceValue,{"Reference key 3"}),
    
    //Relevant steps from here ------>
        groupRows = Table.Group(repBlankNull, {"Unique Key"}, {{"data", each _, type table [Unique Key=nullable text, Reference key 3=nullable text]}}),
        addNestedRef3 =
        Table.TransformColumns(
            groupRows,
            {
                "data",
                (x)=> Table.AddColumn(
                    x,
                    "RefKey3",
                    each if [Reference key 3] = null then List.Max(x[Reference key 3])
                    else [Reference key 3]
                )
            }
        ),
        expandData = Table.ExpandTableColumn(addNestedRef3, "data", {"Reference key 3", "RefKey3"}, {"Reference key 3", "RefKey3"})
    in
        expandData

     

    Summary:

    -1- groupRows = Group By [Unique Ref] and add an All Rows aggregated column called "data"

    -2- addNestedRef3 = Add a column to the nested table that gets the highest [Referece key 3] value if the cell is null

    -3- expandData = Expand the nested table columns back out again

     

    Example query output:

     

    Pete

13 Replies

  • Hi KuntalSingh ,

     

    You can create a new custom column like this:

    if [Reference Key 3] = null then [Unique Key] else [Reference Key 3]

     

    Pete

    • KuntalSingh's avatar
      KuntalSingh
      Helper V

      Hi Pete

      My ask is 

      I have two column Unique key and Reference Key 3

      Reference key 3 colunm have some blank rows and need reference key on the basis of lookup from colunm Unique key  by using M code. If i do it manuall in excel I used formula VLOOKUP([@[Unique_Key]],AA2:AD6546,4,0)

      where AA is Unique Key column and AD is Reference Key 3 column

       

      • BA_Pete's avatar
        BA_Pete
        Super User

         

        Can you provide a copyable example of all the tables that you're referring to please?

        It sounds like you have two separate tables but you've only provided an example of one of them.

        You can paste your examples directly from Excel into a reply window here to ensure formatting stays intact.

         

        Pete