Product Sales Target - Power BI - data-science

I want to calculate the product target value for the data I was given in Power BI. I saw that the target value should be 25% of total sales for a product in the same period of the previous year.
The data I was given comprises 9 tables. The only ones that have important information for this calculation are the following:
Table Product:
Released
Model Code
Model
Family
Discontinued
1/1/2020
5
iPhone XS
IPhone
FALSE
30/5/2020
9
iPhone 13
IPhone
FALSE
...
...
...
...
...
Invoices Table:
STORE_ID
PRODUCT_ID
ORDER_NO
ORDER_QT
UNIT_PRICE
DELIVERY_COST
TAX_RATE
ORDER_DATE
DELIVERY_DATE
CUSTOMER_ID
...
...
...
...
...
...
...
...
...
Should I use a parameter for this or create a new table?

Related

Getting a sum from nested many to many relations based on another relation

I have the following table structure:
calculations
- id
persons
- id
calculations_persons
- id
- calculation_id
- person_id
assets
- id
- person_id
- type (enum: normal/stock)
- stock_id (nullable)
- value
stocks
- id
- value
the relations are like so:
calculations <- Many to Many -> persons
persons | Has Many -> assets
stocks | can Belong To -> assets
Basically, I want to get the calculations table ordered by the value of all assets, but there is a catch, the calculation of assets is based on the fact that if a asset is of type stock then it's value is based on the value of the asset * the stocks value
so if I have for example a calculation with id: 1 with personA with id 1 with assetA of value 50 and assetB of value 100 (type stock) with it's stock value being 20 and person with id 2 with assetA of value 100 the result would look like this
calculation.id, (personA.id, personB.id, ...etc persons), total value
1, (1 , 2), 2150
How would the SQL look like? I'm using postgres

Database Design: Order Items with quantities spanning multiple statuses

I have a requirement to create a database that holds orders with items for each order.
This would be the traditional table setup
Order Table
Id (pk)
CustomerId (fk)
1
1
Item Table
Id (pk)
OrderId (fk)
StatusId (fk)
Quantity
1
1
1
1000
ItemStatus Table
(Don't worry about how the data knows which status is first, second, third, etc)
Id (pk)
Name
Description
IsStart
IsTerminal
1
New
For newly created items
1
0
2
Materials Ordered
For indicating that raw materials are ordered
0
0
3
Pre-Fabrication
For receiving materials and gathering other resources
0
0
4
In Work
For indicating that the assembly process is underway
0
0
5
Staging
For preparing the items for shipping
0
0
6
Shipped
For indicating that items are complete and no longer in the facility
0
1
However
I have the requirement to take the above quantity of 1000 and break it down by status as it pertains to the business workflow.
My initial implementation looked something like this, but I wanted to reach out and see if there is a better design.
Modified Item Table
Id (pk)
OrderId (fk)
1
1
QuantityBreakdown Table
Id (pk)
OrderItemId (fk)
StatusId (fk)
Quantity
1
1
2
200
2
1
3
200
3
1
4
400
4
1
5
200
Edit
Here are some examples in layman terms to help clarify expected solution. All scenarios will be simplified by only having a single item. Also, the handling of ordering materials is out of scope; I just need to know that the item is waiting.
In these examples, we will be handling the creation process of a burger (item #1). In more advanced scenarios, we could add another ITEM such as fries (that would be item #2)
Example 1
A restaurant order is created with 1 burger. All materials needed for the assembly of the burger are on-hand; therefore, the burger will progress through the statuses with all quantities (New => Prep => Cooking => Packaged => Delivered).
Example 2
A restaurant order is created with 2 burgers. Only enough materials for one burger are on-hand; therefore, the item quantity needs to be split. Since we don't want the customer waiting, the first burger will progress through the statuses with a quantity of 1. While the second burger will have to wait in a new status called Pending. Then once the materials are available, the second burger may continue the workflow.
Well I cannot just comment to ask for clarifications.
But I would have each OrderItem be its on distinct item in an order with its own Status (status in OrderItem). So if in fact you had 4 sets of the same item, each would have its own status.
You could always group-by if you want the total # of each OrderItemId

SQL treat two entries as one

I have a table with stock codes and quantity sold, but I would like to treat 2 different stock codes as one, the reason being is that one is imported and the other one locally produced but are the same product,
lets say
Product A - Imported, Stock code is abc123
Product A - Local, Stock code is aimp563
I want to sum over the quantity sold but treat the same product with and an imported stock code and local stock code as one. Is this possible?
Okay this is what I have
tbe table looks like
Product | StockCode | QtySold
Product A - Local | prdA001loc | 100
Product A - Imported | prdAImp7Z4 | 150
SELECT Product, SUM(QtySold) FROM tblA GROUP BY StockCode, Product
But this will just return the table as is. I would like this output:
Product | QtySold
Product A | 250
I believe that you need to update your DB schema to have reflect this information however if you need some naive solution you can use the following statement
SELECT substring(product, 1 , charindex('-',product)), SUM(QtySold)
FROM tblA GROUP BY substring(product, 1 , charindex('-',product))
note that the above statement assuming that all your products name will be similar to what is mentioned inside your question

Two types of invoice numbering (2 tables with 0..1 relationship)

I have table invoices - PK is year+document_type_id+number (this is current numbering, and i can't change it). So, the data is like this:
year document_type_id number
2013 351 1
2013 351 2
2013 352 1
Now, i need to develop second type of numbering - table invoices_2 - PK is year+market_id+cash_register_id+number (this is numbering for some of invoices which is proscribed by the law), and FK is invoices_year+invoices_document_type_id+invoices_number.
invoice - invoice_2 must be 1 -> 0..1 relationship.
The problem is that in invoices_2 table i may have this (which i would like to eliminate - using some PK+FK combination?):
year market_id cash_register_id number invoices_year inovices_document_type invoices_number
2013 1 1 1 2013 351 1
2013 1 1 2 2013 351 1
As you can see, the invoice 2013-351-1 using can be added more that 1 times in invoices_2 table, which must be forbidden.
See http://www.sqlfiddle.com/#!3/6b42c/1
It would be better to put the second numbering system in its own table.
Invoice
-------
year
document_type_id
number
invoice_2_fk
...
Invoice_2
---------
invoice_2 _id
year
market_id
cash_register_id
number
The invoice 2 foreign key in the Invoice table is nullable. When it's null, there is no invoice 2. When it's a valid id, then there is an invoice 2. This is a one to zero / one relationship.

Storing a set of criteria in another table

I have a large table with sales data, useful data below:
RowID Date Customer Salesperson Product_Type Manufacturer Quantity Value
1 01-06-2004 James Ian Taps Tap Ltd 200 £850
2 02-06-2004 Apple Fran Hats Hats Inc 30 £350
3 04-06-2004 James Lawrence Pencils ABC Ltd 2000 £980
...
Many rows later...
...
185352 03-09-2012 Apple Ian Washers Tap Ltd 600 £80
I need to calculate a large set of targets from table containing values different types, target table is under my control and so far is like:
TargetID Year Month Salesperson Target_Type Quantity
1 2012 7 Ian 1 6000
2 2012 8 James 2 2000
3 2012 9 Ian 2 6500
At present I am working out target types using a view of the first table which has a lot of extra columns:
SELECT YEAR(Date)
, MONTH(Date)
, Salesperson
, Quantity
, CASE WHEN Manufacturer IN ('Tap Ltd','Hats Inc') AND Product_Type = 'Hats' THEN True ELSE False END AS IsType1
, CASE WHEN Manufacturer = 'Hats Inc' AND Product_Type IN ('Hats','Coats') THEN True ELSE False END AS IsType2
...
...
, CASE WHEN Manufacturer IN ('Tap Ltd','Hats Inc') AND Product_Type = 'Hats' THEN True ELSE False END AS IsType24
, CASE WHEN Manufacturer IN ('Tap Ltd','Hats Inc') AND Product_Type = 'Hats' THEN True ELSE False END AS IsType25
FROM SalesTable
WHERE [some stuff here]
This is horrible to read/debug and I hate it!!
I've tried a few different ways of simplifying this but have been unable to get it to work.
The closest I have come is to have a third table holding the definition of the types with the values for each field and the type number, this can be joined to the tables to give me the full values but I can't work out a way to cope with multiple values for each field.
Finally the question:
Is there a standard way this can be done or an easier/neater method other than one column for each type of target?
I know this is a complex problem so if anything is unclear please let me know.
Edit - What I need to get:
At the very end of the process I need to have targets displayed with actual sales:
Type Year Month Salesperson TargetQty ActualQty
2 2012 8 James 2000 2809
2 2012 9 Ian 6500 6251
Each row of the sales table could potentially satisfy 8 of the types.
Some more points:
I have 5 different columns that need to be defined against the targets (or set to NULL to include any value)
I have between 30 and 40 different types that need to be defined, several of the columns could contain as many as 10 different values
For point 2, if I am using a row for each permutation of values, 2 columns with 10 values each would give me 100 rows for each sales person for each month which is a lot but if this is the only way to define multiple values I will have to do this.
Sorry if this makes no sense!
If I am correct that the "Target_Type" field in the Target Table is based on the Manufacturer and the Product_Type, then you can create a TargetType table that looks like what's below and JOIN on Manufacturer and the Product_Type to get your Target_Type_Value:
ID Product_Type Manufacturer Target_Type_Value
1 Taps Tap Ltd 1
2 Hats Hats Inc 2
3 Coats Hats Inc 2
4 Hats Caps Inc 3
5 Pencils ABC Ltd 6
This should address the "multiple values for each field" problem by having a row for each possibility.