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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
elisedebrie
New Member

Check if value exists, if not create row with multiple values in multiple columns

Hello all, 

I need some help with a complex Power Query that i need to set up. 

 

In the following query i have multiple lines for an itemnumber with different operationID's. I need to check if the value H-ACI and H-LAV exist as an operationId for all the items. If these operationId's do not exist, a new row(s) should be created for this item with the new operationId. In this new row we also need to set column processQuantity to 1 and ProcessTime to 0,01.

If the operationId already exists, nothing needs to happen. 

 

elisedebrie_0-1674545242287.png

 

1 ACCEPTED SOLUTION
elisedebrie
New Member

Hello all, 

It seems like there is no easy solution for this so i have created some workarounds and got the result that i needed. For everyone that is interested i have used the following code:

 

let

    Source = 

    Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],

    #"Promoted Headers" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true]),

    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"ITEMNUMBER", type text}, {"OPERATIONID", type text}, {"PROCESSQUANTITY", Int64.Type}, {"QUEUETIMEAFTER", Int64.Type}, {"QUEUETIMEBEFORE", Int64.Type}, {"ROUTETYPE", type text}, {"SETUPTIME", Int64.Type}, {"TRANSITTIME", Int64.Type}, {"RESOURCEQUANTITY", Int64.Type}, {"ROUTEGROUPID", type text}, {"PROCESSCOSTCATEGORYID", type text}, {"SETUPCOSTCATEGORYID", type text}, {"ROUTEID", type text}, {"CONSUMPTIONCALCULATIONFACTOR", Int64.Type}, {"CONSUMPTIONCALCULATIONFORMULA", type text}, {"QUANTITYCOSTCATEGORYID", type text}, {"Processtime", type number}, {"LOADPERCENTAGE", type number}}),

    #"Grouped Rows" = Table.Group(#"Changed Type", {"ITEMNUMBER", "OPERATIONID"}, {{"Count", each _, type table [ITEMNUMBER=nullable text, OPERATIONID=nullable text, PROCESSQUANTITY=nullable number, QUEUETIMEAFTER=nullable number, QUEUETIMEBEFORE=nullable number, ROUTETYPE=nullable text, SETUPTIME=nullable number, TRANSITTIME=nullable number, RESOURCEQUANTITY=nullable number, ROUTEGROUPID=nullable text, PROCESSCOSTCATEGORYID=nullable text, SETUPCOSTCATEGORYID=nullable text, ROUTEID=nullable text, CONSUMPTIONCALCULATIONFACTOR=nullable number, CONSUMPTIONCALCULATIONFORMULA=nullable text, QUANTITYCOSTCATEGORYID=nullable text, Processtime=nullable number, LOADPERCENTAGE=nullable number]}}),

    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each if ([OPERATIONID] = "H-FER") then List.Repeat({""},6)

else null),

    #"Expanded Custom" = Table.ExpandListColumn(#"Added Custom", "Custom"),

    #"Grouped Rows1" = Table.Group(#"Expanded Custom", {"ITEMNUMBER", "OPERATIONID"}, {{"Count", each _, type table [ITEMNUMBER=nullable text, OPERATIONID=nullable text, Count=table, Custom=nullable text]}}),

    #"Added Custom1" = Table.AddColumn(#"Grouped Rows1", "Custom index", each Table.AddIndexColumn([Count],"Index",1)),

    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom1",{"Custom index"}),

    #"Expanded Custom index" = Table.ExpandTableColumn(#"Removed Other Columns", "Custom index", {"ITEMNUMBER", "OPERATIONID", "Count", "Custom", "Index"}, {"ITEMNUMBER", "OPERATIONID", "Count", "Custom", "Index"}),

    #"Added Custom2" = Table.AddColumn(#"Expanded Custom index", "OperationId New", each if [Index] = 2 then "H-ACI" else if [Index] = 3 then "H-LAV" else if [Index] = 4 then "H-LEV" else if [Index] = 5 then "H-LIS" else if [Index] = 6 then "H-POL" else [OPERATIONID]),

    #"Expanded Count" = Table.ExpandTableColumn(#"Added Custom2", "Count", {"PROCESSQUANTITY", "QUEUETIMEAFTER", "QUEUETIMEBEFORE", "ROUTETYPE", "SETUPTIME", "TRANSITTIME", "RESOURCEQUANTITY", "ROUTEGROUPID", "PROCESSCOSTCATEGORYID", "SETUPCOSTCATEGORYID", "ROUTEID", "CONSUMPTIONCALCULATIONFACTOR", "CONSUMPTIONCALCULATIONFORMULA", "QUANTITYCOSTCATEGORYID", "Processtime", "LOADPERCENTAGE"}, {"PROCESSQUANTITY", "QUEUETIMEAFTER", "QUEUETIMEBEFORE", "ROUTETYPE", "SETUPTIME", "TRANSITTIME", "RESOURCEQUANTITY", "ROUTEGROUPID", "PROCESSCOSTCATEGORYID", "SETUPCOSTCATEGORYID", "ROUTEID", "CONSUMPTIONCALCULATIONFACTOR", "CONSUMPTIONCALCULATIONFORMULA", "QUANTITYCOSTCATEGORYID", "Processtime", "LOADPERCENTAGE"}),

    #"Added Custom3" = Table.AddColumn(#"Expanded Count", "ProcessQuantityNew", each if [OperationId New] = "H-ACI" then 1 else if [OperationId New] = "H-LAV" then 1 else if [OperationId New] = "H-LEV" then 1 else if [OperationId New] = "H-LIS" then 1 else if [OperationId New] = "H-POL" then 1 else [PROCESSQUANTITY]),

    #"Added Conditional Column" = Table.AddColumn(#"Added Custom3", "PROCESSTIMENew", each if [OperationId New] = "H-ACI" then 0.01 else if [OperationId New] = "H-LAV" then 0.01 else if [OperationId New] = "H-LEV" then 0.01 else if [OperationId New] = "H-LIS" then 0.01 else if [OperationId New] = "H-POL" then 0.01 else [Processtime]),

    #"Added Conditional Column1" = Table.AddColumn(#"Added Conditional Column", "LOADPERCENTAGENew", each if [OperationId New] = "H-ACI" then 0.01 else if [OperationId New] = "H-LAV" then 0.01 else if [OperationId New] = "H-LEV" then 0.01 else if [OperationId New] = "H-LIS" then 0.01 else if [OperationId New] = "H-POL" then 0.01 else [LOADPERCENTAGE])

in

    #"Added Conditional Column1"

View solution in original post

1 REPLY 1
elisedebrie
New Member

Hello all, 

It seems like there is no easy solution for this so i have created some workarounds and got the result that i needed. For everyone that is interested i have used the following code:

 

let

    Source = 

    Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],

    #"Promoted Headers" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true]),

    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"ITEMNUMBER", type text}, {"OPERATIONID", type text}, {"PROCESSQUANTITY", Int64.Type}, {"QUEUETIMEAFTER", Int64.Type}, {"QUEUETIMEBEFORE", Int64.Type}, {"ROUTETYPE", type text}, {"SETUPTIME", Int64.Type}, {"TRANSITTIME", Int64.Type}, {"RESOURCEQUANTITY", Int64.Type}, {"ROUTEGROUPID", type text}, {"PROCESSCOSTCATEGORYID", type text}, {"SETUPCOSTCATEGORYID", type text}, {"ROUTEID", type text}, {"CONSUMPTIONCALCULATIONFACTOR", Int64.Type}, {"CONSUMPTIONCALCULATIONFORMULA", type text}, {"QUANTITYCOSTCATEGORYID", type text}, {"Processtime", type number}, {"LOADPERCENTAGE", type number}}),

    #"Grouped Rows" = Table.Group(#"Changed Type", {"ITEMNUMBER", "OPERATIONID"}, {{"Count", each _, type table [ITEMNUMBER=nullable text, OPERATIONID=nullable text, PROCESSQUANTITY=nullable number, QUEUETIMEAFTER=nullable number, QUEUETIMEBEFORE=nullable number, ROUTETYPE=nullable text, SETUPTIME=nullable number, TRANSITTIME=nullable number, RESOURCEQUANTITY=nullable number, ROUTEGROUPID=nullable text, PROCESSCOSTCATEGORYID=nullable text, SETUPCOSTCATEGORYID=nullable text, ROUTEID=nullable text, CONSUMPTIONCALCULATIONFACTOR=nullable number, CONSUMPTIONCALCULATIONFORMULA=nullable text, QUANTITYCOSTCATEGORYID=nullable text, Processtime=nullable number, LOADPERCENTAGE=nullable number]}}),

    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each if ([OPERATIONID] = "H-FER") then List.Repeat({""},6)

else null),

    #"Expanded Custom" = Table.ExpandListColumn(#"Added Custom", "Custom"),

    #"Grouped Rows1" = Table.Group(#"Expanded Custom", {"ITEMNUMBER", "OPERATIONID"}, {{"Count", each _, type table [ITEMNUMBER=nullable text, OPERATIONID=nullable text, Count=table, Custom=nullable text]}}),

    #"Added Custom1" = Table.AddColumn(#"Grouped Rows1", "Custom index", each Table.AddIndexColumn([Count],"Index",1)),

    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom1",{"Custom index"}),

    #"Expanded Custom index" = Table.ExpandTableColumn(#"Removed Other Columns", "Custom index", {"ITEMNUMBER", "OPERATIONID", "Count", "Custom", "Index"}, {"ITEMNUMBER", "OPERATIONID", "Count", "Custom", "Index"}),

    #"Added Custom2" = Table.AddColumn(#"Expanded Custom index", "OperationId New", each if [Index] = 2 then "H-ACI" else if [Index] = 3 then "H-LAV" else if [Index] = 4 then "H-LEV" else if [Index] = 5 then "H-LIS" else if [Index] = 6 then "H-POL" else [OPERATIONID]),

    #"Expanded Count" = Table.ExpandTableColumn(#"Added Custom2", "Count", {"PROCESSQUANTITY", "QUEUETIMEAFTER", "QUEUETIMEBEFORE", "ROUTETYPE", "SETUPTIME", "TRANSITTIME", "RESOURCEQUANTITY", "ROUTEGROUPID", "PROCESSCOSTCATEGORYID", "SETUPCOSTCATEGORYID", "ROUTEID", "CONSUMPTIONCALCULATIONFACTOR", "CONSUMPTIONCALCULATIONFORMULA", "QUANTITYCOSTCATEGORYID", "Processtime", "LOADPERCENTAGE"}, {"PROCESSQUANTITY", "QUEUETIMEAFTER", "QUEUETIMEBEFORE", "ROUTETYPE", "SETUPTIME", "TRANSITTIME", "RESOURCEQUANTITY", "ROUTEGROUPID", "PROCESSCOSTCATEGORYID", "SETUPCOSTCATEGORYID", "ROUTEID", "CONSUMPTIONCALCULATIONFACTOR", "CONSUMPTIONCALCULATIONFORMULA", "QUANTITYCOSTCATEGORYID", "Processtime", "LOADPERCENTAGE"}),

    #"Added Custom3" = Table.AddColumn(#"Expanded Count", "ProcessQuantityNew", each if [OperationId New] = "H-ACI" then 1 else if [OperationId New] = "H-LAV" then 1 else if [OperationId New] = "H-LEV" then 1 else if [OperationId New] = "H-LIS" then 1 else if [OperationId New] = "H-POL" then 1 else [PROCESSQUANTITY]),

    #"Added Conditional Column" = Table.AddColumn(#"Added Custom3", "PROCESSTIMENew", each if [OperationId New] = "H-ACI" then 0.01 else if [OperationId New] = "H-LAV" then 0.01 else if [OperationId New] = "H-LEV" then 0.01 else if [OperationId New] = "H-LIS" then 0.01 else if [OperationId New] = "H-POL" then 0.01 else [Processtime]),

    #"Added Conditional Column1" = Table.AddColumn(#"Added Conditional Column", "LOADPERCENTAGENew", each if [OperationId New] = "H-ACI" then 0.01 else if [OperationId New] = "H-LAV" then 0.01 else if [OperationId New] = "H-LEV" then 0.01 else if [OperationId New] = "H-LIS" then 0.01 else if [OperationId New] = "H-POL" then 0.01 else [LOADPERCENTAGE])

in

    #"Added Conditional Column1"

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.