Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
ElliotP
Post Prodigy
Post Prodigy

Unpivoting multiple columns

Morning,

 

Updated 9/6

 

 

I currently have a large number of rows which are the result of un-nesting from a json file. I would like to be unpivot two groups of rows as so instead of there simply being one long row per TransactionID there are multiple rows with the same TransactionID but different ItemName category groups.

 

At the moment, there are seven ItemName category groups (ItemName1, ItemName2) which have the same columns names, structures and types but are only differentiated by their respective ItemName, for example ItemName1, ItemName2, ItemName3.

 

I would like there to be a maximum of 7 rows for each permanent grouping, which have the same permanent column groups and only different values from the different ItemName clusters. After we've completed this in powerquery, I would then like to remove the rows which contain a "null" ItemName*ItemID value. So there could be up to seven rows where the permanent grouping of columns (will list below) remain the same, but the values in the variable columns (will list below) are filled by the values from the seven different ItemName clusters.

 

Permanent Columns (same for each row) in the unpivot:

TransactionIDlist_merchant_idlist_creator_idDateTimelist_tender_typelist_tender_namelist_tender_idlist_tender_total_money_amountlist_tender_tendered_money_amountlist_tender_change_back_money_amountlist_tender_refunded_money_amountlist_refunds_typelist_refunds_created_atlist_refunds_processed_atlist_refunds_reasonlist_refunds_refunded_money_amountlist_refunds_payment_idlist_refunds_merchant_idlist_refunds_refunded_processing_fee_money_amountlist_refunds_refunded_additive_tax_money_amountlist_refunds_refunded_inclusive_tax_money_amountlist_refunds_refunded_tip_money_amountlist_refunds_refunded_discount_money_amount

These are the first 23 columns in the excel file.

 

Variable Columns (same column names with subtle difference of number for grouping, but the values which are different for each unpivot'ed rows):

ItemName1NotesItemName1QuantityItemName1ItemName1CategoryItemName1SkuItemName1ItemIDItemName1VariationIDItem Name1DiscountAmountSales1ItemName1DiscountAmountItemName1Modifier1NameItemName1Modifier1AmountItemName1Modifier1IDlist_itemizations_itemization_typeItemName1Modifier2NameItemName1Modifier2AmountItemName1Modifier2IDItemName1Modifier3NameItemName1Modifier3AmountItemName1Modifier3IDItemName1Modifier4NameItemName1Modifier4AmountItemName1Modifier4IDItemName1Modifier5NameItemName1Modifier5Amount

ItemName1Modifier5ID

 

 

These are the next 26 columns in the excel file.

 

I have attached the pbix and the excel file.

 

In the excel file, Sheet One contains the table structure and the data examples. Sheet Two contains an example of how I would like the data to be pivoted to.

 

Pbix and Excel File Link: https://1drv.ms/f/s!At8Q-ZbRnAj8hl4QQ9aqeygF46VF

 

@MarcelBeug has posted a lot about this and I feel his solutions are the right idea and direction. I'll post links and ideas later tonight.

 

1 ACCEPTED SOLUTION

You can easily combine the different queries into 1:

 

let
    FixedParts = List.Transform({1..7}, each "Modifier"&Text.From(_))&{
        "ItemName",
        "Item Name",
        "Notes",
        "Quantity",
        "Category",
        "Sku",
        "ItemID",
        "VariationID",
        "Sales",
        "DiscountAmount",
        "Discount",
        "Name",
        "Amount",
        "ID",
        "list_itemizations_itemization_type"},

    Source = Excel.Workbook(File.Contents("C:\Users\Marcel\Documents\Forum bijdragen\Power BI Community\Elliot 20170609\unpivotdatastructure.xlsx"), null, true),
    Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true]),
    #"Replaced Value" = Table.ReplaceValue(#"Promoted Headers","NULL",null,Replacer.ReplaceValue,Table.ColumnNames(#"Promoted Headers")),

    First23columnNames = List.FirstN(Table.ColumnNames(#"Replaced Value"),23),
    Unpivot = Table.UnpivotOtherColumns(#"Replaced Value", First23columnNames, "Attribute", "Value"),

    VariableParts = Table.AddColumn(Unpivot, "VariableParts", each List.Select(Splitter.SplitTextByAnyDelimiter(FixedParts)([Attribute]), each _ <> "")),
    #"Added Custom" = Table.AddColumn(VariableParts, "FixedPart", each Text.Combine(List.Select(Splitter.SplitTextByEachDelimiter([VariableParts])([Attribute]), each _ <> ""))),
    #"Trimmed Text" = Table.TransformColumns(#"Added Custom",{{"VariableParts", Text.Combine, type text}}),
    #"Removed Columns" = Table.RemoveColumns(#"Trimmed Text",{"Attribute"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[FixedPart]), "FixedPart", "Value"),
    #"Filtered Rows" = Table.SelectRows(#"Pivoted Column", each [VariableParts] >= "1" and [VariableParts] <= "7")
in
    #"Filtered Rows"

 

Once your source data is correct, you probably won't need the last step anymore.

Specializing in Power Query Formula Language (M)

View solution in original post

12 REPLIES 12

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.