Forum Discussion

Ryan_Izzan's avatar
Ryan_Izzan
Copper Contributor
Sep 14, 2022

Combine row based on criteria (Product Package)

Hi everyone,

 

I am currently doing data analysis for my restaurant and got stuck in a process.

 

I have set of data below:

 

Sales NumberMenu CategoryMenu Category DetailMenuQtyPriceSubtotal
165932508771FoodSSSSL16160061600
165932508771ExtrasSSL (PACKAGE)100
165932508771ExtrasONJ11080010800
165932508771BeverageTETS11870018700
165932508771ExtrasOKP145004500
165932508771ExtrasOBK125002500
165932665478FoodSSSSL16160061600
165932665478ExtrasSSL (PACKAGE)100
165932665478ExtrasONJ (PACKAGE)180008000
165932665478ExtrasOBK125002500

 

What i want to do is:

1. If menu have "PACKAGE" on its name, combine to the cell above that has no "PACKAGE" on its name

2. The output of the combination will be merge of menu name and sum of subtotal, everything else on the row can be deleted

 

Here is the example of the output i desired:

 

Sales NumberMenu CategoryMenu Category DetailMenuQtyPriceSubtotal
165932508771FoodSSSSL;SL16160061600
165932508771ExtrasONJ11080010800
165932508771BeverageTETS11870018700
165932508771ExtrasOKP145004500
165932508771ExtrasOBK125002500
165932665478FoodSSSSL;SL;NJ16960069600
165932665478ExtrasOBK125002500

 

My guess it can be done in power query, unfortunately i'm just beginner in that feature.

 

Please Help.

14 Replies

  • Ryan_Izzan 

    As an alternative, here is a macro solution.

    Sub MergePackage()
        Dim r As Long
        Dim n As String
        Dim s As Double
        Dim p As Long
        Application.ScreenUpdating = False
        r = Range("D" & Rows.Count).End(xlUp).Row
        Do
            p = InStr(Range("D" & r), "(PACKAGE)")
            If p Then
                Do
                    n = n & ";" & Left(Range("D" & r).Value, p - 1)
                    s = s + Range("G" & r).Value
                    Range("A" & r).Resize(1, 7).Delete Shift:=xlShiftUp
                    r = r - 1
                    If r = 1 Then Exit Do
                    p = InStr(Range("D" & r), "(PACKAGE)")
                Loop Until p = 0
                Range("D" & r).Value = Range("D" & r).Value & n
                Range("G" & r).Value = Range("G" & r).Value + s
                n = ""
                s = 0
            End If
            r = r - 1
        Loop Until r = 1
        Application.ScreenUpdating = True
    End Sub
    • Ryan_Izzan's avatar
      Ryan_Izzan
      Copper Contributor
      Thank you for your response.

      Sorry for late response, i just try it today but kinda stuck in "Group" step. it keeps loading and the size reach 1,7 GB and still increasing.

      Its my fault that i didn't mention that the table has 23 column and 20.000+ rows. As you mention before, maybe it works on small scale.

      Is there any way to solve this problem, or any better solution from my new information

      Regards,
      • Riny_van_Eekelen's avatar
        Riny_van_Eekelen
        Platinum Contributor

        Ryan_Izzan Difficult to say what's causing it. 20 thousand rows by 23 columns isn't all that spectacular.

        Can you upload or share (Onedrive or similar) a larger and more realistic data set? Not all 20000 rows though but include all columns. Anonymize the data if needed.

Resources