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
im_92
Frequent Visitor

create new table with column values as rows

Hi all, 

 

I have a table in the following format: 

patient_iddateagedatapointvalue
11/03/202156BMI25.0
15/4/202156height182
21/1/202040BMI22.0
23/5/202040height163

 

I want to create a new table with the following format: 

patient_iddateageBMIheight
11/03/20215625.0 
15/4/202156 182
21/1/20204022.0 
23/5/202040 163

 

Is this possible with a rather simple query? 

I have already tried to create a matrix in the correct format & export it. The matrix looks exactly what I need, but after exporting, the datapoints are converted back to rows instead of columns. So I am trying to find another solution. 

 

Thanks!

 

BW, 

Imke

1 ACCEPTED SOLUTION
Ashish_Mathur
Super User
Super User

Hi,

This M code works

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"patient_id", Int64.Type}, {"date", type datetime}, {"age", Int64.Type}, {"datapoint", type text}, {"value", Int64.Type}}),
    #"Pivoted Column" = Table.Pivot(#"Changed Type", List.Distinct(#"Changed Type"[datapoint]), "datapoint", "value")
in
    #"Pivoted Column"

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

4 REPLIES 4
Ashish_Mathur
Super User
Super User

Hi,

This M code works

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"patient_id", Int64.Type}, {"date", type datetime}, {"age", Int64.Type}, {"datapoint", type text}, {"value", Int64.Type}}),
    #"Pivoted Column" = Table.Pivot(#"Changed Type", List.Distinct(#"Changed Type"[datapoint]), "datapoint", "value")
in
    #"Pivoted Column"

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
v-yingjl
Community Support
Community Support

Hi @im_92 ,

Since there are > 100 fields in [datapoint] column, it is better to use unpivot to transform data in power query instead of creating calculated columns one by one.

In addition, calculated column would not show in power query so if you want to use the calculation results in power query, you may need to create custom columns using M code to achieve the same result first.

 

Best Regards,
Community Support Team _ Yingjie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

amitchandak
Super User
Super User

@im_92 , Try Unpivot in power Query

 

https://radacad.com/pivot-and-unpivot-with-power-bi

 

Or new column in DAX

BMI = if([datapoint] ="BMI", [Value], Blank())

 

height= if([datapoint] ="height", [Value], Blank())

In practice, I have >100 datapoints, so I want to avoid creating it column per column. 
Unpivoting in power query is definitely an option, but there the issue is that I have created calculated columns in the table which I now want to reuse in the unpivoted table (but these columns are not shown in power query) 

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.