Forum Discussion

NadeemAhamed's avatar
NadeemAhamed
Helper V
2 years ago
Solved

Row Header name change for visual

Hi Everyone.   I have one table in that i have same row header name in multiple places such as Variance. When we are bringing the data into PowerBI it is considering the same name row header...
  • Alex87's avatar
    2 years ago

    This one was quite a challenge. Did it dynamically in Power Query : Pair the months and calculate variance for the second month in each pair. You do not need to rename your variances 1, 2 in the source table. Just keep it as in the initial raw data "Variance". Or even better, don't even include the row Variance. I added some tricky magic with empty space based on the number of the index to manage the naming to be always displayed "Variance" 😉 And the last Index column will help you to properly sort your table. Enjoy! 

     

     

     

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8krMU9JRMjZRitWJVnJLTQJyTM3AnLDEoszEvORUoIiREVjEN7EIJA1R61hQhNCIpFbXyACquBLIszAHc7xKQbaYmGIoNjGCyueAFEN0OpamAznmBhiKDYFCsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Period = _t, Report = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Period", type text}, {"Report", Int64.Type}}),
    
     // Filter out the "Variance" rows
        #"Filtered Rows" = Table.SelectRows(#"Changed Type", each [Period] <> "Variance"),
        
        // Add Index column
        AddIndex = Table.AddIndexColumn(#"Filtered Rows", "Index", 0, 1, Int64.Type),
    
        // Pair the months and calculate variance for the second month in each pair
        AddPairIndex = Table.AddColumn(AddIndex, "PairIndex", each Number.IntegerDivide([Index], 2)),
        Grouped = Table.Group(AddPairIndex, {"PairIndex"}, {{"AllData", each _, type table [Period = text, Report = Int64.Type, Index = Int64.Type, PairIndex = Int64.Type]}}),
        CalculateVariance = Table.AddColumn(Grouped, "Variance", each let
            pair = [AllData],
            first = pair{0}?,
            second = pair{1}?
        in
            if second <> null then second[Report] - first[Report] else null),
        ExpandedVariance = Table.ExpandTableColumn(CalculateVariance, "AllData", {"Period", "Report", "Index"}),
    
        // Create a table for variance rows
        VarianceRows = Table.SelectRows(ExpandedVariance, each [Variance] <> null),
        VarianceTable = Table.AddColumn(VarianceRows, "MyPeriod", each Text.Repeat(" ", [Index]) & "Variance"),
        VarianceTableFinal = Table.SelectColumns(VarianceTable, {"MyPeriod", "Variance", "Index", "PairIndex"}),
        #"Renamed Columns" = Table.RenameColumns(VarianceTableFinal,{{"MyPeriod", "Period"}}),
        RenamedVarianceTable = Table.RenameColumns(#"Renamed Columns",{{"Variance", "Report"}}),
        #"Removed Duplicates" = Table.Distinct(RenamedVarianceTable, {"PairIndex"}),
        #"Added to Column" = Table.TransformColumns(#"Removed Duplicates", {{"PairIndex", each _ + 0.1, type number}}),
        // Combine the original and variance tables
        RemoveVarianceColumn = Table.RemoveColumns(ExpandedVariance, {"Variance"}),
        CombinedTables = Table.Combine({RemoveVarianceColumn, #"Added to Column"}),
        SortedCombined = Table.Sort(CombinedTables,{{"PairIndex", Order.Ascending}}),
        #"Changed Type1" = Table.TransformColumnTypes(SortedCombined,{{"PairIndex", type number}}),
    
        // Final sorting and renaming
        #"Sorted Rows" = Table.Sort(#"Changed Type1",{{"PairIndex", Order.Ascending}}),
        #"Removed Columns" = Table.RemoveColumns(#"Sorted Rows",{"Index", "PairIndex"}),
        #"Changed Type2" = Table.TransformColumnTypes(#"Removed Columns",{{"Report", Int64.Type}}),
        #"Added Index" = Table.AddIndexColumn(#"Changed Type2", "Index", 0, 1, Int64.Type)
    in
        #"Added Index"

     

     

     

     

     

    If it answers your query, please mark my reply as the solution