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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
grengifo
Frequent Visitor

split by delimiter - dynamic number of columns

Hi @ImkeF

 

I was wondering if you could help me with the following problem.

 

I created a table that has invoice number in the first column and item number in the second column. Basically it is showing all the items that were purchased in the same invoice.

 

 pic1.jpg

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

What I need is a file that shows only one row per invoice number and the query should show the item numbers in columns as shown below

 

pic2.jpg

 

 

 

 

 

 

 

 

 

The problem is that Power Query limited the number of columns in the split by delimiter step to 3. In the case of invoice 5, it has 5 different products in it (17,18,19, 60 and 61) but the query only shows 17, 18 and 19 and ignored products 60 and 61

 

I could have manually changed the maximum number of columns to 5 but it is cumbersome to having to do this manually every time new data gets uploaded. I cannot figure out a way to make the split process dynamic so that it adds the necessary number of columns depending on the maximum number of items in the invoices.

 

Here is the query:

 

let


Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"invoice", Int64.Type}, {"item", Int64.Type}}),
#"Removed Duplicates" = Table.Distinct(#"Changed Type"),
#"Sorted Rows" = Table.Sort(#"Removed Duplicates",{{"invoice", Order.Ascending}, {"item", Order.Ascending}}),
#"Grouped Rows" = Table.Group(#"Sorted Rows", {"invoice"}, {{"Count", each Table.RowCount(_), type number}, {"AllItems", each _, type table}}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.Column([AllItems],"item")),
#"Extracted Values" = Table.TransformColumns(#"Added Custom", {"Custom", each Text.Combine(List.Transform(_, Text.From), ";"), type text}),
#"Removed Columns" = Table.RemoveColumns(#"Extracted Values",{"Count"}),
#"Split Column by Delimiter" = Table.SplitColumn(#"Removed Columns", "Custom", Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv), {"Custom.1", "Custom.2", "Custom.3"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Custom.1", Int64.Type}, {"Custom.2", Int64.Type}, {"Custom.3", Int64.Type}})


in


#"Changed Type1"

 

 

Thank you very much for your time and consideration.

2 ACCEPTED SOLUTIONS
ImkeF
Super User
Super User

You can add a clustered index. Pivot on it then and it will serve as the column names/index.

 Please see this function here: https://github.com/ImkeF/M/blob/master/Library/Table.ClusteredIndex.pq

 

Let me know if you need help applying this function.

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

Hi @ImkeF

 

Thank you very much for your response.

 

I actually used your solution in the following post and video and I applied them to my particular situation successfully.

 

Custom-column-Index-or-Ranking-by-other-column/m-p/33883#M12117

 

https://youtu.be/-3KFZaYImEY

 

Thank you very much!!

View solution in original post

2 REPLIES 2
ImkeF
Super User
Super User

You can add a clustered index. Pivot on it then and it will serve as the column names/index.

 Please see this function here: https://github.com/ImkeF/M/blob/master/Library/Table.ClusteredIndex.pq

 

Let me know if you need help applying this function.

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Hi @ImkeF

 

Thank you very much for your response.

 

I actually used your solution in the following post and video and I applied them to my particular situation successfully.

 

Custom-column-Index-or-Ranking-by-other-column/m-p/33883#M12117

 

https://youtu.be/-3KFZaYImEY

 

Thank you very much!!

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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