Custom Column For Matching Values

radonwilson

New Member
Joined
Jun 23, 2021
Messages
49
Office Version
  1. 2019
Platform
  1. Windows
  1. In Trans_Type Col, I have 3 transaction types i.e. Order, Shipping Services & Refund.
  2. Now, some Order_IDs of Order (Trans_Type) are matching with Refund (Trans_Type), I want to tag these matched as "Found" and the rest as "Unfound" in Custom Column.
How to create a custom column in a power query?

Demo.xlsx
ABC
1Trans_TypeOrder IDHelper/Custom Column
2Order111Not Found
3Shipping Service111Not Found
4Refund999Found
5Shipping Service222Not Found
6Order222Found
7Order333Not Found
8Order444Not Found
9Order555Found
10Order666Not Found
11Order777Found
12Order888Not Found
13Order999Found
14Refund777Found
15Shipping Service333Not Found
16Shipping Service444Not Found
17Shipping Service555Not Found
18Shipping Service999Not Found
19Refund222Found
20Refund555Found
21Refund2121Not Found
22Shipping Service666Not Found
23Shipping Service777Not Found
24Shipping Service888Not Found
25Refund4242Not Found
26Refund3939Not Found
27Refund5757Not Found
Sheet1
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
I have "" for Shipping Services, but you can change that in the end of each If statement.
mr excel questions 12.xlsm
ABCD
1Trans_TypeOrder IDHelper/Custom Column
2Order111Not FoundNot Found
3Shipping Service111 Not Found
4Refund999FoundFound
5Shipping Service222 Not Found
6Order222FoundFound
7Order333Not FoundNot Found
8Order444Not FoundNot Found
9Order555FoundFound
10Order666Not FoundNot Found
11Order777FoundFound
12Order888Not FoundNot Found
13Order999FoundFound
14Refund777FoundFound
15Shipping Service333 Not Found
16Shipping Service444 Not Found
17Shipping Service555 Not Found
18Shipping Service999 Not Found
19Refund222FoundFound
20Refund555FoundFound
21Refund2121Not FoundNot Found
22Shipping Service666 Not Found
23Shipping Service777 Not Found
24Shipping Service888 Not Found
25Refund4242Not FoundNot Found
26Refund3939Not FoundNot Found
27Refund5757Not FoundNot Found
Sheet32
Cell Formulas
RangeFormula
C2:C27C2=IF(A2="Order",IF(ISNUMBER(MATCH("Refund"&B2,$A$2:$A$27&$B$2:$B$27,0)),"Found","Not Found"), IF(A2="Refund",IF(ISNUMBER(MATCH("Order"&B2,$A$2:$A$27&$B$2:$B$27,0)),"Found","Not Found"),""))
 
Upvote 0
I have "" for Shipping Services, but you can change that in the end of each If statement.
mr excel questions 12.xlsm
ABCD
1Trans_TypeOrder IDHelper/Custom Column
2Order111Not FoundNot Found
3Shipping Service111 Not Found
4Refund999FoundFound
5Shipping Service222 Not Found
6Order222FoundFound
7Order333Not FoundNot Found
8Order444Not FoundNot Found
9Order555FoundFound
10Order666Not FoundNot Found
11Order777FoundFound
12Order888Not FoundNot Found
13Order999FoundFound
14Refund777FoundFound
15Shipping Service333 Not Found
16Shipping Service444 Not Found
17Shipping Service555 Not Found
18Shipping Service999 Not Found
19Refund222FoundFound
20Refund555FoundFound
21Refund2121Not FoundNot Found
22Shipping Service666 Not Found
23Shipping Service777 Not Found
24Shipping Service888 Not Found
25Refund4242Not FoundNot Found
26Refund3939Not FoundNot Found
27Refund5757Not FoundNot Found
Sheet32
Cell Formulas
RangeFormula
C2:C27C2=IF(A2="Order",IF(ISNUMBER(MATCH("Refund"&B2,$A$2:$A$27&$B$2:$B$27,0)),"Found","Not Found"), IF(A2="Refund",IF(ISNUMBER(MATCH("Order"&B2,$A$2:$A$27&$B$2:$B$27,0)),"Found","Not Found"),""))
Any simpler way of doing the same?

How to create a custom column in POWER QUERY?
 
Upvote 0
yes, you could do it in power query, I'm sorry I am not a expert an custom columns there.
 
Upvote 0
Power Query:
let
    Source = Table.Buffer(Excel.CurrentWorkbook(){[Name="Table1"]}[Content]),
    Result = Table.AddColumn(Source, "Found?", each 
        let 
            lst = Table.SelectRows(Source, (x)=> x[Order ID] = [Order ID] and [Trans_Type]<>"Shipping Service")[Trans_Type]
        in 
            if List.ContainsAll(lst, {"Order", "Refund"}) then "Found" else "Not Found"
    )
in
    Result
 
Upvote 0
Solution
Power Query:
let
    Source = Table.Buffer(Excel.CurrentWorkbook(){[Name="Table1"]}[Content]),
    Result = Table.AddColumn(Source, "Found?", each
        let
            lst = Table.SelectRows(Source, (x)=> x[Order ID] = [Order ID] and [Trans_Type]<>"Shipping Service")[Trans_Type]
        in
            if List.ContainsAll(lst, {"Order", "Refund"}) then "Found" else "Not Found"
    )
in
    Result
Thanks for your solution it's working.

Where I can learn this power query m language from basics?
 
Upvote 0
Power Query:
let
    Source = Table.Buffer(Excel.CurrentWorkbook(){[Name="Table1"]}[Content]),
    Result = Table.AddColumn(Source, "Found?", each
        let
            lst = Table.SelectRows(Source, (x)=> x[Order ID] = [Order ID] and [Trans_Type]<>"Shipping Service")[Trans_Type]
        in
            if List.ContainsAll(lst, {"Order", "Refund"}) then "Found" else "Not Found"
    )
in
    Result
could you please explain how its working?
 
Upvote 0
Table.AddColumn goes row by row through the Source table, for each row it gets the Trans_Type column of a filtered table where that filter is OrderID matches that row's OrderID, and that and it's Trans_Type value is not Shipping Service. That value is stored as a list in the variable lst. Then it checks to see if both the words Order and Refund are in the list, if True then return the word Found in that row of the new column ([Found?]) if false then return "Not Found."

To learn M I recommend searching on YouTube where there are hundreds of tutorials.
 
Upvote 0
One of the best PQ tutorials is a full college level class - Full Advanced Data Analysis & BI Class (MSPTDA). Power Query. I highly recommend it. It's currently 83 videos, and nearly all have start and end workbook samples to download as well as data and PDFs as needed. The channel is amazing. Don't be fooled by how old the videos are, they still teach the basic and even high level skills needed for PQ. You might want to check out the channel itself. Mike is one of the best with PQ that I've seen!
 
Upvote 0
Table.AddColumn goes row by row through the Source table, for each row it gets the Trans_Type column of a filtered table where that filter is OrderID matches that row's OrderID, and that and it's Trans_Type value is not Shipping Service. That value is stored as a list in the variable lst. Then it checks to see if both the words Order and Refund are in the list, if True then return the word Found in that row of the new column ([Found?]) if false then return "Not Found."

To learn M I recommend searching on YouTube where there are hundreds of tutorials.
Thank you for the detailed answer.
 
Upvote 0

Forum statistics

Threads
1,215,482
Messages
6,125,060
Members
449,206
Latest member
Healthydogs

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