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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Tob_P
Helper V
Helper V

Two values against sales, return only 1

Hi,

 

I have sales orders that are either approved or pending. A sales order in some instances can be either of those but for the purposes of a custom column, I would like to look at each Sales Order No and if it both approved and pending, then I would like all rows corresponding to that Sales Order No to return Pending, but if it only contains Approved, then return approved and if it only contains Pending, then return pending.

 

Sample data:

Sales Order NoApproved/Pending
SO707125Pending
SO707125Approved
SO707778Pending
SO707778Approved
SO707778Approved
SO707778Approved
SO707778Approved
SO707778Approved
SO707778Approved
SO708310Pending
SO708310Approved
SO708310Approved
SO708310Approved
SO708310Approved
SO711592Pending
SO711592Approved
SO711592Approved
SO744581Approved
SO744581Approved
SO779586Pending
SO779586Pending

 

Desired result...

Sales Order NoApproved/Pending
SO707125Pending
SO707125Pending
SO707778Pending
SO707778Pending
SO707778Pending
SO707778Pending
SO707778Pending
SO707778Pending
SO707778Pending
SO708310Pending
SO708310Pending
SO708310Pending
SO708310Pending
SO708310Pending
SO711592Pending
SO711592Pending
SO711592Pending
SO744581Approved
SO744581Approved
SO779586Pending
SO779586

Pending

 

Any guidance appreciated!

1 ACCEPTED SOLUTION

Using the feedback from @p45cal, here the adjusted code:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCvY3NzA3NDJV0lEKSM1LycxLV4rVQRF2LCgoyi9LTUGIm5tbYFMOEcalnO7iFsaGBlicCRXGpZxScUNDU0sjTGthwriUo4ubmJhaGJIgbm5pamGGaS2GcCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Sales Order No" = _t, #"Approved/Pending" = _t]),
    ChangedType = Table.TransformColumnTypes(Source,{{"Sales Order No", type text}, {"Approved/Pending", type text}}),
    add_score = Table.AddColumn(ChangedType, "Score", each if [#"Approved/Pending"] = "Approved" then 0 else 1, Int64.Type),
    GroupedRows = Table.Group(add_score, {"Sales Order No"}, {{"Approved", each List.Sum([Score]), type number}, {"Table", each _, type table [Sales Order No=nullable text, #"Approved/Pending"=nullable text, Score=number]}}),
    add_approvedpending = Table.AddColumn(GroupedRows, "Approved/Pending", each if [Approved] > 0 then "Pending" else "Approved", type text),
    RemovedColumns = Table.RemoveColumns(add_approvedpending,{"Sales Order No", "Approved"}),
    ExpandedTable = Table.ExpandTableColumn(RemovedColumns, "Table", {"Sales Order No"}, {"Sales Order No"})
in
    ExpandedTable

 

View solution in original post

8 REPLIES 8
p45cal
Resolver III
Resolver III

Another

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCvY3NzA3NDJV0lEKSM1LycxLV4rVQRF2LCgoyi9LTUGIm5tbYFMOEcalnO7iFsaGBlicCRXGpZxScUNDU0sjTGthwriUo4ubmJhaGJIgbm5pamGGaS2GcCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Sales Order No" = _t, #"Approved/Pending" = _t]),
    ChangedType = Table.TransformColumnTypes(Source,{{"Sales Order No", type text}, {"Approved/Pending", type text}}),
    GroupedRows = Table.Group(ChangedType, {"Sales Order No"}, {{"AllRows",each _[#"Sales Order No"]},{"Approved/Pending", each if List.Contains(_[#"Approved/Pending"],"Pending") then "Pending" else "Approved"}}),
    ExpandedAllRows = Table.ExpandListColumn(GroupedRows, "AllRows"),
    RemovedColumns = Table.RemoveColumns(ExpandedAllRows,{"AllRows"})
in
    RemovedColumns

 

ThxAlot
Super User
Super User

Easy enough with calculated column,

ThxAlot_0-1723498202482.png



Expertise = List.Accumulate(


        {Days as from Today},


        {Skills and Knowledge},


        (Current, Everyday) => Current & Day.LeanAndPractise(Everyday)


)



p45cal
Resolver III
Resolver III

@Chewdata, an observation, not a criticism; you could eliminate the Count column by reversing the 0/1 in the add_score step, don't create a count column in the GroupedRows step and change the if in the add_approvedpending step to read each if [Approved] > 0 then "Pending" else "Approved"

That is really smart, thanks!!

Chewdata
Resolver IV
Resolver IV

Hey!

This is how I would solve this.

 

  1. Create score column Approved = 1, pending = 0.
  2. Group columns, add columns: Count, sum of score and All rows.
  3. Add new Approved/Pending column. If Score = Count then approved, else Pending.
  4. Remove unnessecary columns
  5. Expand the table to get all rows back.

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCvY3NzA3NDJV0lEKSM1LycxLV4rVQRF2LCgoyi9LTUGIm5tbYFMOEcalnO7iFsaGBlicCRXGpZxScUNDU0sjTGthwriUo4ubmJhaGJIgbm5pamGGaS2GcCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Sales Order No" = _t, #"Approved/Pending" = _t]),
    ChangedType = Table.TransformColumnTypes(Source,{{"Sales Order No", type text}, {"Approved/Pending", type text}}),
    add_score = Table.AddColumn(ChangedType, "Score", each if [#"Approved/Pending"] = "Approved" then 1 else 0, Int64.Type),
    GroupedRows = Table.Group(add_score, {"Sales Order No"}, {{"Count", each Table.RowCount(_), Int64.Type}, {"Approved", each List.Sum([Score]), type number}, {"Table", each _, type table [Sales Order No=nullable text, #"Approved/Pending"=nullable text, Score=number]}}),
    add_approvedpending = Table.AddColumn(GroupedRows, "Approved/Pending", each if [Approved] = [Count] then "Approved" else "Pending", type text),
    RemovedColumns = Table.RemoveColumns(add_approvedpending,{"Sales Order No", "Count", "Approved"}),
    ExpandedTable = Table.ExpandTableColumn(RemovedColumns, "Table", {"Sales Order No"}, {"Sales Order No"})
in
    ExpandedTable

 

 

 Hopefully this is helpfull!

Using the feedback from @p45cal, here the adjusted code:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCvY3NzA3NDJV0lEKSM1LycxLV4rVQRF2LCgoyi9LTUGIm5tbYFMOEcalnO7iFsaGBlicCRXGpZxScUNDU0sjTGthwriUo4ubmJhaGJIgbm5pamGGaS2GcCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Sales Order No" = _t, #"Approved/Pending" = _t]),
    ChangedType = Table.TransformColumnTypes(Source,{{"Sales Order No", type text}, {"Approved/Pending", type text}}),
    add_score = Table.AddColumn(ChangedType, "Score", each if [#"Approved/Pending"] = "Approved" then 0 else 1, Int64.Type),
    GroupedRows = Table.Group(add_score, {"Sales Order No"}, {{"Approved", each List.Sum([Score]), type number}, {"Table", each _, type table [Sales Order No=nullable text, #"Approved/Pending"=nullable text, Score=number]}}),
    add_approvedpending = Table.AddColumn(GroupedRows, "Approved/Pending", each if [Approved] > 0 then "Pending" else "Approved", type text),
    RemovedColumns = Table.RemoveColumns(add_approvedpending,{"Sales Order No", "Approved"}),
    ExpandedTable = Table.ExpandTableColumn(RemovedColumns, "Table", {"Sales Order No"}, {"Sales Order No"})
in
    ExpandedTable

 

bhanu_gautam
Super User
Super User

@Tob_P , Here is the power query to achieve desired output

 

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    GroupedRows = Table.Group(Source, {"Sales Order No"}, {{"All Statuses", each _, type table [Sales Order No=nullable text, Approved/Pending=nullable text]}}),
    AddedCustom = Table.AddColumn(GroupedRows, "Final Status", each if List.Contains([All Statuses][Approved/Pending], "Pending") then "Pending" else "Approved"),
    ExpandedRows = Table.ExpandTableColumn(AddedCustom, "All Statuses", {"Sales Order No", "Approved/Pending"}),
    ReplacedValues = Table.ReplaceValue(ExpandedRows, each [Approved/Pending], each [Final Status], Replacer.ReplaceValue, {"Approved/Pending"}),
    RemovedColumns = Table.RemoveColumns(ReplacedValues,{"Final Status"})
in
    RemovedColumns



Did I answer your question? Mark my post as a solution! And Kudos are appreciated

Proud to be a Super User!




LinkedIn






Hi @bhanu_gautam 

 

Thank you for this - have tried within my original .pbix file and on a new file with just the sample data and there seems to be an error at AddedCustom = Table.AddColumn(GroupedRows, "Final Status", each if List.Contains([All Statuses][Approved/Pending], "Pending") then "Pending" else "Approved"),

...the highlighted section. It doesn't recognise it as a column. Can I ask if all the steps are in the correct order?

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.

Top Solution Authors