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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
DanFromMontreal
Helper III
Helper III

Modifing data based on a correction table

Hello dear Power BI community,

I've got a good challenge that I was unable to resolve and/or find the right approach to modify several data in a table.

I have a table (tblData) containing more than 3000 lines.

I was given a table containing the correction (tblCorrection) that I need to apply to my tblData (in red).

The final result should look like the tblDataFinal.

The way I see it is that I need to find in the tblCorrection where there is a correction (red) and find the intersection Column-Row in the tblData and do the replace.

If the cell is empty, there is NO correction to be made.

Easier said then done as I'm a rookie in Power Query.

Any advice how to approach this?

 

Thank you in advance.

 

DanFromMontreal_0-1651000850464.png

 

 

RowIDCustomerCityAddressZipcodePhone

Row1  CC  
Row5AA  BB 
Row8 DD   
Row10  EEFFGG

 

RowIDCustomerCityAddressZipcodePhone

Row1Customer-Row1City-Row1CCZipcode-Row1Phone-Row1
Row2Customer-Row2City-Row2Address-Row2Zipcode-Row2Phone-Row2
Row3Customer-Row3City-Row3Address-Row3Zipcode-Row3Phone-Row3
Row4Customer-Row4City-Row4Address-Row4Zipcode-Row4Phone-Row4
Row5AACity-Row5Address-Row5BBPhone-Row5
Row6Customer-Row6City-Row6Address-Row6Zipcode-Row6Phone-Row6
Row7Customer-Row7City-Row7Address-Row7Zipcode-Row7Phone-Row7
Row8Customer-Row8DDAddress-Row8Zipcode-Row8Phone-Row8
Row9Customer-Row9City-Row9Address-Row9Zipcode-Row9Phone-Row9
Row10Customer-Row10City-Row10EEFFGG
1 ACCEPTED SOLUTION
v-kkf-msft
Community Support
Community Support

Hi @DanFromMontreal ,

 

Please try the following code:

 

let

    #"Unpivot tblData" = Table.UnpivotOtherColumns(tblData, {"RowID"}, "Attribute", "Value"),
    #"Unpivot tblCorrection" = Table.UnpivotOtherColumns(tblCorrection, {"RowID"}, "Attribute", "Value"),
    Source = Table.NestedJoin(#"Unpivot tblData", {"RowID", "Attribute"}, #"Unpivot tblCorrection", {"RowID", "Attribute"}, "tblCorrection", JoinKind.LeftOuter),
    #"Expanded tblCorrection" = Table.ExpandTableColumn(Source, "tblCorrection", {"Value"}, {"tblCorrection.Value"}),
    #"Added Custom" = Table.AddColumn(#"Expanded tblCorrection", "Custom", each if [tblCorrection.Value] = " " or [tblCorrection.Value] = null then [Value] else [tblCorrection.Value]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Value", "tblCorrection.Value"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Attribute]), "Attribute", "Custom")
in
    #"Pivoted Column"

vkkfmsft_0-1651217330855.png

 

If the problem is still not resolved, please provide detailed error information or the expected result you expect. Let me know immediately, looking forward to your reply.
Best Regards,
Winniz
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

6 REPLIES 6
v-kkf-msft
Community Support
Community Support

Hi @DanFromMontreal ,

 

Please try the following code:

 

let

    #"Unpivot tblData" = Table.UnpivotOtherColumns(tblData, {"RowID"}, "Attribute", "Value"),
    #"Unpivot tblCorrection" = Table.UnpivotOtherColumns(tblCorrection, {"RowID"}, "Attribute", "Value"),
    Source = Table.NestedJoin(#"Unpivot tblData", {"RowID", "Attribute"}, #"Unpivot tblCorrection", {"RowID", "Attribute"}, "tblCorrection", JoinKind.LeftOuter),
    #"Expanded tblCorrection" = Table.ExpandTableColumn(Source, "tblCorrection", {"Value"}, {"tblCorrection.Value"}),
    #"Added Custom" = Table.AddColumn(#"Expanded tblCorrection", "Custom", each if [tblCorrection.Value] = " " or [tblCorrection.Value] = null then [Value] else [tblCorrection.Value]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Value", "tblCorrection.Value"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Attribute]), "Attribute", "Custom")
in
    #"Pivoted Column"

vkkfmsft_0-1651217330855.png

 

If the problem is still not resolved, please provide detailed error information or the expected result you expect. Let me know immediately, looking forward to your reply.
Best Regards,
Winniz
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Wow @v-kkf-msft , it does the trick.

Now, I need to better understand each step and learn from it.

Thank you

Anonymous
Not applicable

this should be faster.

if you publish tables with fake data but with the size and structure of your tables, one can do some tests to find the best performing solution.

 

PS

The "optimal" strategy obviously depends on the size of the tables.
From how many columns and how many rows the tables typically have it derives whether it is convenient to make the transformations by rows or by columns, whether to merge or do lookups, etc ...

Anonymous
Not applicable

let
    Origine = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("XdK7CsJAFIThd9k6wmbvW4ovIJaGVCZgCl0xEfHtBfHAzOlmmq/6h8Gc2rs3nTm81q3d5udO/rJ9ZO+n6Tmvq9zz8ri0aZZ7vLb7/4zdz3PKc+A59hx7Dj0nnleeB8+z59nz6HnxgvICeIG9wF5AL4gXlRfBi+xF9iJ6UbykvAReYi+xl9BL4mXlZfAye5m9jF4WryivgFfYK+wV9Ip4VXkVvMpeZa+iV8XrrQ7aYtFWJW1V05aitmYcvw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [RowID = _t, Customer = _t, City = _t, Addres = _t, Zipcode = _t, Phone = _t]),
    #"Modificato tipo" = Table.TransformColumnTypes(Origine,{{"RowID", type text}, {"Customer", type text}, {"City", type text}, {"Addres", type text}, {"Zipcode", type text}, {"Phone", type text}}),
    #"Merge di query eseguito" = Table.NestedJoin(#"Modificato tipo", {"RowID"}, corr, {"RowID"}, "corr", JoinKind.LeftOuter),
    #"Tabella corr espansa" = Table.ExpandTableColumn(#"Merge di query eseguito", "corr", {"RowID", "Customer", "City", "Addres", "Zipcode", "Phone"}, {"RowID.1", "Customer.1", "City.1", "Addres.1", "Zipcode.1", "Phone.1"}),
    nrows=Table.RowCount(Origine),
    ncols=Table.ColumnCount(Origine),
    names=Table.ColumnNames(Origine),
    ttc=Table.ToColumns(#"Tabella corr espansa"),
    tr=List.Transform({0..ncols-1}, each List.Transform({0..nrows-1}, (r)=>ttc{_+6}{r}??ttc{_}{r})),
    tfc=Table.FromColumns(tr,names)
in
    tfc
Anonymous
Not applicable

 

let
    Origine = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCsovN1TSUYIgZ2coM1YHLGMKZDs6wqWdnJDkLCBiLi5waZiMoQFcyNUVSLi5AQl3d6XYWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [RowID = _t, Customer = _t, City = _t, Addres = _t, Zipcode = _t, Phone = _t]),
    #"Sostituito valore" = Table.ReplaceValue(Origine,"",null,Replacer.ReplaceValue,{"Customer", "City", "Addres", "Zipcode", "Phone"})
in
    #"Sostituito valore"
let
    Origine = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("XdK7CsJAFIThd9k6wmbvW4ovIJaGVCZgCl0xEfHtBfHAzOlmmq/6h8Gc2rs3nTm81q3d5udO/rJ9ZO+n6Tmvq9zz8ri0aZZ7vLb7/4zdz3PKc+A59hx7Dj0nnleeB8+z59nz6HnxgvICeIG9wF5AL4gXlRfBi+xF9iJ6UbykvAReYi+xl9BL4mXlZfAye5m9jF4WryivgFfYK+wV9Ip4VXkVvMpeZa+iV8XrrQ7aYtFWJW1V05aitmYcvw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [RowID = _t, Customer = _t, City = _t, Addres = _t, Zipcode = _t, Phone = _t]),
    #"Modificato tipo" = Table.TransformColumnTypes(Origine,{{"RowID", type text}, {"Customer", type text}, {"City", type text}, {"Addres", type text}, {"Zipcode", type text}, {"Phone", type text}}),
    ncols=Table.ColumnCount(#"Modificato tipo"),
    ttr=Table.TransformRows(#"Modificato tipo" , each Record.FromList(List.Transform({0..ncols-1}, (f)=> Record.FieldValues(corrtab{[RowID=[RowID]]}? ?? _){f} ?? Record.FieldValues(_){f}),Record.FieldNames(_)))
in
    Table.FromRecords(ttr)

 

Goog morning @Anonymous ,

Thank you for answering to my post and sorry for the delay.

I have tried your solution but your last "let" is giving mr an error.  It doest not recognize "corrtab" in:

    ttr=Table.TransformRows(#"Modificato tipo" , each Record.FromList(List.Transform({0..ncols-1}, (f)=> Record.FieldValues(corrtab{[RowID=[RowID]]}? ?? _){f} ?? Record.FieldValues(_){f}),Record.FieldNames(_)))

 

I therefore cannot test your solution.

No error were found in the other 2 script.

Tried to understand the error to fix it but could not.

Also, thank you for your advice about loading a file.  Read the post and it is very insightful.

However, due to company's policy, the use of Dropbox, Google drive or other tool, it is strickly forbidden.

 

Thank you again for looking once more at your code to find the problem.

Merci beaucoup / grazie. 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors