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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Syndicate_Admin
Administrator
Administrator

Excel Power Query Transforming Multiple Columns based on a Single Condition

I am working with an ODBC query that is pulling customer invoice information.

Some invoice types do not store billing information since they are more or less automatic.

 

What I am wanting to do is if a record's [BillToName] is null then I want to replace all of the billing address columns with the customer's main address..

 

So if [BillToName] = null then

[BillToName] = [CustomerName] and

[BillToAddress1] = [AddressLine1] and

[BillToAddress2] = [AddressLine2] and etc.....

 

It's important to note that only 1 column is being checked (BillToName). I can't mix/match each individual column where some parts of the address somehow was there so it didn't get replaced and the other columns that were null did get replaced to where the billing address has pieces of both addresses.

 

Thanks for any help you can provide!

3 REPLIES 3
Syndicate_Admin
Administrator
Administrator

Sure.

 

Sample Data Table

 

CustomerNoCustomerNameInvoiceNoAddressLine1AddressLine2AddressLine3CityStateZipCodeCountryCodeBillToNameBillToAddress1BillToAddress2BillToAddress3BillToCityBillToStateBillToZipCodeBillToCountryCode
ABC001ABC Company, Inc1234567123 Main Street  My CityST11123-1234USA        
ABC002ABC Company, Inc d/b/a MyCo9876543123 Main Street  My CityST11123-1234USAMyCo900 Any StSuite 200 El CityZZ55123-9898USA

 

For each record that contains a null [BillToName], every portion of the "BillTo" address is replaced with the Customer's main address:

{[CustomerName], [AddressLine1], [AddressLine2], etc}

 

I have attached a sample workbook containing the above with a local table that has been pulled into Power Query as well.

Here's one way:

 

let
    Source = Excel.CurrentWorkbook(){[Name="Table26"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{
        {"CustomerNo", type text}, {"CustomerName", type text}, {"InvoiceNo", Int64.Type}, 
        {"AddressLine1", type text}, {"AddressLine2", type any}, {"AddressLine3", type any}, 
        {"City", type text}, {"State", type text}, {"ZipCode", type text}, 
        {"CountryCode", type text}, {"BillToName", type text}, {"BillToAddress1", type text}, 
        {"BillToAddress2", type text}, {"BillToAddress3", type any}, 
        {"BillToCity", type text}, {"BillToState", type text}, {"BillToZipCode", type text}, 
        {"BillToCountryCode", type text}}),

//Fill in if BillTo is Blank
    #"Bill To" = 
        Table.FromRecords(
            Table.TransformRows(#"Changed Type", (r)=>
                if r[BillToName] = null or r[BillToName] = "" or r[BillToName] = " " then 
                Record.TransformFields(r,{
                    {"BillToName", each r[CustomerName]},
                    {"BillToAddress1", each r[AddressLine1]},
                    {"BillToAddress2", each r[AddressLine2]},
                    {"BillToAddress3", each r[AddressLine3]},
                    {"BillToCity", each r[City]},
                    {"BillToState", each r[State]},
                    {"BillToZipCode", each r[ZipCode]},
                    {"BillToCountryCode", each r[CountryCode]}
                }) else r)
        ),

//Being lazy, I made them all type text, but you can be more specific if necessary
    #"Changed Type2" = Table.TransformColumnTypes(#"Bill To",
        List.Transform(Table.ColumnNames(#"Bill To"), each {_, type text})) 
            
in
    #"Changed Type2"

ronrsnfld_0-1689356497067.png

 

 

 

ronrsnfld
Super User
Super User

Perhaps if you provide a textual (not a picture) data sample that illustrates your problem, and a screenshot of your expected results, focused assistance could be more easily provided.

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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