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
sumanthdk
New Member

Converting distinct rows to Columns based on criteria

Hi,

I have the following data set - 

 

MonthYearCategorySub CategoryValue
January2023PurchaseInventory$50.00
January2023PurchaseEquipment$200
February2023GoodsInwards$100
March2023ITProcurement$1000

 

How do I convert the above data set to - 

CategorySub CategoryJanuaryFebruaryMarchAprilMayYear  
PurchaseInventory50    2023  
PurchaseEquipment200    2023  
GoodsInwards 100   2023  
ITProcurement  1000  2023  

 

I have a table which is 20000 rows with historical data from 2010. Basically looking to categorise by different months (columns) and the category/sub category as rows. 

 

Looking for a solution in Power Query. 

 

Thank you for your help.

1 ACCEPTED SOLUTION
m_dekorte
Super User
Super User

Hi @sumanthdk 

 

This is certainly not a best practice, and exclusive to Excel reporting alone, I would say.

Nonetheless, it can be done entirely through the User Interface; here is the pattern.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8krMK00sqlTSUTIyMDIGUgGlRckZicWpQKZnXllqXkk+WNbUQClWB79y18LSzIJcoA6wLES9W2pSEaoG9/z8lGKw4eWJRWCWIVStbyLQKIRCzxCQ8UX5yaVFqVBTgSqBSmMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Month = _t, Year = _t, Category = _t, #"Sub Category" = _t, Value = _t]),
    ChType = Table.TransformColumnTypes(Source,{{"Value", Currency.Type}}),
    MergeCols = Table.CombineColumns(ChType,{"Year", "Category", "Sub Category"},Combiner.CombineTextByDelimiter("|", QuoteStyle.None),"Merged"),
    PivotCol = Table.Pivot(MergeCols, List.Distinct(MergeCols[Month]), "Month", "Value", List.Sum),
    SplitByDelimiter = Table.SplitColumn(PivotCol, "Merged", Splitter.SplitTextByDelimiter("|", QuoteStyle.Csv), {"Year", "Category", "Sub Category"})
in
    SplitByDelimiter

I hope this is helpful

View solution in original post

2 REPLIES 2
m_dekorte
Super User
Super User

Hi @sumanthdk 

 

This is certainly not a best practice, and exclusive to Excel reporting alone, I would say.

Nonetheless, it can be done entirely through the User Interface; here is the pattern.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8krMK00sqlTSUTIyMDIGUgGlRckZicWpQKZnXllqXkk+WNbUQClWB79y18LSzIJcoA6wLES9W2pSEaoG9/z8lGKw4eWJRWCWIVStbyLQKIRCzxCQ8UX5yaVFqVBTgSqBSmMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Month = _t, Year = _t, Category = _t, #"Sub Category" = _t, Value = _t]),
    ChType = Table.TransformColumnTypes(Source,{{"Value", Currency.Type}}),
    MergeCols = Table.CombineColumns(ChType,{"Year", "Category", "Sub Category"},Combiner.CombineTextByDelimiter("|", QuoteStyle.None),"Merged"),
    PivotCol = Table.Pivot(MergeCols, List.Distinct(MergeCols[Month]), "Month", "Value", List.Sum),
    SplitByDelimiter = Table.SplitColumn(PivotCol, "Merged", Splitter.SplitTextByDelimiter("|", QuoteStyle.Csv), {"Year", "Category", "Sub Category"})
in
    SplitByDelimiter

I hope this is helpful

@m_dekorte  - thank you so much. had to make some minor modifications to adjust to my working environment. but works eventually. 

as a novice to power query myself, why would you say this is not best practice? if so what would be your approach?

thanks again.

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.

Top Solution Authors
Top Kudoed Authors