Forum Discussion

bartus1's avatar
bartus1
Helper I
9 years ago
Solved

Check for empty table before expandRecordColumn

Greetings,   My function gets a list of records per pEventID.  All works fine when a record is not empty, so it can expand the table - #"Expanded Column1"   Now, when this hits an empty record, I...
  • MarcelBeug's avatar
    MarcelBeug
    9 years ago

    It was not clear that you had an empty table, I understood you had empty nested records in Column1,

     

    In any case, you can use function Table.IsEmpty to check if the table is empty.

     

    Another wild shot: if the table is empty and you don't have any columns, you can still create a table with Column1 with the record structure you would have if there would be a record, but without data. This will still let you expand Column1 and end up with the 2 columns, but without data.

     

    Just to illustrate how it works: the code below has 2 Source steps, of which 1 must be commented (currently the first line).

    The first will create a table with 1 row; the second will create an empty table.

     

    let
    //    Source = #table(1,{{[EventItemId = 1, EventItemDescription = "Description"]}}),
        Source = Table.FromList({}),
        CheckEmpty = if Table.IsEmpty(Source) then #table(type table[Column1 = [EventItemId = any, EventItemDescription = any]],{}) else Source,
        #"Expanded Column1" = Table.ExpandRecordColumn(CheckEmpty, "Column1", {"EventItemId", "EventItemDescription"}, {"EventItemId", "EventItemDescription"})
    in
        #"Expanded Column1"