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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
kkanda
Resolver II
Resolver II

Pivot columns in query editor

Hi All,

The following table is imported from the database to Power BI desktop.

ObjNrCharIDValue_TextValue_Nr
A0102NBR_OF_PHASES 3
A0102MAX_MVA250
A0102HIGH_VOLTAGE_RATING_KV115/0/00
A0102LOW_VOLTAGE_RATING_KV13.09/0/00
A0102XFR_CONFIGDY0
A0102HIGH_OPERATING_VOLTAGE 115
A0102LOW_OPERATING_VOLTAGE 12.5
A0104NBR_OF_PHASES 3
A0104MAX_MVA200
A0104HIGH_VOLTAGE_RATING_KV115/0/00
A0104LOW_VOLTAGE_RATING_KV13.09/0/00
A0104XFR_CONFIGDY0
A0104HIGH_OPERATING_VOLTAGE 115
A0104LOW_OPERATING_VOLTAGE 12.5

The Values are given in two columns one is text and the other is a number - the values are mutually exclusive. I want to combine them to one column. Then pivot the columns in such a way that I get the following result: 

ObjNrNBR_OF_PHASESMAX_MVAHIGH_VOLTAGE_RATING_KVLOW_VOLTAGE_RATING_KVXFR_CONFIGHIGH_OPERATING_VOLTAGELOW_OPERATING_VOLTAGE
A0102325115/0/013.09/0/0DY11512.5
A0104320115/0/013.09/0/0DY11512.5

We have another 400+ objects in the table. 

Please suggest how to go about the process in Query editor. 

1 ACCEPTED SOLUTION
SundarRaj
Solution Supplier
Solution Supplier

Hi @kkanda, you can have a look at this solution in Power Query. I'll leave the image of the ouput and M code used. The source used is the table you mentioned above. Thanks

SundarRaj_0-1749877248850.png

Here's the code used:
let
Source = #table(
{"ObjNr", "CharID", "Value_Text", "Value_Nr"},
{
{"A0102", "NBR_OF_PHASES", null, 3},
{"A0102", "MAX_MVA", null, 25},
{"A0102", "HIGH_VOLTAGE_RATING_KV", "115/0/0", 0},
{"A0102", "LOW_VOLTAGE_RATING_KV", "13.09/0/0", 0},
{"A0102", "XFR_CONFIG", "DY", 0},
{"A0102", "HIGH_OPERATING_VOLTAGE", null, 115},
{"A0102", "LOW_OPERATING_VOLTAGE", null, 12.5},
{"A0104", "NBR_OF_PHASES", null, 3},
{"A0104", "MAX_MVA", null, 20},
{"A0104", "HIGH_VOLTAGE_RATING_KV", "115/0/0", 0},
{"A0104", "LOW_VOLTAGE_RATING_KV", "13.09/0/0", 0},
{"A0104", "XFR_CONFIG", "DY", 0},
{"A0104", "HIGH_OPERATING_VOLTAGE", null, 115},
{"A0104", "LOW_OPERATING_VOLTAGE", null, 12.5}
}
),
Records = Table.TransformRows(
Source,
each _ & [Value_Nr = if [Value_Nr] = 0 then [Value_Text] else [Value_Nr]]
),
FromRec = Table.FromRecords(Records, {"ObjNr", "CharID", "Value_Nr"}),
Grouped = Table.Group(
FromRec,
{"ObjNr"},
{{"AllTable", each _, type table [ObjNr = text, CharID = text, Value_Nr = any]}}
),
Table = Table.TransformColumns(
Grouped,
{"AllTable", each Table.Pivot(_, [CharID], "CharID", "Value_Nr", each _{0})}
),
Pivoted = Table.Combine(Table[AllTable])
in
Pivoted

Sundar Rajagopalan

View solution in original post

3 REPLIES 3
kkanda
Resolver II
Resolver II

Hello Sundar,

Thank you very much for the tips. I followed the code to bring values to one column and then went through Pivot Column operation and got the following result.  I have done the Pivot on CharID and selected the option "Do not aggregate". 

kkanda_0-1749910470051.png

The generated code in the Advanced Editor is the following:

let
Source = Excel.Workbook(File.Contents("C:\temp\TestWbook.xlsx"), null, true),
TestWsh_Sheet = Source{[Item="TestWsh",Kind="Sheet"]}[Data],
#"Promoted Headers" = Table.PromoteHeaders(TestWsh_Sheet, [PromoteAllScalars=true]),
#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"ObjNr", type text}, {"CharID", type text}, {"Value_Text", type any}, {"Value_Nr", type text}}),
#"Records" = Table.TransformRows(
#"Changed Type", each _ & [Value_Nr = if [Value_Nr] = "0" then [Value_Text] else [Value_Nr]]
),
#"Converted to Table" = Table.FromList(Records, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
#"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"ObjNr", "CharID", "Value_Text", "Value_Nr"}, {"ObjNr", "CharID", "Value_Text", "Value_Nr"}),
#"Removed Columns" = Table.RemoveColumns(#"Expanded Column1",{"Value_Text"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Removed Columns",{{"Value_Nr", type text}, {"ObjNr", type text}, {"CharID", type text}}),
#"Pivoted Column" = Table.Pivot(#"Changed Type1", List.Distinct(#"Changed Type1"[CharID]), "CharID", "Value_Nr")
in
#"Pivoted Column"

 

 

 

 

 

 

@kkanda Yes, I think that is a better last step. Cheers!

Sundar Rajagopalan
SundarRaj
Solution Supplier
Solution Supplier

Hi @kkanda, you can have a look at this solution in Power Query. I'll leave the image of the ouput and M code used. The source used is the table you mentioned above. Thanks

SundarRaj_0-1749877248850.png

Here's the code used:
let
Source = #table(
{"ObjNr", "CharID", "Value_Text", "Value_Nr"},
{
{"A0102", "NBR_OF_PHASES", null, 3},
{"A0102", "MAX_MVA", null, 25},
{"A0102", "HIGH_VOLTAGE_RATING_KV", "115/0/0", 0},
{"A0102", "LOW_VOLTAGE_RATING_KV", "13.09/0/0", 0},
{"A0102", "XFR_CONFIG", "DY", 0},
{"A0102", "HIGH_OPERATING_VOLTAGE", null, 115},
{"A0102", "LOW_OPERATING_VOLTAGE", null, 12.5},
{"A0104", "NBR_OF_PHASES", null, 3},
{"A0104", "MAX_MVA", null, 20},
{"A0104", "HIGH_VOLTAGE_RATING_KV", "115/0/0", 0},
{"A0104", "LOW_VOLTAGE_RATING_KV", "13.09/0/0", 0},
{"A0104", "XFR_CONFIG", "DY", 0},
{"A0104", "HIGH_OPERATING_VOLTAGE", null, 115},
{"A0104", "LOW_OPERATING_VOLTAGE", null, 12.5}
}
),
Records = Table.TransformRows(
Source,
each _ & [Value_Nr = if [Value_Nr] = 0 then [Value_Text] else [Value_Nr]]
),
FromRec = Table.FromRecords(Records, {"ObjNr", "CharID", "Value_Nr"}),
Grouped = Table.Group(
FromRec,
{"ObjNr"},
{{"AllTable", each _, type table [ObjNr = text, CharID = text, Value_Nr = any]}}
),
Table = Table.TransformColumns(
Grouped,
{"AllTable", each Table.Pivot(_, [CharID], "CharID", "Value_Nr", each _{0})}
),
Pivoted = Table.Combine(Table[AllTable])
in
Pivoted

Sundar Rajagopalan

Helpful resources

Announcements
June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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