Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

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?

  • Hello Anonymous 

     

    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"

    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

     

3 Replies

  • Jimmy801's avatar
    Jimmy801
    Community Champion

    Hello Anonymous 

     

    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"

    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

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      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.

      • Jimmy801's avatar
        Jimmy801
        Community Champion

        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