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

We've captured the moments from FabCon & SQLCon that everyone is talking about, and we are bringing them to the community, live and on-demand. Starts on April 14th. Register now

Reply
nicm-cc
Frequent Visitor

Pivoting Key Value pairs data

I have SQL data which I am reading into PowerBI. The data has a column 'Hits' which has a list of key value pairs in each row. 
I need to extract the key-value pairs to unique columns based on the key.

Currently I have this M code which does the job:

 

let
Source = Sql.Database("xxxxx.database.windows.net", "CCAnalytics", [Query="SELECT * FROM CallPhraseHits cph INNER JOIN Calls c ON cph.CallId = c.CallId WHERE c.CustomerId = '" & Text.From(DataSource) & "';"]),

// Add a custom step to decompress the "Hits" column and convert it to a table
#"Decompress Data" = Table.TransformColumns(Source, {{"Hits", each Text.FromBinary(Binary.Decompress(_, Compression.GZip)), type text}}),

//Split the KeyValuePairColumn into a row for each Key-Value pair
#"Split into rows" = Table.ExpandListColumn(Table.TransformColumns(#"Decompress Data", {{"Hits", Splitter.SplitTextByDelimiter(",", QuoteStyle.None), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Hits"),

//Replace Unused Characters
#"Replaced Value" = Table.ReplaceValue(#"Split into rows","[{","",Replacer.ReplaceText,{"Hits"}),
#"Replaced Value1" = Table.ReplaceValue(#"Replaced Value"," {","",Replacer.ReplaceText,{"Hits"}),
#"Replaced Value2" = Table.ReplaceValue(#"Replaced Value1","}]","",Replacer.ReplaceText,{"Hits"}),
#"Replaced Value3" = Table.ReplaceValue(#"Replaced Value2","}","",Replacer.ReplaceText,{"Hits"}),

//Split Key and Value into separate columns
#"Split KeyValue" = Table.SplitColumn(#"Replaced Value3", "Hits", Splitter.SplitTextByDelimiter(":", QuoteStyle.Csv), {"Key", "Value"}),

// Format Key Value Columns
#"Changed Decimal" = Table.ReplaceValue(#"Split KeyValue",".",",",Replacer.ReplaceText,{"Value"}),
// Format Value Columns
#"Convert to Number" = Table.TransformColumnTypes(#"Changed Decimal",{{"Key", type text}, {"Value", type number}}),

//Pivot Key into column-name and Value into its value
PivotColumn = Table.Pivot(#"Convert to Number", List.Distinct(#"Convert to Number"[Key]), "Key", "Value"),


in 

Pivot Column

 


This works fine for small datasets but anything larger than a couple thousand rows basically becomes unusable. The bottleneck is definitely the Table.Pivot operation. 
Is there a better way to dynamically extract the Key-value pairs into columns?
P.S this is an embedded powerbi solution so I cannot make use of Scripts (Python or R) which would have been ideal.

2 REPLIES 2
rubayatyasmin
Community Champion
Community Champion

Hi, @nicm-cc 

 

how about adding table.buffer() before pivoting the entire table of split the tables into several chunks then perform the pivot then append the result? Though it's a workaround that you will have to do but it may solve your problem. 

 

check the #"Convert to Number" step supports query folding. Query folding.

 

Also, is it possible to extract the key value pair in the sql source? if so then give it a shot. 

 

rubayatyasmin_0-1689517080227.png


Did I answer your question? Mark my post as a solution!super-user-logo

Proud to be a Super User!


Hi rubayatyasmin,

 

The query does not support query folding as it uses an advanced sql select statement.

I am working on trying to extract a select view from the source itself but this has not proved easy.

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.