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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Andy_Ng
New Member

How to "Unpivot" by column group based on condition

Hello,

I have a question that I really need help from experts.

I'm entering data for a table of values and temperatures, where:

  • The device code is used as the column name containing the value.
  • The device code paired with _T is the name of the column containing the temperature.

That table has the structure as below.

DateP01P02P01_TP02_TP03P04P03_T
1/1/20231323303727
1/2/20233422295528
1/3/20232525293326

Then I need to transform that "Input" table into a data structure like the "Output" table below but haven't found a way to do it.

DateCodeValueTemp
1/1/2023P01123
1/1/2023P02330
1/1/2023P03327
1/1/2023P047 
1/2/2023P01322
1/2/2023P02429
1/2/2023P03528
1/2/2023P045 
1/3/2023P01225
1/3/2023P02529
1/3/2023P03326
1/3/2023P043 

Please help. Thank you very much.

1 ACCEPTED SOLUTION
wdx223_Daniel
Super User
Super User

let
    Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    Custom1 = Table.UnpivotOtherColumns(Source,{"Date"},"x","y"),
    Custom2 = Table.SplitColumn(Custom1,"x",each List.FirstN(Text.Split(Text.Replace(_,"_T","_Temperature")&"_Value","_"),2),{"Name","x"}),
    Custom3 = Table.Pivot(Custom2,{"Value","Temperature"},"x","y",each _{0}?)
in
    Custom3

wdx223_Daniel_0-1700618403981.png

 

View solution in original post

4 REPLIES 4
wdx223_Daniel
Super User
Super User

let
    Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    Custom1 = Table.UnpivotOtherColumns(Source,{"Date"},"x","y"),
    Custom2 = Table.SplitColumn(Custom1,"x",each List.FirstN(Text.Split(Text.Replace(_,"_T","_Temperature")&"_Value","_"),2),{"Name","x"}),
    Custom3 = Table.Pivot(Custom2,{"Value","Temperature"},"x","y",each _{0}?)
in
    Custom3

wdx223_Daniel_0-1700618403981.png

 

Thank you, Daniel. I'm not proficient in coding, but from your code, I have grasped the concept. It's extremely interesting. Thanks a lot.

HotChilli
Super User
Super User

Unpivot all columns except date.

Split the Attribute column on '_'

Duplicate the table.

In version 1 of the table, keep only the rows that do not have a T value (Filter this from the column header)

In version 2 of the table, keep only the rows that have a T value.

Merge the tables together on date and attribute (Make sure you get the join right for your data - looks like a left join from the non-T table)

Thanks, HotChilli. The idea is very interesting, and I'll give it a try. I'd also like to share Daniel's idea:

  • Unpivot columns that are not Date; This will create two new columns, Attribute containing the column name, and Value containing the value.
  • Add a new column (e.g., named MyColumnName) with content as Temperature for data with _T and Value for data without _T.
  • Remove _T.
  • Run PivotTable again using data in the MyColumnName column as the column name and the Value column for the values.

Thank you for your help

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

Top Solution Authors
Top Kudoed Authors