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
IvanS
Helper V
Helper V

Removing column after calculation in Power Query (for data anonymization purpose)

Hello,

 

I am creating data model with accounting data which means that there is information which needs to be anonymized. I have 2 tables:

 

Table "Invoices"

Vendor NameVendor IDInvoice Nr.Invoice AmountCurrency
AAA1111231231000EUR
BBB2223213212000EUR
CCC3334564563000USD
AAA111654654500USD
CCC333789789800GBP

 

Then I have another table: "Internal vendors"

Vendor ID
AAA
BBB
DDD
EEE

 

I need to create calculated column which will add the "Alternative Vendor Name" by looking up the "Internal Vendors" table and if the vendor is there, it will add text "INTERNAL" and if not, then it will take the name from "Vendor Name".

 

Now, the problem is that I need to remove the "Vendor Name" column from table "Invoices" so the end-users cannot see the original names. Hiding column is not very helpful, as users can use measures (such as SELECTEDVALUE) to get the data. As soon as I remove the column, my caluculation in "Alternative Vendor Name" is failing.

The desired result is following:

Alternative Vendor NameVendor IDInvoice Nr.Invoice AmountCurrency
INTERNAL1111231231000EUR
INTERNAL2223213212000EUR
CCC3334564563000USD
INTERNAL111654654500USD
CCC333789789800GBP

 

Is there any solution for that (e.g. Power BI will perform calculation first and then remove the column)?


Thank you for any suggestions!

Ivan

1 ACCEPTED SOLUTION
v-yiruan-msft
Community Support
Community Support

Hi @IvanS ,

I created a sample pbix file(see attachment) for you base on provided table data, please check whether that is what you want.

1. Merge these two tables base on the column [Vendor Name] of table "Invoices" and the column [Vendor ID] of table "Internal vendors"

yingyinr_0-1646991062806.png

2. Add custom column to update the value base on the conditions

yingyinr_1-1646991147884.png

3. Remove the orignial column [Vendor Name] in the table Invoices

The full applied codes as follow:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("VY07DoAgEETvsjUFLB+xBDS2RkNFuP81nMUGkpdlJryF1iilRIqMMTLZAglaaxxnfairRjlnNGbGtGyA1NUppcitlXXnA5D6O/U9hjP/FbwDCH5R5me2uAOEOJQr39T7Bw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Vendor Name" = _t, #"Vendor ID" = _t, #"Invoice Nr." = _t, #"Invoice Amount" = _t, Currency = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Vendor Name", type text}, {"Vendor ID", Int64.Type}, {"Invoice Nr.", Int64.Type}, {"Invoice Amount", Int64.Type}, {"Currency", type text}}),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"Vendor Name"}, #"Internal vendors", {"Vendor ID"}, "Internal vendors", JoinKind.LeftOuter),
    #"Expanded Internal vendors" = Table.ExpandTableColumn(#"Merged Queries", "Internal vendors", {"Vendor ID"}, {"Vendor ID.1"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Internal vendors", "Alternative Vendor Name", each if [Vendor ID.1]=null then [Vendor Name] else "INTERNAL" ),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Vendor ID.1", "Vendor Name"})
in
    #"Removed Columns"

yingyinr_2-1646991473574.png

Best Regards

Community Support Team _ Rena
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

3 REPLIES 3
v-yiruan-msft
Community Support
Community Support

Hi @IvanS ,

I created a sample pbix file(see attachment) for you base on provided table data, please check whether that is what you want.

1. Merge these two tables base on the column [Vendor Name] of table "Invoices" and the column [Vendor ID] of table "Internal vendors"

yingyinr_0-1646991062806.png

2. Add custom column to update the value base on the conditions

yingyinr_1-1646991147884.png

3. Remove the orignial column [Vendor Name] in the table Invoices

The full applied codes as follow:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("VY07DoAgEETvsjUFLB+xBDS2RkNFuP81nMUGkpdlJryF1iilRIqMMTLZAglaaxxnfairRjlnNGbGtGyA1NUppcitlXXnA5D6O/U9hjP/FbwDCH5R5me2uAOEOJQr39T7Bw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Vendor Name" = _t, #"Vendor ID" = _t, #"Invoice Nr." = _t, #"Invoice Amount" = _t, Currency = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Vendor Name", type text}, {"Vendor ID", Int64.Type}, {"Invoice Nr.", Int64.Type}, {"Invoice Amount", Int64.Type}, {"Currency", type text}}),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"Vendor Name"}, #"Internal vendors", {"Vendor ID"}, "Internal vendors", JoinKind.LeftOuter),
    #"Expanded Internal vendors" = Table.ExpandTableColumn(#"Merged Queries", "Internal vendors", {"Vendor ID"}, {"Vendor ID.1"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Internal vendors", "Alternative Vendor Name", each if [Vendor ID.1]=null then [Vendor Name] else "INTERNAL" ),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Vendor ID.1", "Vendor Name"})
in
    #"Removed Columns"

yingyinr_2-1646991473574.png

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
johnt75
Super User
Super User

Create the calculated column in Power Query, then you will be able to safely delete the original column and it won't be exposed in your report.

Merge your data table with the Internal Vendors table, then create a new custom column. If the value from the merged column is null then use the original vendor name, otherwise use "INTERNAL". You can then delete the original column and any other columns introduced during the merge.

Samarth_18
Community Champion
Community Champion

Hi @IvanS ,

 

Please refer to below article. I have used it earlier in my case. Hope it help you too.

 

https://eriksvensen.wordpress.com/2019/02/28/powerquery-replicate-doing-an-excel-vlookup-in-m/ 

 

Thanks,

Samarth

Best Regards,
Samarth

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!
Connect on Linkedin

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.