Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
KuntalSingh
Helper V
Helper V

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

1 ACCEPTED SOLUTION

 

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:

BA_Pete_0-1694687349222.png

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




View solution in original post

13 REPLIES 13
BA_Pete
Super User
Super User

Hi @KuntalSingh ,

 

You can create a new custom column like this:

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

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




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

 

 

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



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




I have one table only having multipal column. In Column Reference key 3.1 some Rows are blanks. Need the to lookup values from Unique colunm by using Vlookup and get the values in Reference key 3.1 column 

 

Unique_Key Reference Key 1 Reference Key 2 Reference key 3.1
27689899. 41535261
27689899. 41535261
27585136. 41535253
28316334D138048 41535226
27930964. 41535195
27930964. 41535174
27930964. 41534497
27930802QAUP2223/05466 41524563
278679712022-23/17/CR 41516145
28376909JMV/16/2022-23 41508140
2830830416 41488782
28303756UP2223351 41430718
2825252322-23/32 41382471
275998431060397907 41354750
28092499IOC/VA/011 41244021
27595735GST/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

 

It sounds like you want to fill in any blank values in the [Reference Key 3.1] column with the value from the [Unique Key] column, but that is exactly what my previous solution did, so why wasn't that what you needed?

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




No,

Lookup unique_key to return the reference key 3 where the ref key are blank.

If we do it manually used Vlookup formula: =Vlookup (Unique_Key,Rance( Unique_Key & reference key3 ) Column number in the range containing the return value, 0).

Hope you understand my ask

 

 

Ok, let's break this down.

You have [Unique Key] which has a value in every row.

You have [Ref Key 3] which has some missing values.

You want to fill in the blanks in [Ref Key 3] using another column within the same table.

What is the name of the column that you want to get the values from to fill in the blank [Ref Key 3] values?

 

Assume that I have no idea what VLOOKUP does or how it works.

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




To get the values from Ref key 3, by looking up the unique key in the column. In other words to find the ref key 3 that matches with the unique key of the blank ref key 3

 

Ah, ok. I think I understand now.

How do you want to assign a [Ref 3] value to a blank space when a single [Unique Key] can have multiple [Ref 3] values? Which [Ref 3] value do you want to put in the blank?

 

BA_Pete_1-1694684305912.pngBA_Pete_2-1694684398944.png

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




any one reference key can work

 

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:

BA_Pete_0-1694687349222.png

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




Thanks! Pete

 

Sample Data,

Unique KeyReference Key 1Reference Key 2Reference key 3
2ND RA/2892398128923981  42348310
2ND RA/2892398128923981  42348313
2ND RA/2892398128923981  42348315
00328980146  42348471
NMPPL-SIK/OFF/RT27916712  42349073
NMPPL-SIK/OFF/RT27917080  42349107
159227660942  42349938
159227660942  42349952
ATI/23-24/4628422284  42350916
8321127883461  5943992158
20.01.202328585286  5946437446
20.01.202328585286  5946437658
10.02.202328585286  5946443575
10.02.202328585286  5946443699
10.02.202328585286  5946443705
20.02.202328587403  5946586447
7045127883461  5946789505
08.01.202328576858  5946853727
08.01.202328576858  5946853728
22028585286  5946954412
7069228428832  5947154833
7069228428832  5947154833
7069228428832  5947154833
7069228428832  5947154833
7069428428832  5947155105
7069428428832  5947155105
7069428428832  5947155105
7069428428832  5947155105
7070028428832  5947155230
7070028428832  5947155230
7070028428832  5947155230
7070028428832  5947155230
DOM2023240146128428832  5947256693
DOM2023240146128428832  5947256693
9128482429  5947289137
9128482429  5947289137
9128482429  5947289137
9128482429  5947289137
9128482429  5947289137
9128482429  5947289137
9128482429  5947289137
9128482429  5947289137
9128482429  5947289137
9128482429  5947289137
9128482429  5947289137
9128482429  5947289137
9128482429  5947289137
9128482429  5947289137
9128482429  5947289137
9128482429  5947289137
9128482429  5947289137
9128482429  5947289137
9128482429  5947289137
9128482429  5947289137
9228482429  5947289437
9228482429  5947289437
9228482429  5947289437
9228482429  5947289437
9228482429  5947289437
9228482429  5947289437
9228482429  5947289437
9228482429  5947289437
9228482429  5947289437
9228482429  5947289437
9528482429  5947291344
9528482429  5947291344
9528482429  5947291344
9528482429  5947291344
9528482429  5947291344
9528482429  5947291344
9528482429  5947291344
9528482429  5947291344
9528482429  5947291344
9528482429  5947291344
9528482429  5947291344
9528482429  5947291344
9428482429  5947291348
9428482429  5947291348
9428482429  5947291348
9428482429  5947291348
9428482429  5947291348
9428482429  5947291348
9428482429  5947291348
9428482429  5947291348
9428482429  5947291348
9428482429  5947291348
10028482429  5947291431
10028482429  5947291431
10028482429  5947291431
10028482429  5947291431
9728482429  5947291434
9728482429  5947291434
9728482429  5947291434
9728482429  5947291434
9728482429  5947291434
9728482429  5947291434
9928482429  5947291437
9928482429  5947291437
9928482429  5947291437
9928482429  5947291437
9928482429  5947291437
9928482429  5947291437
9828482429  5947291510
9828482429  5947291510
9828482429  5947291510
9828482429  5947291510
9828482429  5947291510
9828482429  5947291510
9828482429  5947291510
9828482429  5947291510
9828482429  5947291510
9828482429  5947291510
10128482429  5947402859
10128482429  5947402859
10228482429  5947402866
10228482429  5947402866
DOM2023240150228428832  5947403044
DOM2023240150228428832  5947403044
DOM2023240150428428832  5947403045
DOM2023240150428428832  5947403045
DOM2023240150428428832  5947403045
DOM2023240150428428832  5947403045
DOM2023240153528428832  5947403048
DOM2023240153528428832  5947403048
DOM2023240150428428832  5947403105
DOM2023240150428428832  5947403105
DOM2023240148628428832  5947403129
DOM2023240148628428832  5947403129
DOM2023240148728428832  5947403191
DOM2023240148728428832  5947403191
DOM2023240148828428832  5947403192
DOM2023240148828428832  5947403192
DOM2023240149228428832  5947403195
DOM2023240149228428832  5947403195
DOM2023240153128428832  5947403345
DOM2023240153128428832  5947403345
DOM2023240153328428832  5947403346
DOM2023240153328428832  5947403346
DOM2023240169828428832  5947403482
DOM2023240169828428832  5947403482
DOM2023240153228428832  5947403485
DOM2023240153228428832  5947403485
DOM2023240153428428832  5947403489
DOM2023240153428428832  5947403489
DOM2023240147828428832  5947405163
DOM2023240147828428832  5947405163
DOM2023240148028428832  5947405165
DOM2023240148028428832  5947405165
DOM2023240147928428832  5947405222
DOM2023240147928428832  5947405222
DOM2023240148128428832  5947405225
DOM2023240148128428832  5947405225
DOM2023240170628428832  5947405226
DOM2023240170628428832  5947405226
DOM2023240151928428832  5947405487
DOM2023240151928428832  5947405487
DOM2023240153728428832  5947405489
DOM2023240153728428832  5947405489
DOM2023240153828428832  5947405682
DOM2023240153828428832  5947405682
DOM2023240154128428832  5947405683
DOM2023240154128428832  5947405683
DOM2023240154328428832  5947405684
DOM2023240154328428832  5947405684
DOM2023240169928428832  5947405685
DOM2023240169928428832  5947405685
DOM2023240154228428832  5947405686
DOM2023240154228428832  5947405686
DOM2023240154428428832  5947405688
DOM2023240154428428832  5947405688
DOM2023240169928428832  5947405689
DOM2023240169928428832  5947405689
SEPL/202/23-2428601947  5947472159
SEPL/202/23-2428601947  5947472159
SEPL/203/23-2428601947  5947472322
SEPL/203/23-2428601947  5947472322
SEPL/204/23-2428601947  5947487589
SEPL/204/23-2428601947  5947487589
SEPL/200/23-2428601947  5947518405
SEPL/200/23-2428601947  5947518405
SEPL/201/23-2428601947  5947518407
SEPL/201/23-2428601947  5947518407
SEPL/205/23-2428601947  5947540367
SEPL/205/23-2428601947  5947540367
SEPL/206/23-2428601947  5947540369
SEPL/206/23-2428601947  5947540369
INV/04328684753  5947701395
INV/04328684753  5947701395
INV/04328684753  5947701395
INV/04328684753  5947701395
INV/04328684753  5947701395
INV/04328684753  5947701395
INV/04328684753  5947701395
INV/04328684753  5947701395
INV/04328684753  5947701395
1J/5/50069/23-2429112976  5947760402
1J/5/50069/23-2429112976  5947760402
1J/5/50068/23-2429112976  5947760403
1J/5/50068/23-2429112976  5947760403
DOM2023240047528428832  5947764671
DOM2023240047528428832  5947764671
DOM2023240047528428832  5947764671
DOM2023240047528428832  5947764671
DOM2023240047528428832  5947764671
DOM2023240047528428832  5947764671
DOM2023240047528428832  5947764671
DOM2023240047528428832  5947764671
DOM2023240150328428832  5947764675
DOM2023240150328428832  5947764675
DOM2023240150328428832  5947764675
DOM2023240150328428832  5947764675
DOM2023240047528428832  5947820731
DOM2023240047528428832  5947820731
DOM2023240047528428832  5947820731
DOM2023240047528428832  5947820731
DOM2023240047528428832  5947820731
DOM2023240047528428832  5947820731
1J/5/50081/23-2429112976  5947878329
1J/5/50081/23-2429112976  5947878329
1J/5/50081/23-2429112976  5947878329
1J/5/50082/23-2429112976  5947879253
1J/5/50082/23-2429112976  5947879253
1J/5/50082/23-2429112976  5947879253
09228482429   
09228482429   
AS/IOCL/23-24/0828914923   
09228482429  #N/A
09228482429  #N/A
09228482429  #N/A
09228482429  #N/A
DOM2023240047528428832  5947764671
09228482429  #N/A
09228482429  #N/A

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.