Forum Discussion
Summarize Multiple Columns
Hi,
I am trying to summarize multiple columns in the Origianl Table below. I would like to add up all the Amounts and summarize by the Charge Des so that I can summarize in a table similar to on one i have attached. I can't figure out the DAX formula to summarize the data. Thank you in advance.
Original Table:
| Order # | Trans Amt | Charge 1 Des | Amount 1 | Charge 2 Des | Amount 2 | Charge 3 Des | Amount 3 | Charge 4 Des | Amount 4 | Charge 5 Des | Amount 5 | Charge 6 Des | Amount 6 | Charge 7 Des | Amount 7 | ||
| 92628 | 46.21 | Fuel | 5.15 | Handle | 7.88 | Label | 1.05 | Per Discount | -25.88 | ||||||||
| 93277 | 17.42 | DAS | 2.07 | Label | 1.05 | Per Discount | -6.1 | ||||||||||
| 92956 | 50.24 | Fuel | 5.24 | Per Discount | -29.14 | Handle | 7.63 | Label | 1.05 | ||||||||
| 92956 | 0 | Fuel | 0.73 | Handle | 4 | ||||||||||||
| 92956 | 0 | Fuel | 0.73 | Return | 4 | Handle | 6 | Handle | 7.63 | ||||||||
| 92956 | 47.2 | Return | 5.01 | Per Discount | -27.38 | Handle | 7.63 | DAS | 3.25 | ||||||||
| 92956 | 44.11 | Fuel | 4.77 | Per Discount | -25.58 | Handle | 7.63 | Label | 1.05 | ||||||||
| 92956 | 0 | Fuel | 0.73 | Return | 4 | Label | 1.05 | ||||||||||
| 93274 | 61.38 | Fuel | 6.09 | Per Discount | -35.6 | Handle | 7.63 | Label | 1.05 | ||||||||
| 93180 | 41.42 | Per Discount | -24.02 | Fuel | 4.5 | DAS | 7.25 | Label | 1.05 |
Summary Table:
Order | |||
| 92628 | |||
| Trans Amt | 46.21 | ||
| Fuel | 5.15 | ||
| Handle | 7.88 | ||
| Per Discount | -25.88 | ||
| Label | 1.05 | ||
| 93277 | |||
| Trans Amt | 17.42 | ||
| Per Discount | -6.1 | ||
| Label | 1.05 | ||
| DAS | 2.07 | ||
| 92956 | |||
| Trans Amt | 141.55 | ||
| Fuel | 17.21 | ||
| Handle | 40.52 | ||
| Per Discount | -81.1 | ||
| Label | 3.15 | ||
| DAS | 3.25 | ||
| Return | 8 | ||
| 93274 | |||
| Trans Amt | 61.38 | ||
| Fuel | 6.09 | ||
| Handle | 7.63 | ||
| Per Discount | -35.6 | ||
| Label | 1.05 | ||
| 93180 | |||
| Trans Amt | 41.42 | ||
| Fuel | 4.5 | ||
| Per Discount | -24.02 | ||
| Label | 1.05 | ||
| DAS | 7.25 |
hello christiannobis
please check if this accomodate your need.
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("nVLBSgMxEP0V2XN9ZCaTzO5RKNKDB9Fj6aHqHoSyQu3+f5ONbtOaYBWGTAIz7715mfW66dhz2ywa8WAK+X7sdyE5kAspj9V2eNv14aJoY8fD9mUqJZhT6WO/v1m+f75+jMMhPG/ZxeLNIhBZVo3lCuGQl3fP4WQYveBJcRW6ByVs7pyPqg1Y8iGmV0VZB5LzsbytEcfIicyJxEBtDiPFca6FeeoP4344g5mRfekrJs2XuKLgHMzBUN0IhW0LoOmDLLhugwgoWxqBap3GwZVo/ul3wahf1ud7CWODpzTzF6aH6arCrYP/s25LbdQslJb9hxkCw3PLbJ+bbddke51kcwQ=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Order #" = _t, #"Trans Amt" = _t, #"Charge 1 Des" = _t, #"Amount 1" = _t, #"Charge 2 Des" = _t, #"Amount 2" = _t, #"Charge 3 Des" = _t, #"Amount 3" = _t, #"Charge 4 Des" = _t, #"Amount 4" = _t, #"Charge 5 Des" = _t, #"Amount 5" = _t, #"Charge 6 Des" = _t, #"Amount 6" = _t, #"Charge 7 Des" = _t, #"Amount 7" = _t]),
#"Replaced Value3" = Table.ReplaceValue(Source,".",",",Replacer.ReplaceText,{"Order #", "Trans Amt", "Charge 1 Des", "Amount 1", "Charge 2 Des", "Amount 2", "Charge 3 Des", "Amount 3", "Charge 4 Des", "Amount 4", "Charge 5 Des", "Amount 5", "Charge 6 Des", "Amount 6", "Charge 7 Des", "Amount 7"}),
#"Changed Type" = Table.TransformColumnTypes(#"Replaced Value3",{{"Order #", Int64.Type}, {"Trans Amt", type number}, {"Charge 1 Des", type text}, {"Amount 1", type number}, {"Charge 2 Des", type text}, {"Amount 2", type number}, {"Charge 3 Des", type text}, {"Amount 3", type number}, {"Charge 4 Des", type text}, {"Amount 4", type number}, {"Charge 5 Des", type text}, {"Amount 5", type number}, {"Charge 6 Des", type text}, {"Amount 6", type number}, {"Charge 7 Des", type text}, {"Amount 7", type number}}),
#"Merged Columns" = Table.CombineColumns(Table.TransformColumnTypes(#"Changed Type", {{"Amount 1", type text}}, "en-ID"),{"Charge 1 Des", "Amount 1"},Combiner.CombineTextByDelimiter(";", QuoteStyle.None),"Merged"),
#"Merged Columns1" = Table.CombineColumns(Table.TransformColumnTypes(#"Merged Columns", {{"Amount 2", type text}}, "en-ID"),{"Charge 2 Des", "Amount 2"},Combiner.CombineTextByDelimiter(";", QuoteStyle.None),"Merged.1"),
#"Merged Columns2" = Table.CombineColumns(Table.TransformColumnTypes(#"Merged Columns1", {{"Amount 3", type text}}, "en-ID"),{"Charge 3 Des", "Amount 3"},Combiner.CombineTextByDelimiter(";", QuoteStyle.None),"Merged.2"),
#"Merged Columns3" = Table.CombineColumns(Table.TransformColumnTypes(#"Merged Columns2", {{"Amount 4", type text}}, "en-ID"),{"Charge 4 Des", "Amount 4"},Combiner.CombineTextByDelimiter(";", QuoteStyle.None),"Merged.3"),
#"Merged Columns4" = Table.CombineColumns(Table.TransformColumnTypes(#"Merged Columns3", {{"Amount 5", type text}}, "en-ID"),{"Charge 5 Des", "Amount 5"},Combiner.CombineTextByDelimiter(";", QuoteStyle.None),"Merged.4"),
#"Merged Columns5" = Table.CombineColumns(Table.TransformColumnTypes(#"Merged Columns4", {{"Amount 6", type text}}, "en-ID"),{"Charge 6 Des", "Amount 6"},Combiner.CombineTextByDelimiter(";", QuoteStyle.None),"Merged.5"),
#"Merged Columns6" = Table.CombineColumns(Table.TransformColumnTypes(#"Merged Columns5", {{"Amount 7", type text}}, "en-ID"),{"Charge 7 Des", "Amount 7"},Combiner.CombineTextByDelimiter(";", QuoteStyle.None),"Merged.6"),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Merged Columns6", {"Order #"}, "Attribute", "Value"),
#"Split Column by Delimiter" = Table.SplitColumn(Table.TransformColumnTypes(#"Unpivoted Other Columns", {{"Value", type text}}, "en-ID"), "Value", Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv), {"Value.1", "Value.2"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Value.1", type text}, {"Value.2", type number}}),
#"Replace Value1" = Table.ReplaceValue(#"Changed Type1", each [Attribute], each if not Text.StartsWith([Attribute], "Trans") then "" else [Attribute], Replacer.ReplaceText,{"Attribute"}),
#"Merged Columns7" = Table.CombineColumns(Table.TransformColumnTypes(#"Replace Value1", {{"Value.2", type text}}, "en-ID"),{"Attribute", "Value.1", "Value.2"},Combiner.CombineTextByDelimiter(";", QuoteStyle.None),"Merged"),
#"Replaced Value" = Table.ReplaceValue(#"Merged Columns7"," ",":",Replacer.ReplaceText,{"Merged"}),
#"Replaced Value1" = Table.ReplaceValue(#"Replaced Value",";"," ",Replacer.ReplaceText,{"Merged"}),
#"Trimmed Text" = Table.TransformColumns(#"Replaced Value1",{{"Merged", Text.Trim, type text}}),
#"Split Column by Delimiter1" = Table.SplitColumn(#"Trimmed Text", "Merged", Splitter.SplitTextByDelimiter(" ", QuoteStyle.Csv), {"Merged.1", "Merged.2"}),
#"Changed Type2" = Table.TransformColumnTypes(#"Split Column by Delimiter1",{{"Merged.1", type text}, {"Merged.2", type number}}),
#"Replaced Value2" = Table.ReplaceValue(#"Changed Type2",":"," ",Replacer.ReplaceText,{"Merged.1"})
in
#"Replaced Value2"the M is kind of a mess, but perhaps gives you an idea where to start.
You can tweak the m-code as your preferences.
Hope this will help.Thank you.
@Hi christiannobis
Lets achieve this by using Power Query in Power BI Desktop.
Open power bi desktop and go to power query editor.
Take blank query and then go to advance editor and pest this m-code
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("nVLBSgMxEP0V2XN9ZCaTzO5RKNKDB9Fj6aHqHoSyQu3+f5ONbtOaYBWGTAIz7715mfW66dhz2ywa8WAK+X7sdyE5kAspj9V2eNv14aJoY8fD9mUqJZhT6WO/v1m+f75+jMMhPG/ZxeLNIhBZVo3lCuGQl3fP4WQYveBJcRW6ByVs7pyPqg1Y8iGmV0VZB5LzsbytEcfIicyJxEBtDiPFca6FeeoP4344g5mRfekrJs2XuKLgHMzBUN0IhW0LoOmDLLhugwgoWxqBap3GwZVo/ul3wahf1ud7CWODpzTzF6aH6arCrYP/s25LbdQslJb9hxkCw3PLbJ+bbddke51kcwQ=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Order #" = _t, #"Trans Amt" = _t, #"Charge 1 Des" = _t, #"Amount 1" = _t, #"Charge 2 Des" = _t, #"Amount 2" = _t, #"Charge 3 Des" = _t, #"Amount 3" = _t, #"Charge 4 Des" = _t, #"Amount 4" = _t, #"Charge 5 Des" = _t, #"Amount 5" = _t, #"Charge 6 Des" = _t, #"Amount 6" = _t, #"Charge 7 Des" = _t, #"Amount 7" = _t]), // Get column names that contain "Charge" and "Amount" with error handling ColumnNames = Table.ColumnNames(Source), ChargeColumns = List.Select(ColumnNames, each Text.Contains(_, "Charge") and Text.Contains(_, "Des")), AmountColumns = List.Select(ColumnNames, each Text.Contains(_, "Amount")), // Ensure we have matching pairs PairCount = List.Min({List.Count(ChargeColumns), List.Count(AmountColumns)}), PairIndexes = if PairCount > 0 then {1..PairCount} else {}, // Function to process each pair with error handling ProcessPair = (tbl as table, index as number) => let ChargeCol = try ChargeColumns{index-1} otherwise null, AmountCol = try AmountColumns{index-1} otherwise null in if ChargeCol <> null and AmountCol <> null and Table.HasColumns(tbl, {ChargeCol, AmountCol}) then try Table.CombineColumns(tbl, {ChargeCol, AmountCol}, Combiner.CombineTextByDelimiter("|", QuoteStyle.None), "Pair_" & Number.ToText(index)) otherwise tbl else tbl, // Apply to all pairs with error handling ProcessedTable = if List.Count(PairIndexes) > 0 then List.Accumulate(PairIndexes, Source, ProcessPair) else Source, // Check if we have any pairs to unpivot ProcessedColumnNames = Table.ColumnNames(ProcessedTable), PairColumnsToUnpivot = List.Select(ProcessedColumnNames, each Text.StartsWith(_, "Pair_")), // Only proceed with unpivot if we have pairs UnpivotedColumns = if List.Count(PairColumnsToUnpivot) > 0 then let IdentifierCols = {"Order #"}, ValidIdentifierCols = List.Select(IdentifierCols, each Table.HasColumns(ProcessedTable, {_})) in if List.Count(ValidIdentifierCols) > 0 then Table.UnpivotOtherColumns(ProcessedTable, ValidIdentifierCols, "Attribute", "Value") else Table.Unpivot(ProcessedTable, PairColumnsToUnpivot, "Attribute", "Value") else ProcessedTable, // Split and clean only if we have unpivoted data SplitColumns = if Table.HasColumns(UnpivotedColumns, {"Value"}) then try Table.SplitColumn(UnpivotedColumns, "Value", Splitter.SplitTextByDelimiter("|", QuoteStyle.Csv), {"Charge_Des", "Amount"}) otherwise UnpivotedColumns else UnpivotedColumns, // Filter and clean FilteredRows = if Table.HasColumns(SplitColumns, {"Charge_Des"}) then Table.SelectRows(SplitColumns, each [Charge_Des] <> null and [Charge_Des] <> "" and [Charge_Des] <> "null") else SplitColumns, #"Added Conditional Column" = Table.AddColumn(FilteredRows, "Final Amount", each if [Amount] = null then [Charge_Des] else [Amount]), #"Added Conditional Column1" = Table.AddColumn(#"Added Conditional Column", "Fina Charge_Des", each if [Attribute] = "Trans Amt" then [Attribute] else [Charge_Des]), #"Removed Columns" = Table.RemoveColumns(#"Added Conditional Column1",{"Attribute", "Charge_Des", "Amount"}), #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Final Amount", "Amount"}}), #"Changed Type1" = Table.TransformColumnTypes(#"Renamed Columns",{{"Amount", type number}, {"Fina Charge_Des", type text}}) in #"Changed Type1"Results:
You can download the answered file from here: https://limewire.com/d/Xv74q#0Jvro2iEVu
Did it work? ✔ Give a Kudo • Mark as Solution – help others too!Hi,
Using this Power Query code, transform your data into a 3 column table
let Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content], #"Added Custom" = Table.AddColumn(Source, "Custom", each Table.FromRows(List.Split(List.Skip(Record.ToList(_),2),2),{"Description","Amount"}))[[#"Order #"],[Custom]], #"Expanded Custom.1" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"Description", "Amount"}, {"Description", "Amount"}), Custom1 = Table.SelectRows(#"Expanded Custom.1", each [Description]<>null and [Amount]<>null), #"Changed Type" = Table.TransformColumnTypes(Custom1,{{"Order #", type text}, {"Description", type text}, {"Amount", type number}}) in #"Changed Type"Yu should now be able to build your desired visual.
Hope this helps.
8 Replies
- christiannobisNew Member
This is a better image of the origianl data.
- IrwanSuper User
hello christiannobis
please check if this accomodate your need.
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("nVLBSgMxEP0V2XN9ZCaTzO5RKNKDB9Fj6aHqHoSyQu3+f5ONbtOaYBWGTAIz7715mfW66dhz2ywa8WAK+X7sdyE5kAspj9V2eNv14aJoY8fD9mUqJZhT6WO/v1m+f75+jMMhPG/ZxeLNIhBZVo3lCuGQl3fP4WQYveBJcRW6ByVs7pyPqg1Y8iGmV0VZB5LzsbytEcfIicyJxEBtDiPFca6FeeoP4344g5mRfekrJs2XuKLgHMzBUN0IhW0LoOmDLLhugwgoWxqBap3GwZVo/ul3wahf1ud7CWODpzTzF6aH6arCrYP/s25LbdQslJb9hxkCw3PLbJ+bbddke51kcwQ=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Order #" = _t, #"Trans Amt" = _t, #"Charge 1 Des" = _t, #"Amount 1" = _t, #"Charge 2 Des" = _t, #"Amount 2" = _t, #"Charge 3 Des" = _t, #"Amount 3" = _t, #"Charge 4 Des" = _t, #"Amount 4" = _t, #"Charge 5 Des" = _t, #"Amount 5" = _t, #"Charge 6 Des" = _t, #"Amount 6" = _t, #"Charge 7 Des" = _t, #"Amount 7" = _t]),
#"Replaced Value3" = Table.ReplaceValue(Source,".",",",Replacer.ReplaceText,{"Order #", "Trans Amt", "Charge 1 Des", "Amount 1", "Charge 2 Des", "Amount 2", "Charge 3 Des", "Amount 3", "Charge 4 Des", "Amount 4", "Charge 5 Des", "Amount 5", "Charge 6 Des", "Amount 6", "Charge 7 Des", "Amount 7"}),
#"Changed Type" = Table.TransformColumnTypes(#"Replaced Value3",{{"Order #", Int64.Type}, {"Trans Amt", type number}, {"Charge 1 Des", type text}, {"Amount 1", type number}, {"Charge 2 Des", type text}, {"Amount 2", type number}, {"Charge 3 Des", type text}, {"Amount 3", type number}, {"Charge 4 Des", type text}, {"Amount 4", type number}, {"Charge 5 Des", type text}, {"Amount 5", type number}, {"Charge 6 Des", type text}, {"Amount 6", type number}, {"Charge 7 Des", type text}, {"Amount 7", type number}}),
#"Merged Columns" = Table.CombineColumns(Table.TransformColumnTypes(#"Changed Type", {{"Amount 1", type text}}, "en-ID"),{"Charge 1 Des", "Amount 1"},Combiner.CombineTextByDelimiter(";", QuoteStyle.None),"Merged"),
#"Merged Columns1" = Table.CombineColumns(Table.TransformColumnTypes(#"Merged Columns", {{"Amount 2", type text}}, "en-ID"),{"Charge 2 Des", "Amount 2"},Combiner.CombineTextByDelimiter(";", QuoteStyle.None),"Merged.1"),
#"Merged Columns2" = Table.CombineColumns(Table.TransformColumnTypes(#"Merged Columns1", {{"Amount 3", type text}}, "en-ID"),{"Charge 3 Des", "Amount 3"},Combiner.CombineTextByDelimiter(";", QuoteStyle.None),"Merged.2"),
#"Merged Columns3" = Table.CombineColumns(Table.TransformColumnTypes(#"Merged Columns2", {{"Amount 4", type text}}, "en-ID"),{"Charge 4 Des", "Amount 4"},Combiner.CombineTextByDelimiter(";", QuoteStyle.None),"Merged.3"),
#"Merged Columns4" = Table.CombineColumns(Table.TransformColumnTypes(#"Merged Columns3", {{"Amount 5", type text}}, "en-ID"),{"Charge 5 Des", "Amount 5"},Combiner.CombineTextByDelimiter(";", QuoteStyle.None),"Merged.4"),
#"Merged Columns5" = Table.CombineColumns(Table.TransformColumnTypes(#"Merged Columns4", {{"Amount 6", type text}}, "en-ID"),{"Charge 6 Des", "Amount 6"},Combiner.CombineTextByDelimiter(";", QuoteStyle.None),"Merged.5"),
#"Merged Columns6" = Table.CombineColumns(Table.TransformColumnTypes(#"Merged Columns5", {{"Amount 7", type text}}, "en-ID"),{"Charge 7 Des", "Amount 7"},Combiner.CombineTextByDelimiter(";", QuoteStyle.None),"Merged.6"),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Merged Columns6", {"Order #"}, "Attribute", "Value"),
#"Split Column by Delimiter" = Table.SplitColumn(Table.TransformColumnTypes(#"Unpivoted Other Columns", {{"Value", type text}}, "en-ID"), "Value", Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv), {"Value.1", "Value.2"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Value.1", type text}, {"Value.2", type number}}),
#"Replace Value1" = Table.ReplaceValue(#"Changed Type1", each [Attribute], each if not Text.StartsWith([Attribute], "Trans") then "" else [Attribute], Replacer.ReplaceText,{"Attribute"}),
#"Merged Columns7" = Table.CombineColumns(Table.TransformColumnTypes(#"Replace Value1", {{"Value.2", type text}}, "en-ID"),{"Attribute", "Value.1", "Value.2"},Combiner.CombineTextByDelimiter(";", QuoteStyle.None),"Merged"),
#"Replaced Value" = Table.ReplaceValue(#"Merged Columns7"," ",":",Replacer.ReplaceText,{"Merged"}),
#"Replaced Value1" = Table.ReplaceValue(#"Replaced Value",";"," ",Replacer.ReplaceText,{"Merged"}),
#"Trimmed Text" = Table.TransformColumns(#"Replaced Value1",{{"Merged", Text.Trim, type text}}),
#"Split Column by Delimiter1" = Table.SplitColumn(#"Trimmed Text", "Merged", Splitter.SplitTextByDelimiter(" ", QuoteStyle.Csv), {"Merged.1", "Merged.2"}),
#"Changed Type2" = Table.TransformColumnTypes(#"Split Column by Delimiter1",{{"Merged.1", type text}, {"Merged.2", type number}}),
#"Replaced Value2" = Table.ReplaceValue(#"Changed Type2",":"," ",Replacer.ReplaceText,{"Merged.1"})
in
#"Replaced Value2"the M is kind of a mess, but perhaps gives you an idea where to start.
You can tweak the m-code as your preferences.
Hope this will help.Thank you.
- DataVitalizerSuper User
To summarize your data by Order and Charge Description with totals, you first need to reshape your data so it’s easier to work with, tight now, your charges and amounts are spread across multiple columns, which makes it hard to sum them up by charge.
Here’s what to do:
Open Power Query Editor in Power BI.
Select all the columns that have Charge Descriptions and Amounts (like Charge 1 Des, Amount 1, Charge 2 Des, Amount 2, etc.).
Right-click and choose Unpivot Columns — this will turn those multiple columns into two simple columns: one for Charge Description and one for Amount.
Rename these new columns to “Charge Des” and “Amount” for clarity.
Close & apply the changes to load the reshaped data back into Power BI.
Now, create a matrix visual with:
Rows: Order # and Charge Des
Values: Sum of Amount
Add your original “Trans Amt” column as a separate field in the matrix to show the total transaction amount per order.
Did it work? 👍 A kudos would be appreciated
🟨 Mark it as a solution to help spread knowledge 💡
- RoyelSuper User
@Hi christiannobis
Lets achieve this by using Power Query in Power BI Desktop.
Open power bi desktop and go to power query editor.
Take blank query and then go to advance editor and pest this m-code
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("nVLBSgMxEP0V2XN9ZCaTzO5RKNKDB9Fj6aHqHoSyQu3+f5ONbtOaYBWGTAIz7715mfW66dhz2ywa8WAK+X7sdyE5kAspj9V2eNv14aJoY8fD9mUqJZhT6WO/v1m+f75+jMMhPG/ZxeLNIhBZVo3lCuGQl3fP4WQYveBJcRW6ByVs7pyPqg1Y8iGmV0VZB5LzsbytEcfIicyJxEBtDiPFca6FeeoP4344g5mRfekrJs2XuKLgHMzBUN0IhW0LoOmDLLhugwgoWxqBap3GwZVo/ul3wahf1ud7CWODpzTzF6aH6arCrYP/s25LbdQslJb9hxkCw3PLbJ+bbddke51kcwQ=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Order #" = _t, #"Trans Amt" = _t, #"Charge 1 Des" = _t, #"Amount 1" = _t, #"Charge 2 Des" = _t, #"Amount 2" = _t, #"Charge 3 Des" = _t, #"Amount 3" = _t, #"Charge 4 Des" = _t, #"Amount 4" = _t, #"Charge 5 Des" = _t, #"Amount 5" = _t, #"Charge 6 Des" = _t, #"Amount 6" = _t, #"Charge 7 Des" = _t, #"Amount 7" = _t]), // Get column names that contain "Charge" and "Amount" with error handling ColumnNames = Table.ColumnNames(Source), ChargeColumns = List.Select(ColumnNames, each Text.Contains(_, "Charge") and Text.Contains(_, "Des")), AmountColumns = List.Select(ColumnNames, each Text.Contains(_, "Amount")), // Ensure we have matching pairs PairCount = List.Min({List.Count(ChargeColumns), List.Count(AmountColumns)}), PairIndexes = if PairCount > 0 then {1..PairCount} else {}, // Function to process each pair with error handling ProcessPair = (tbl as table, index as number) => let ChargeCol = try ChargeColumns{index-1} otherwise null, AmountCol = try AmountColumns{index-1} otherwise null in if ChargeCol <> null and AmountCol <> null and Table.HasColumns(tbl, {ChargeCol, AmountCol}) then try Table.CombineColumns(tbl, {ChargeCol, AmountCol}, Combiner.CombineTextByDelimiter("|", QuoteStyle.None), "Pair_" & Number.ToText(index)) otherwise tbl else tbl, // Apply to all pairs with error handling ProcessedTable = if List.Count(PairIndexes) > 0 then List.Accumulate(PairIndexes, Source, ProcessPair) else Source, // Check if we have any pairs to unpivot ProcessedColumnNames = Table.ColumnNames(ProcessedTable), PairColumnsToUnpivot = List.Select(ProcessedColumnNames, each Text.StartsWith(_, "Pair_")), // Only proceed with unpivot if we have pairs UnpivotedColumns = if List.Count(PairColumnsToUnpivot) > 0 then let IdentifierCols = {"Order #"}, ValidIdentifierCols = List.Select(IdentifierCols, each Table.HasColumns(ProcessedTable, {_})) in if List.Count(ValidIdentifierCols) > 0 then Table.UnpivotOtherColumns(ProcessedTable, ValidIdentifierCols, "Attribute", "Value") else Table.Unpivot(ProcessedTable, PairColumnsToUnpivot, "Attribute", "Value") else ProcessedTable, // Split and clean only if we have unpivoted data SplitColumns = if Table.HasColumns(UnpivotedColumns, {"Value"}) then try Table.SplitColumn(UnpivotedColumns, "Value", Splitter.SplitTextByDelimiter("|", QuoteStyle.Csv), {"Charge_Des", "Amount"}) otherwise UnpivotedColumns else UnpivotedColumns, // Filter and clean FilteredRows = if Table.HasColumns(SplitColumns, {"Charge_Des"}) then Table.SelectRows(SplitColumns, each [Charge_Des] <> null and [Charge_Des] <> "" and [Charge_Des] <> "null") else SplitColumns, #"Added Conditional Column" = Table.AddColumn(FilteredRows, "Final Amount", each if [Amount] = null then [Charge_Des] else [Amount]), #"Added Conditional Column1" = Table.AddColumn(#"Added Conditional Column", "Fina Charge_Des", each if [Attribute] = "Trans Amt" then [Attribute] else [Charge_Des]), #"Removed Columns" = Table.RemoveColumns(#"Added Conditional Column1",{"Attribute", "Charge_Des", "Amount"}), #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Final Amount", "Amount"}}), #"Changed Type1" = Table.TransformColumnTypes(#"Renamed Columns",{{"Amount", type number}, {"Fina Charge_Des", type text}}) in #"Changed Type1"Results:
You can download the answered file from here: https://limewire.com/d/Xv74q#0Jvro2iEVu
Did it work? ✔ Give a Kudo • Mark as Solution – help others too! - Ashish_MathurSuper User
Hi,
Using this Power Query code, transform your data into a 3 column table
let Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content], #"Added Custom" = Table.AddColumn(Source, "Custom", each Table.FromRows(List.Split(List.Skip(Record.ToList(_),2),2),{"Description","Amount"}))[[#"Order #"],[Custom]], #"Expanded Custom.1" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"Description", "Amount"}, {"Description", "Amount"}), Custom1 = Table.SelectRows(#"Expanded Custom.1", each [Description]<>null and [Amount]<>null), #"Changed Type" = Table.TransformColumnTypes(Custom1,{{"Order #", type text}, {"Description", type text}, {"Amount", type number}}) in #"Changed Type"Yu should now be able to build your desired visual.
Hope this helps.
- v-venuppuCommunity Support
Hi christiannobis ,
Thank you for reaching out to Microsoft Fabric Community.
Thank you Ashish_Mathur Royel DataVitalizer Irwan for the prompt response.
I wanted to check if you had the opportunity to review the information provided and resolve the issue..?Please let us know if you need any further assistance.We are happy to help.
Thank you.
- v-venuppuCommunity Support
Hi christiannobis ,
May I ask if you have resolved this issue? Please let us know if you have any further issues, we are happy to help.
Thank you.
- v-venuppuCommunity Support
Hi christiannobis ,
I hope the information provided is helpful.I wanted to check whether you were able to resolve the issue with the provided solutions.Please let us know if you need any further assistance.
Thank you.