Forum Discussion

k_umesh's avatar
k_umesh
Frequent Visitor
9 months ago
Solved

power query Dynamic pivoting and automate data refresh

I need to pivot JSON data and dynamically add columns during each data refresh in a Fabric semantic model. This functionality only works when I trigger a manual refresh using the Refresh button; scheduled refreshes or the Dataset Refresh API do not apply these changes.

 

Below is the one of My Power Query

 

let
    // Parameters
    CloudConnectionName = #"CloudConnectionName",   
    DatabaseName = DatabaseName,
    SchemaName = #"SchemaName",                    

    // Load source table
    Source = Sql.Database(CloudConnectionName, DatabaseName),
    TableData = Source{[Schema=SchemaName, Item="MAINTENANCE_LI_CUSTOM_ATTRIBUTES"]}[Data],

    // Parse JSON safely
    ParseJSON = Table.AddColumn(
        TableData,
        "Parsed",
        each if [JsonData] <> null and Text.Trim([JsonData]) <> ""
             then try Json.Document([JsonData]) otherwise null
             else null
    ),

    // Convert Parsed to table safely (empty table if null)
    ConvertToTable = Table.TransformColumns(
        ParseJSON,
        {"Parsed", each if _ <> null then Record.ToTable(_) else #table({"Name","Value"}, {}), type table}
    ),

    // Expand Name and Value
    ExpandKeyValue = Table.ExpandTableColumn(ConvertToTable, "Parsed", {"Name", "Value"}),

    // Remove null Name rows and ensure text types
    Cleaned = Table.SelectRows(ExpandKeyValue, each [Name] <> null),
    EnsureText = Table.TransformColumnTypes(Cleaned, {{"Name", type text}, {"Value", type text}}),

    // Get dynamic list of keys from current data only
    AvailableKeys = if Table.IsEmpty(EnsureText) then {} else List.Distinct(EnsureText[Name]),

    // Pivot dynamically based on current keys only
    MAINTENANCE_LI_CUSTOM_ATTRIBUTES = Table.Pivot(
        EnsureText,
        AvailableKeys,
        "Name",
        "Value",
        each List.First(_, null)
    )
in
    MAINTENANCE_LI_CUSTOM_ATTRIBUTES




  • Anonymous's avatar
    Anonymous
    9 months ago

    Hi k_umesh ,

    The refresh issue happens because the query uses dynamic pivoting, which means Power Query creates new columns automatically based on whatever JSON keys appear in the data. This works during manual refresh, but scheduled or API refresh cannot handle changing columns, so it fails with errors saying a column doesn’t exist. To fix this, you can use a fixed list of column names instead of letting Power Query create them dynamically, so the table structure stays the same every time. Another easy option is to avoid pivoting in Power Query and let a Matrix visual in Power BI do the pivoting, which keeps the data stable and makes scheduled refresh safe. If your data comes from a database, you can also pivot in SQL by creating a view with a stable set of columns. If you need pivoted columns in your dataset, the best way to go is to stick with a fixed list of pivot columns. That way, your schema stays consistent and you won't run into refresh issues.
    https://learn.microsoft.com/en-us/power-bi/connect-data/refresh-data#data-refresh-and-schema-refresh

5 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi k_umesh ,

    The refresh issue happens because the query uses dynamic pivoting, which means Power Query creates new columns automatically based on whatever JSON keys appear in the data. This works during manual refresh, but scheduled or API refresh cannot handle changing columns, so it fails with errors saying a column doesn’t exist. To fix this, you can use a fixed list of column names instead of letting Power Query create them dynamically, so the table structure stays the same every time. Another easy option is to avoid pivoting in Power Query and let a Matrix visual in Power BI do the pivoting, which keeps the data stable and makes scheduled refresh safe. If your data comes from a database, you can also pivot in SQL by creating a view with a stable set of columns. If you need pivoted columns in your dataset, the best way to go is to stick with a fixed list of pivot columns. That way, your schema stays consistent and you won't run into refresh issues.
    https://learn.microsoft.com/en-us/power-bi/connect-data/refresh-data#data-refresh-and-schema-refresh

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi k_umesh ,

      I hope the information provided above assists you in resolving the issue. If you have any additional questions or concerns, please do not hesitate to contact us. We are here to support you and will be happy to help with any further assistance you may need.

       

      • Anonymous's avatar
        Anonymous
        Not applicable

        Hi k_umesh ,

        I hope the above details help you fix the issue. If you still have any questions or need more help, feel free to reach out. We’re always here to support you

         

  •  scheduled refreshes or the Dataset Refresh API do not apply these changes.

    Are you getting an error message? Dynamic query?

    • k_umesh's avatar
      k_umesh
      Frequent Visitor

      Yes,Those missing columns are old pivoted columns .

      Error returned: 'The '<oii>EL3Boolean</oii>' column does not exist in the rowset