cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
TbombToronto
Frequent Visitor

Creating new rows based on previous rows

I am not sure if this can be done in Power Query (I am using Power BI).  I am looking to create some new rows based on the existing data.  It's almost like doing a bit of an unpivot but to an existing column and appending some info.  Here is the data I have:

 

EntityIdFieldIdFieldValueFieldDate
abc123123412642079441/1/2010
abc12316664326.431/1/2010
abc1231235Low Risk1/1/2010
abc123987567891/1/2010
abc123245626801/1/2010

 

Here is what I am trying to accomplish (in order to do some compares to the source system):

 

EntityIdFieldIdFieldValue
abc12312341264207944
abc1231234_FieldDate1/1/2010
abc12316664326.43
abc1231666_FieldDate1/1/2010
abc1231235Low Risk
abc1231235_FieldDate1/1/2010
abc12398756789
abc12398756_FieldDate1/1/2010
abc12324562680
abc1232456_FieldDate1/1/2010

 

Can this be done in Power Query?

1 ACCEPTED SOLUTION
Jimmy801
Super User III
Super User III

Hello @TbombToronto 

 

whenever you have a logic, there is a way to make it work in power query. What you think of using an Unpivoting-function on your last two colums? You can try this code

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSkxKNjQyVtJRApImYMrMxMjA3NIEzNE31DcyMDRQitVBVmlmZgakTIyNzPRMjHErMzI2BVI++eUKQZnF2TjVWVqYm4LMM7ewxKnGyASsxMjMwgBFTSwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [EntityId = _t, FieldId = _t, FieldValue = _t, FieldDate = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"EntityId", type text}, {"FieldId", Int64.Type}, {"FieldValue", type text}, {"FieldDate", type date}}, "en-US"),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"EntityId", "FieldId"}, "Attribute", "Value")
in
    #"Unpivoted Columns"

Jimmy801_0-1609174950243.png

Copy paste this code to the advanced editor in a new blank query to see how the solution works.


If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

 

View solution in original post

3 REPLIES 3
Jimmy801
Super User III
Super User III

Hello @TbombToronto 

 

whenever you have a logic, there is a way to make it work in power query. What you think of using an Unpivoting-function on your last two colums? You can try this code

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSkxKNjQyVtJRApImYMrMxMjA3NIEzNE31DcyMDRQitVBVmlmZgakTIyNzPRMjHErMzI2BVI++eUKQZnF2TjVWVqYm4LMM7ewxKnGyASsxMjMwgBFTSwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [EntityId = _t, FieldId = _t, FieldValue = _t, FieldDate = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"EntityId", type text}, {"FieldId", Int64.Type}, {"FieldValue", type text}, {"FieldDate", type date}}, "en-US"),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"EntityId", "FieldId"}, "Attribute", "Value")
in
    #"Unpivoted Columns"

Jimmy801_0-1609174950243.png

Copy paste this code to the advanced editor in a new blank query to see how the solution works.


If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

 

View solution in original post

Thanks, that does get me halfway there.  I guess once I am there, I can create a new column where I can derive what I ultimately need.  If the fieldid = field value then use field id otherwise concatenate fieldid + attribute.

Hello

 

Exactly. The only thing you have to pay attention that you cannot concatenate numbers and text. Use text.from to get a text value from your numbers column.

 

All the best

 

Jimmy

Helpful resources

Announcements
PBI User Groups

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group

April Update

Check it Out!

Click here to read more about the April 2021 Updates!

MBAS Carousel

Sign up for our May 4th event!

May the fourth be with you, join us online!

secondImage

The largest Power BI virtual conference

100+ sessions, 100+ speakers, Product managers, MVPs, and experts. All about Power BI. Attend online or watch the recordings.