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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Nurzh
Frequent Visitor

New column based on occurrence of string from another column

Hi all, 

I would like to create a new column in a table that will show me Master Document  numbers for Project Copies. 

Example: 

Table "Document"

 

Document NumberDocument Category
61-2300-P-5090Master Document 
61-2300-P-5090-07812Project Working Copy 
61-2300-P-5090-05908PProject Working Copy 
61-2300-P-5090-05908T1Project Working Copy 
61-2300-P-5090-05908TProject Working Copy 
61-2300-P-5090-07812-21Project Working Copy 
61-2300-P-6090Master Document 
61-2300-P-6090-07812Project Working Copy 
61-2300-P-6090-07812Project Working Copy 

 

Desired New Column that will identify us the Number of Master Document of this copies: 

Document NumberDocument CategoryNew column (Master Documents)
61-2300-P-5090Master Document 61-2300-P-5090
61-2300-P-5090-07812Project Working Copy 61-2300-P-5090
61-2300-P-5090-05908PProject Working Copy 61-2300-P-5090
61-2300-P-5090-05908T1Project Working Copy 61-2300-P-5090
61-2300-P-5090-05908TProject Working Copy 61-2300-P-5090
61-2300-P-5090-07812-21Project Working Copy 61-2300-P-5090
61-2300-P-6090Master Document 61-2300-P-6090
61-2300-P-6090-07812Project Working Copy 61-2300-P-6090
61-2300-P-6090-07812Project Working Copy 61-2300-P-6090

 

The lenght of Master Documents can be different and we need to check it by Category. 

 

One more thing is that the I published this post to DAX section, and get the solution by this formula: 

 

 

 

Master Documents = 
MAXX ( 
    FILTER ( 
        'Table',
        'Table'[Document Category] = "Master Document"
            && CONTAINSSTRING ( EARLIER ( 'Table'[Document Number] ), 'Table'[Document Number] )
    ),
    'Table'[Document Number]
)

 

 

But in this table I have 2.8 million rows, 2.7 million Master Documents of it, and that's why now almost 8 hours I am waiting for the result, it just stuck on "Working on it"... 

 

I am searching if there is any other  ways to realise it in Power Query faster?

 

Thank you for any help! 

5 REPLIES 5
slorin
Super User
Super User

Just group existing column, 

 

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjPUNTI2MNAN0DU1sDRQ0lHyTSwuSS1ScMlPLs1NzStRitVBV6RrYG5haARUGlCUn5WaXKIQnl+UnZmXruCcX1CpgFWDqaWBRQDJOkIMSddCkg6QP3SNiLXFjJgAMiM1gIjREAsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Document Number" = _t, #"Document Category" = _t]),
Group = Table.Group(Source,
{"Document Category", "Document Number"},
{{"Data", each _}},
GroupKind.Local,
(x,y) => if y[Document Category]="Master Document" then 1 else 0),
RenameColumns = Table.RenameColumns(Group,{{"Document Number", "Master Documents"}}),
RemoveColumns = Table.RemoveColumns(RenameColumns,{"Document Category"}),
Expand_Data = Table.ExpandTableColumn(RemoveColumns, "Data", {"Document Number", "Document Category"}, {"Document Number", "Document Category"})
in
Expand_Data

 

Stéphane 

slorin
Super User
Super User

Hi

With Table.Group

= Table.Group(
YourSource,
{"Document Category", "Document Number"},
{{"Data", each _}},
GroupKind.Local,
(x,y) => if y[Document Category]="Master Document" then 1 else 0)

Then expand columns

 

Stéphane

Nurzh
Frequent Visitor

Hi @slorin , 

Thank you, could you please clarify it will be by adding conditional column or need to just group existing column? I mean could you please describe actions step by step?

Einomi
Helper V
Helper V

Hi,

 

Did you try to extract the 14 first characters of the column Document Number, maybe it can be faster

 

You go to the ribbon

Add column > Extract > First characters and then type in 14

Nurzh
Frequent Visitor

Yes I tried, but this will not work for all cases, because the lenght of Master Documents can be different and we need to check it by Category. 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.