Eliminate lasts characters from field by list loop criteria in Power Query

pepito

New Member
Joined
Dec 21, 2020
Messages
1
Office Version
  1. 2016
Platform
  1. Windows
Hi guys, hope you're doing fine:

I have a table in Excel with many products, these products may have a color id by the end of the product code chain or not.

I have another table with colors ( let's say a color list )

Sample.JPG
Image Enlarger


How can I do a loop to eliminate color id by row in Products based in color list? I have tried to go with last occurrence of delimitator '-' but my rows may have other numbers than color ids by the end

Thank you very much in advance
Cool


**List of colors in very short for sample purposes
 

Attachments

  • Sample.JPG
    Sample.JPG
    105.2 KB · Views: 8

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
maybe
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Product"]}[Content],
    Count = Table.AddColumn(Source, "SCount", each List.Count(Text.Split([Products],"-"))-1),
    IF = Table.AddColumn(Count, "Custom", each if [SCount] = 1 then Text.BeforeDelimiter([Products], "-") else [Products]),
    TSC = Table.SelectColumns(IF,{"Custom"})
in
    TSC
Custom
001080001
006046122
008020018
014112448
206210830A
944444444-220110142-1
944444444-220110348-2
944444444-220110728-2
944444444-220110804-4
944444444-220110804-5
944444444- 220120007-8
944444444- 220120038- 2
944444444-220120183-11
944444444- 220120183-12
944444444-220120183-13
944444444-220120335-2
944444444-220120335-3
944444444-220120335-4
944444444 M -220110007-10
DET-001030-01
DET-001032-01
DET-001035-01
DET-001036-01
PIECE-220110099-2

btw. use XL2BB to post representative source data and expected result, pictures are useless
 
Last edited:
Upvote 0
or if you absolutely want to use color list table
Power Query:
let
    Product = Excel.CurrentWorkbook(){[Name="Product"]}[Content],
    TAD = Table.AddColumn(Product, "TAD", each Text.AfterDelimiter([Products], "-", {0, RelativePosition.FromEnd}), type text),
    Color = Excel.CurrentWorkbook(){[Name="Color"]}[Content],
    Join = Table.NestedJoin(TAD,{"TAD"},Color,{"color list"},"Color",JoinKind.LeftOuter),
    Expand = Table.ExpandTableColumn(Join, "Color", {"color list"}, {"color list"}),
    Replace = Table.ReplaceValue(Expand,null,"@",Replacer.ReplaceValue,{"color list"}),
    IF = Table.AddColumn(Replace, "Result", each if not Text.Contains([Products], [color list]) then [Products] else Text.BeforeDelimiter([Products], "-")),
    TSC = Table.SelectColumns(IF,{"Result"})
in
    TSC
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,753
Messages
6,174,307
Members
452,554
Latest member
Louis1225

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