Forum Discussion

SnowBoardTim's avatar
SnowBoardTim
New Member
1 year ago
Solved

Unpivot Question

    Hello all!  I need some help in making a table friendlier to manipulate in Power BI and use their file as the source so I can capture their updates.  Above is a picture of what I am worki...
  • Anonymous's avatar
    Anonymous
    1 year ago

    Hi SnowBoardTim ,
    Thank you for the patience! If the issue is still not resolve,please refer the answer below:

    There is a limitation when importing merged columns to Power BI , the first column will reflect as is, while the other column will appear blank in the header. To avoid this issue, unmerge the columns in the header and copy the same header to all the columns manually. Then, the rows can be merged using the m code below. Please change the source as per your requirements. Attached the pbix file for reference.

    let
    Source = Table.FromRows(
    Json.Document(
    Binary.Decompress(
    Binary.FromText("i45WCs4sSVXSUXJMTs4vzSsBsgJSizLzUwzhLCM4yxjOMoGzTOEsMzjLXClWJ1rJxxEoEAHEfvqORJEgTb6pJUX5Bfk5mcWkao4FAA==", BinaryEncoding.Base64),
    Compression.Deflate
    )
    ),
    let _t = ((type nullable text) meta [Serialized.Text = true]) in
    type table [#"(blank)" = _t, #"(blank).1" = _t, #"2024 Actual W Forecast" = _t, #"2024 Actual W Forecast.1" = _t, #"2024 Actual W Forecast.2" = _t, #"2024 Actual W Forecast.3" = _t, #"2024 Actual W Forecast.4" = _t, #"2024 Actual W Forecast.5" = _t, #"2024 Actual W Forecast.6" = _t]
    ),

    OriginalHeaders = Table.ColumnNames(Source),

    //  Cleaned headers: remove .1/.2 suffixes and replace (blank) with space
    CleanedHeaders = List.Transform(
    OriginalHeaders,
    each Text.Replace(Text.BeforeDelimiter(_, "."), "(blank)", "")
    ),

    FirstRow = Source{0},
    FirstRowValues = Record.ToList(FirstRow),

    // Combine cleaned header + first row value
    CombinedHeaders = List.Transform(
    List.Zip({CleanedHeaders, FirstRowValues}),
    each Text.Combine(List.RemoveNulls(_), " ")
    ),

    DataWithoutFirstRow = Table.Skip(Source, 1),
    FinalTable = Table.RenameColumns(DataWithoutFirstRow, List.Zip({Table.ColumnNames(DataWithoutFirstRow), CombinedHeaders}))
    in
    FinalTable

    If this answer meets your requiremnets, consider accepting it as solution.

    Thank you.