Blog Post

Power BI Community Blog
2 MIN READ

Convert Single-Column Values into Structured Rows and Columns with M query

Anonymous's avatar
Anonymous
Not applicable
2 years ago

When preparing data, we may meet a scenario that needs to split all values stored in a single column into structured rows and columns. Here I will share a simple method with M query.  

Usage scenario: 

Consider we have sample table as below: 

We hope to have the following outcome: 

Operation steps: 

1.Use Group by feature to aggregate table rows based on Category group and select All Rows operation.  

This will generate a step using Table.Group function. In the formula bar, you will have:  

 

2.Modify the Table.Group function to invoke List.Split function and use current contents item count as parameter.  

 

 

List.Split(_[Field], List.Count(_[Field])/2) 

 

 

Notice:  

At the above expression '2' is a variable which means the final result has 2 groups, it can be changed if you want each group to have fewer items and you will have more groups. 

Since List.Split only support whole number as optional parameter, we also need to add Number.Round function to handle the excepted result: Number.Round(List.Count(_[Field])/2) .

 

3.Invoke above function with List.Transform to combine contents with comma. 

 

 

List.Transform(List.Split(_[Field], Number.Round(List.Count(_[Field])/2)), each Text.Combine(_,",")) 

 

 

Now in the formula bar, you will have: 

 

4.Expand the field values. 

 

5.Use 'Split column by delimiter' to expand field values to columns and rename the result column names. 

 

  

Reference links: 

How to GROUP BY or summarize rows - Power Query | Microsoft Learn 

Table.Group - PowerQuery M | Microsoft Learn 

List.Split - PowerQuery M | Microsoft Learn 

 

Author: Xiaoxin Sheng

Reviewer: Ula and Kerry

 

Updated 2 years ago
Version 3.0

4 Comments

  • JulieBABI's avatar
    JulieBABI
    Frequent Visitor

    Oh wow Xiaoxin! 
    I think that solution could work for a data modeling challenge I've solved other ways that may not be as effective!  Thank you for sharing. 

  • the same result can be obtained like this:

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("XcyxDYAwEATBVtDHTvx/RwsUYbkGEJL7JyCxNpxkxojrvddz9GjRY7bdCRcs2PC5OfEn/sSf+BN/4i/8hb/wF/7CX/iFX/iFX/iFX/iN3/iN3/iN3/8/Pw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Group = _t, Field = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Group", type text}, {"Field", type text}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"Group"}, {{"Content", (x)=> Table.FromRows( List.Split( x[Field],3),{"Col1","Col2","col3"})}}),
        #"Expanded Content" = Table.ExpandTableColumn(#"Grouped Rows", "Content", {"Col1", "Col2", "col3"}, {"Col1", "Col2", "col3"})
    in
        #"Expanded Content"
  • Anonymous's avatar
    Anonymous
    Not applicable

    thank you

  • Anonymous's avatar
    Anonymous
    Not applicable

    scqsc