Inventory management with parent child relationship

Hi all,

I have created an app for inventory management. I have two tables: ITEMS and INVENTORY LOG. I have to record the number of boxes, how many inner boxes are there and how many units are there in each inner box to complete the stock take.

For example, in the ITEMS table, I have created a master item A (consists of 10 bags) containing inner item B (each bag contains 100 items from item C) which is further consists of item C

I have setup two fields: Inner Qty and Child SKU Child SKU refers to the same ITEMS table

For Item A, Child SKU is Item B and for Item B, Child SKU is Item C.

I need to add up all the amounts and get the total qty for Item C since that is the core unit. Anyone can help me achieve this?

1 Like

Hi @Paras_Sood ,

Great question! Take a look at our Inventory Management App

Here is the Expression to use to calculate the amount of inventory remaining for any items in your database.

You will want to add a virtual column and insert this expression as a formula.

SUM(
  SELECT(
    Inventory[Amount],
      ([Item ID] = [_THISROW].[ID])
  )
)

Let me know if this helps or if you have other questions, I’ll do my best to help out in any way that I can.

Thanks,

Austin from AppSheet Training

1 Like

Hey Austin!

Thank you for your help but I need some parent-child level calculations. Let me explain my use case better with an actual example:

We send out milk chocolate peanuts in 90g packets. As per my previous example in the earlier post, let’s say that Milk Chocolate peanuts is 90g. Now, we put 4 of these packets in an inner carton. Let that inner carton be item B.

Now, for shipping purposes, we store 8 such inner cartons inside a shipper box. Let the shipper carton be item C.

Therefore, if I have say 10 cartons of the shipper in stock, which is item C, that would mean that I have 10 x 4 x 8 of 90g milk chocolate peanuts, and 10 x 4 x 8 x 90g of Milk chocolate peanuts (by weight)

How can I create this sort of relationship?

Hope I have explained my question better

I tried to create a “Parent” record for each item which is a ref_rows function, consisting of all parent records where the item is a child.

I used the following formula:

[Temp Total Stock Available]+SUM([Parent][Temp Total Stock Available]*[Parent][Inner Qty in UoM])

Hasn’t worked yet. Still trying to figure out how to achieve this.

Temp Total Stock available is a virtual column

When I test it out individually, I can see that the correct values for the expressions individually are fetched, but when i use the above expression I do not get the correct output. Please find screenshots below:

[Parent][Temp Total Stock Available]

[Parent][Inner Qty in UoM]

[Temp Total Stock Available]+SUM([Parent][Temp Total Stock Available]*[Parent][Inner Qty in UoM])

I fixed this by creating another virtual column for calculating ([Temp Total Stock Available]*[Inner Qty in UoM]) and then replacing that in the above expression. Works as expected.

1 Like

Okay coming back to this, now another problem arises:

This works for 2 tier calculations (ie Item A containing Item B)

However, the link gets broken when trying to calculate item C, because the virtual column “Child Calculation” was calculating Temp Total Stock Available * Inner Qty, and Temp Total Stock is the actual stock at hand for the parent. Now temp total stock will have some value for item A and that can be used to calculate value for Item B, but for item B the actual stock at hand is zero since it is only present inside of item A.

This has been captured in the field [Total Stock of Item incl ship], so I thought that I should replace this value in child calculation (Formula earlier was Temp Total Stock Available * Inner Qty now changed to total stock of item incl ship * inner qty) but now the app doesn’t load, it takes too long. Any suggestions?

Item A

Item B

Hi @Paras_Sood ,

Thanks for reaching out again! These are great questions. Couple of steps I would take to help get your App running correctly.

  1. Data Structure

What are the main entities/components for your app? (i.e. ITEMS, INVENTORY)

What information would you like to record from these entities? (i.e. these will be your columns - ID, Name, QTY, etc.)

  1. Expressions

How are you current calculating Total Stock Amount? (i.e. before AppSheet)

If we can get the data structured correctly then I think the expressions will be able to run much more efficiently.

Also, just asking these questions so I can have a better understanding of the app. Once I have a better understanding I can advise on some next steps for you.

Thanks again for already detailing some great information about your app build. Looking forward to helping you with this app.

Thanks,

Austin from AppSheet Training

1 Like

Hi Austin

Thank you for your reply!

Let me break down the two tables I have for you: Items and Inventory

Table # 1 Items:

Item ID, Barcode, Category, Brand, Size, Name, Description, Image, Notes, Child SKU (if exists), Standard UoM, Valuation UoM, Price per UoM.

I also have:

  • Inner Qty: which represents how many units of child item are inside of this item
  • Total Stock Available: uses the formula you mentioned earlier
  • Parent: REF_ROWS(“Items”,“Child SKU”)
  • Child Calculation: to calculate how many units of child item are there in the total stock eg item A has 8 units of item B inside it and there are 7 units of item A available so this calculation would result in 56 units.
  • Total Stock of Item incl ship: To calculate the total stock including stock inside of parent items. [Total Stock Available]+SUM([Parent][Child calculation])

Formula for total stock available:

SUM(
SELECT(
Inventory Log[Amount Var],
([Item ID] = [_THISROW].[Item ID])
)
)

Formula for child calculation: [Inner Qty in UoM]*[Temp Total Stock Available]

Table #2 Inventory:

Inventory ID, Item ID (Ref), DateTime, Qty, Amount Var, Action, Notes

Qty is where we record the qty irrespective of whether we want to add to stock or pull from stock.

Formula for Amount Var: IF ([Action] = “Add Stock”,[Qty in UoM],(0-[Qty in UoM]))

The problem is with the formula I have used to calculate Total Stock of Item incl ship, because this will work when I have stock of item A and want to know how much stock I have available for item B, but if I have another level of packaging inside of Item B (ie Item C) then this formula fails because Total Stock Available for Item B is zero and therefore child calculation for item B would also be zero. I need like a temporary placeholder. Should be an easy fix but I can’t wrap my head around it.

For sure! I think I am understanding the situation now. Thanks for the detailed reply!

I think using an IF() expression would be a great way to solve this calculation.

It might look something like this:

IF(
ISNOTBLANK([Child SKU]), 
([Total Stock Available]+SUM([Parent][Child calculation]), 
[Total Stock Available])
)

Okay I think this should work. What it is saying is IF the Child SKU IS NOT BLANK then run the calculation on line 3, IF Child SKU IS BLANK then run only total stock available calculation.

Let me know if this helps or if you need anything else.

Thanks!

Thank you for the answer, Austin but unfortunately that’s not quite what I’m looking for. Let me explain my example in more detail:

If I store 15 units of item C inside 1 unit of item B, and I store 12 units of item B inside of 1 unit of item A, then when I run my calculations, I want to see two different values:

  1. Actual Stock Available: To see how many individual units of item A, B and C I have independent of whether they are inside anything.

  2. Total Stock Available: To see how many units of Item C I have including whatever is present in item A and in item B.

Further expanding on my example, if I have the following actual stock available:

Item A (contains 12 units of item B): 5 units

Item B (contains 15 units of item C): 3 units

Item C: 4 units

I would expect to see the following values in total stock available:

Item A (Child SKU - Item B): 5 units

Item B (Child SKU - Item C): 3 + 12 x 5 = 63 units

Item C (Child SKU: Blank): 4 + 63 x 15 = 949 units

Using the formula you have provided, the results I would get right now for total stock available are:

Item A (Child SKU - Item B): 5 units

Item B (Child SKU - Item C): 3 + 15 x 5 = 78 units

Item C (Child SKU - Blank): 4 units

I hope this makes sense and I appreciate you helping me out, thank you so much.

I have written a piece of Python code to depict what I was suggesting as the solution, but for some reason this doesn’t work in appsheet, probably because it has no provision for recognising objects. But this is kind of what I am going for.

class item:
	def __init__(self, name, UoM, inner, exact_stock, create=True):
		self.total_stock = 0
		self.exact_stock = exact_stock
		self.UoM = UoM
		self.name = name
		self.inner = inner
		self.parent = None
		self.child =  None

	def set_child_parent(self,child=None):

		# child = item(create) if child is None else child #if no child then create new child otherwise put child item. by default 
		self.child = child
		self.child.parent = self

	def calculate_total_stock(self):
		if self.parent:
			temp = self.parent.total_stock * self.parent.inner
			print("Temp value for "+self.name+" = "+str(temp))
			self.total_stock = self.exact_stock + temp
		else:
			self.total_stock = self.exact_stock
		return self.total_stock

a = item("item A","pcs", 12, 5)
b = item("item B","pcs", 15, 3)
c = item("item C","pcs", 45, 4)

a.set_child_parent(b)
b.set_child_parent(c)

print("Name: "+a.name+", Child: "+a.child.name+", Inner Qty:"+str(a.inner)+" "+a.child.UoM+", Exact Stock: "+str(a.exact_stock))
print("Name: "+b.name+", Child: "+b.child.name+", Parent: "+b.parent.name+", Inner Qty:"+str(b.inner)+" "+b.child.UoM+", Exact Stock: "+str(b.exact_stock))
print("Name: "+c.name+", Parent: "+c.parent.name+", Inner Qty:"+str(c.inner)+", Exact Stock: "+str(c.exact_stock))

print(a.name+", Total Stock: "+str(a.calculate_total_stock()))
print(b.name+", Total Stock: "+str(b.calculate_total_stock()))
print(c.name+", Total Stock: "+str(c.calculate_total_stock()))

Output:

Name: item A, Child: item B, Inner Qty:12 pcs, Exact Stock: 5
Name: item B, Child: item C, Parent: item A, Inner Qty:15 pcs, Exact Stock: 3
Name: item C, Parent: item B, Inner Qty:45, Exact Stock: 4
item A, Total Stock: 5
Temp value for item B = 60
item B, Total Stock: 63
Temp value for item C = 945
item C, Total Stock: 949

I tried to apply this fix by applying the following changes:

[Child Calculation] = [Total Stock of Item incl ship] * [Inner Qty in UoM]

[Total Stock of Item incl ship] = IF(ISNOTBLANK([Parent]),[Temp Total Stock Available]+SUM([Parent][Child calculation]),[Temp Total Stock Available])

However this makes the app to not load, even though there is no infinite recursion. But how else can I achieve this?

@Steve do you have any suggestions? Sorry guys I’m in a bit of a fix with this task. Appreciate your help

I’m afraid I don’t have the time to spend to understand your problem–it looks very complex.

Hi @Paras_Sood ,

Thanks for expanding on your app build for me. I think the best way we could help is to setup a Tech Talk with @Cale_QREW here: https://calendly.com/cale-9/tech-talk. He can help with setting you up with an AppSheet Consultant from our team to help build out these features in your app.

Hope this helps, let me know if you have any other questions.

Thanks,

Austin from AppSheet Training

Thanks for your suggestion but unfortunately I am based in Sydney, and the meeting timings available are between 1am to 4:30am which is impossible for me :slightly_smiling_face:

Is it possible for you to provide me with an email?