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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

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
Community Champion
Community Champion

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
Community Champion
Community Champion

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

 

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
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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

Top Solution Authors
Top Kudoed Authors