POWER QUERY: Add value in column based on search in another column

furqan_yousuf

New Member
Joined
Apr 18, 2021
Messages
5
Office Version
  1. 365
Platform
  1. Windows
I want this done in power query: I downloaded statement from bank and based on certain value in the description field I want to assign value to it. For eg. I download transactions from bank and it has 3 columns (date, description, amount) I will add 4 the column next to amount called "Customer Name". If the description contains "New York" "Manhattan" "Times Sq" the Customer Name will be NYC -- if desc contains "IL" Chicago" "O'hare" the customer name will be "Chicago City" . Excuse my english
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
I am sure there are many other ways, probably better ones, but here is what I came up with:

Sample source data:
Book1
ABC
1DateDescriptionAmount
21-JanNew York100
34-JanIL123
47-JanNew York150
59-JanChicago199
622-JanSan Diego330
Sheet1


Power Query:
let
    // Sample source data taken from the worksheet
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Data = Table.TransformColumnTypes(Source,{{"Date", type datetime}, {"Description", type text}, {"Amount", Int64.Type}}),

    // The first list item is "Customer Name" return value for the match
    filter = {{"NYC", "New York", "Manhattan", "Times Sq"},
              {"Chicago City", "IL", "Chicago", "O'hare"}},


    Filter = Table.AddColumn(Data, "Customer Name",  
        (d) => List.Generate(
            () => [i = 0],
            each [i] < List.Count(filter),
            each [i = [i] + 1],
            each if List.AnyTrue(List.Transform(filter{[i]}, each Text.Contains(d[Description], _)))
                    then filter{[i]}{0}
                    else null
        )
    ),
    Result = Table.TransformColumns(Filter, {"Customer Name", each Text.Combine(List.Transform(_, Text.From),"-"), type text})

in
    Result

Book1
ABCD
1DateDescriptionAmountCustomer Name
21/1/2021New York100NYC
31/4/2021IL123Chicago City
41/7/2021New York150NYC
51/9/2021Chicago199Chicago City
61/22/2021San Diego330
Result
 
Upvote 0

Forum statistics

Threads
1,215,263
Messages
6,123,956
Members
449,135
Latest member
jcschafer209

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top