Forum Discussion
Create a new column based on distinct result
Hello Anonymous
try out this solution. It uses Table.Group and depending on your indicated conditions, applies different calculations.
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("vZPRSsMwFIZf5dDr0qUZXuzSBS8E2camiIwhYT3dDrRJTNIx38Zn8cnMxSZoI81NpclFkj/w/eTrdpstLR2eFL2tsX5lZZZnYfT2XsIUnfO6RQu3YfH58fcHZc4ZKxgbyP1M3p0NKodQawuikZZq2ktPWoWzZ/JHuHeuw2yXR5DLHnPxcKkyMvW8c6TQOVh11uiAv9AeVlafqMIqgZxHyPmY5OyaXKrmHYTFijwIaSvYNGQmGy89tqg8kANz6VEkFJlGikz/5QkWGh6pRRBHaQ/oBlh5xHD+2/D5MOhNGuj3jdk1v8Y9kvGggigmTRQeU5z3FR8TO/X33H0B", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"ORIGINAL UNIQUE REFERENCE" = _t, #"# (sequence per line item, i.e. 1,2,3) of Inserted Line " = _t, #"NEW UNIQUE REFERENCE" = _t, #"ORIGINAL (Y)" = _t, #"Customer Name" = _t, #" Spend - Expense Amount (USD) " = _t, #" AMOUNT IN QUESTION " = _t, SPECIFIC = _t, #"Current Audit Status" = _t]),
#"Renamed Columns1" = Table.RenameColumns(Source,{{" Spend - Expense Amount (USD) ", "Spend - Expense Amount (USD)"}}),
#"Renamed Columns" = Table.RenameColumns(#"Renamed Columns1",{{" AMOUNT IN QUESTION ", "AMOUNT IN QUESTION"}}),
Trans = Table.TransformColumns(#"Renamed Columns",{{"AMOUNT IN QUESTION", each Number.From(_, "en-US"), Int64.Type}, {"Spend - Expense Amount (USD)", each Number.From(_, "en-US"), Int64.Type}}),
#"Grouped Rows" = Table.Group(Trans, {"ORIGINAL UNIQUE REFERENCE"}, {{"Custom", (group)=> if List.Max(group[#"Spend - Expense Amount (USD)"])> List.Sum(group[AMOUNT IN QUESTION]) then List.Sum(group[AMOUNT IN QUESTION]) else List.Max(group[#"Spend - Expense Amount (USD)"]) , type number}})
in
#"Grouped Rows"
Copy paste this code to the advanced editor in a new blank query to see how the solution works.
If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too
Have fun
Jimmy
Hi Jimmy801
Good day.
I already tried copy-paste the codes. I already got the idea but there was a proble. See image and notes below:
From there, i have noticed that for Orig Ref 02, it showed 1500 which should be 900 only. Because the only amount with issue is 900 from the 1500USD receipted. Altho, as you can see on the Audited File I attached earlier, it came out with two rows because it resulted to a 2 audit issues.
Thanks for the help again in advance.
Echo,
- Jimmy8016 years agoCommunity Champion
Hello Anonymous
well, the first question about the code... .this is a code, created from Power Query when you input the data manually. Function is called "Enter data".
About the calculation itself. You stated this conditions
f Spend Expense Amount (USD) based on Original (Y) is greater than Sum of Amount in Question, then value is equal the Spend Expense Amount. Otherwise, Sum of Amount in Question."Spend Expense Amount of Ref_2 = 1500
Sum of Amount in question = 900 + 900 = 1800
in your condition this is untrue and therefore the second part should be applied that is Sum of Amount in Question and this is 1800
I just checked my code and I saw that I inverted the calculations.... So I can not understand how in this circumstances 900 could be a result. Can you explain how how the sum-function should work?
here the correct code
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("vZPRSsMwFIZf5dDr0qUZXuzSBS8E2camiIwhYT3dDrRJTNIx38Zn8cnMxSZoI81NpclFkj/w/eTrdpstLR2eFL2tsX5lZZZnYfT2XsIUnfO6RQu3YfH58fcHZc4ZKxgbyP1M3p0NKodQawuikZZq2ktPWoWzZ/JHuHeuw2yXR5DLHnPxcKkyMvW8c6TQOVh11uiAv9AeVlafqMIqgZxHyPmY5OyaXKrmHYTFijwIaSvYNGQmGy89tqg8kANz6VEkFJlGikz/5QkWGh6pRRBHaQ/oBlh5xHD+2/D5MOhNGuj3jdk1v8Y9kvGggigmTRQeU5z3FR8TO/X33H0B", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"ORIGINAL UNIQUE REFERENCE" = _t, #"# (sequence per line item, i.e. 1,2,3) of Inserted Line " = _t, #"NEW UNIQUE REFERENCE" = _t, #"ORIGINAL (Y)" = _t, #"Customer Name" = _t, #" Spend - Expense Amount (USD) " = _t, #" AMOUNT IN QUESTION " = _t, SPECIFIC = _t, #"Current Audit Status" = _t]), #"Renamed Columns1" = Table.RenameColumns(Source,{{" Spend - Expense Amount (USD) ", "Spend - Expense Amount (USD)"}}), #"Renamed Columns" = Table.RenameColumns(#"Renamed Columns1",{{" AMOUNT IN QUESTION ", "AMOUNT IN QUESTION"}}), Trans = Table.TransformColumns(#"Renamed Columns",{{"AMOUNT IN QUESTION", each Number.From(_, "en-US"), Int64.Type}, {"Spend - Expense Amount (USD)", each Number.From(_, "en-US"), Int64.Type}}), #"Grouped Rows" = Table.Group(Trans, {"ORIGINAL UNIQUE REFERENCE"}, {{"Custom", (group)=> if List.Max(group[#"Spend - Expense Amount (USD)"])> List.Sum(group[AMOUNT IN QUESTION]) then List.Max(group[#"Spend - Expense Amount (USD)"]) else List.Sum(group[AMOUNT IN QUESTION]) , type number}}) in #"Grouped Rows"If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too
Have fun
Jimmy