Forum Discussion

nareshr89's avatar
nareshr89
Icon for Helper II rankHelper II
4 years ago

Adding rows to an existing table in Power BI

I have a table that is used to format the KPI to a specific buckets based on header and each KPI has a sorting order number. I would like to add new rows under multiple places and want the sorting order to update automatically.

Sample data below.

 

Ex - I want to add a KPI that should be inserted in the 3rd row and the sort order should be automatically updated for the below rows.

6 Replies

  • One quick and dirty solution is to use 'Enter data' to create a new table with an intermediate index for the KPI and append that to your existing table.

     

    (Column names abbreviated for readability. Use your actual column names.)

  • mork22's avatar
    mork22
    Regular Visitor

    Go to "Transform Data"

    Select the Query where you need to add a value (in my example "Custom Values")

    Then click on the gear next to source

    In the next screen you can add new values

    Click OK to save the new values

     

  • jNilsen's avatar
    jNilsen
    Regular Visitor

    I assume the SortOrder_chr (rankChr) is fixed in a table and not counted as a relative position in that table.

    Each rank is decided relative to the Attri_pnl_f before and after according to Attr.._sortOrder where the rankChr is set at regDate.

    When you need to insert a new row there is no available chr's between rows (..."2,3,4"...) or you need to rewrite each rankChr one higher than the one you want to replace and continue the rankUp.

     

    To solve this you can replace all Attr.._sortOrder with new chrSets like "aaa,aba,aca.." or "100,200,300..." which gives you space to insert new rows and assign a new relative rankChr.

    AlfaNum will give you 25 rows for each chr, num will give you 9.

     

    Also, you can create a table wih just [Attr.._format] and [Attr.._sortOrder] and update new rows and rewrite each rankUpChr there whitch you then use to insert the rank into this table.

     

  • v-yingjl's avatar
    v-yingjl
    Icon for Community Support rankCommunity Support

    Hi nareshr89 ,

    To sort rows and re-sort them in Power Query Editor, first you need to clear that the logic to sort these rows initially, if the logic is not clear, perhaps using enter data like @ AlexisOlson would be a 'quick and dirty' solution.

     

    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.

  • Chewdata's avatar
    Chewdata
    Icon for Responsive Resident rankResponsive Resident

    Maybe the following is a solution for your problem. With Table.InsertRows you can insert an new record at a given index. Not sure if your sort column is dynamic or static. For this to work you would need to add a dynamic indexColumn for the sorting.

    See the code below for an example:

    let
        // replace the source with your data
        Source = YOURDATA,
    
        // a list with the record for the new kpi
        newKPI = {[
            Attributes_PnL_Format = "np_totalCost",
            Category = "Revenue by source",
            New_PNL_Name = "Total Cost"
        ]},
    
        // insert the record at the desired position
        add_newRecord = Table.InsertRows(
            Source, 
            1, // the position you want it to be inserted at
            newKPI // the record
            ),
    
        // create a index column that can be used for sorting
        add_sortOrder = Table.AddIndexColumn(
            add_newRecord, 
            "SortOrder", 
            0, 
            1, 
            Int64.Type
            )
    in
        add_sortOrder