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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
ecarroll
Frequent Visitor

Power Query/BI Question - Merging Query Within Itself

Hi - I have a query that looks something like this:

Company NameTypeCode
ABC CoInitial 
ABC CompanyFinal1234.100
ABC FSChange 
XYZ CoQC 
XYZ CoFinal5678.100
Example CoInitial 
Example CompanyFinal8675.309

 

And I want to drag the client code up/down based on similar looking names. In some cases the names will be the same. If it's not 100% accurate and can only work on exact names that is ok too. The ideal scenario will be an outcome like this: 

Company NameTypeCode
ABC CoInitial1234.100
ABC CompanyFinal1234.100
ABC FSChange1234.100
XYZ CoQC5678.100
XYZ CoFinal5678.100
Example CoInitial8675.309
Example CompanyFinal8675.309

 

I've tried a few different options but I can't seem to get it to work, hoping for some new ideas. Some background - I've tried doing a lookup value from within powerbi (kept coming up blank), and also creating a separate query of just the "Finals" with the client codes and merging back to the original query - this broke the computer it took so long so I don't know if it worked or not. Any ideas would be much appreciated!

1 ACCEPTED SOLUTION

Around 6,000 but I'm really only concerned about 2,000 of them. They come from a Microsoft Form (people enter in names manually, and are not consistent when they do). Yes, they will continualy enter them in so I can't maintain any kind of master list. I do have a master list of the names but people don't always use the same name as the master list (in fact they rarely do). It's the different between saying Advertising Booking Company and ABC - sometimes they spell it out, sometimes they don't. I am going to add an optional client ID field to the form for people to fill in, but unfortunately I can't make it required, so I will keep running into this issue. 

View solution in original post

5 REPLIES 5
ecarroll
Frequent Visitor

Thanks but I guess I should have said that the company names are much more complex than that so splitting with a delimiter is not an option, they need to stay as they are. 

 

I tried creating a new query of just finals from the original but now it will not allow me to merge them, the preview of the new query will not appear?

I was worried that name-complexity might be a problem. How many names are you likely to be dealing with, where are they coming from and will new ones be being added continually?

Around 6,000 but I'm really only concerned about 2,000 of them. They come from a Microsoft Form (people enter in names manually, and are not consistent when they do). Yes, they will continualy enter them in so I can't maintain any kind of master list. I do have a master list of the names but people don't always use the same name as the master list (in fact they rarely do). It's the different between saying Advertising Booking Company and ABC - sometimes they spell it out, sometimes they don't. I am going to add an optional client ID field to the form for people to fill in, but unfortunately I can't make it required, so I will keep running into this issue. 

PC2790
Community Champion
Community Champion

Try this code:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcnRyVnDOV9JR8szLLMlMzAGyFJRidWASuQWJeZVAMbfMPLCcoZGxiZ6hgQFciVswUNQ5IzEvPRWuNSIyCmJmoDOmGMwkUzNzC7hJrhWJuQU5qdhdgpBEd42FmbmpnrGBpVJsLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Company Name" = _t, Type = _t, Code = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Company Name", type text}, {"Type", type text}, {"Code", type number}}),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Changed Type", "Company Name", Splitter.SplitTextByEachDelimiter({" "}, QuoteStyle.Csv, false), {"Company Name.1", "Company Name.2"}),
    #"Grouped Rows" = Table.Group(#"Split Column by Delimiter", {"Company Name.1"}, {{"All Rows", each Table.FillUp(Table.FillDown(_,{"Code"}),{"Code"}), type table [Company Name.1=nullable text, Company Name.2=nullable text, Type=nullable text, Code=nullable number]}}),
    #"Expanded All Rows" = Table.ExpandTableColumn(#"Grouped Rows", "All Rows", {"Company Name.2", "Type", "Code"}, {"Company Name.2", "Type", "Code"}),
    Custom1 = Table.AddColumn(#"Expanded All Rows", "Company Name", each [Company Name.1]&" " &[Company Name.2]),
    #"Removed Columns" = Table.RemoveColumns(Custom1,{"Company Name.1", "Company Name.2"})
in
    #"Removed Columns"

The outcome will be like this:

PC2790_0-1649868861414.png

 

Coriel-11
Resolver I
Resolver I

I would does this in Power Query, by creating two tables and then merging them back togather as follows.
Table 1
Filter the original table down to just "type" = "Final" 

For the "Company Name" column, use split by delimiter on the first space, You'll get columns called "Company Name.1", "Company Name.2" etc.

Keep "Company Name.1" and Code and delete all the others 

 

Table 2

Create a new column that only contains the text up to that delimiter (using extract, but new columns by examples will probably do it  simply)

Then use that column to merge Table 1 into this column, joining the new column made above with "Company Name.1" column in Table 1.

Expand as necessary

 

Might give you problems if you have things like "The XYZ company". If so you might first need to do a series of Replace stages to filter these out.

Matt

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel1

Power BI Monthly Update - May 2024

Check out the May 2024 Power BI update to learn about new features.