Joining tables in Powerpivot - sql-server

I have two tables: 'Sales' and 'Planed sales'. They represent sales data for about 80 different products and planed sales for those products across two months for every day. This is how they look:
Table 'Sales'
productName
Sold
Date
product_A
10
01.01.2022.
product_B
15
01.01.2022.
product_A
11
02.01.2022.
product_B
20
02.01.2022.
And then table 'Planed sales'
productName
Planed sales
Date
product_A
10
01.01.2022.
product_B
14
01.01.2022.
product_A
9
02.01.2022.
product_B
15
02.01.2022.
Product names are same in both tables and so are the dates, only 'Sold' and 'Planed sales' data differs.
I somehow need to use these two tables in a SQL query for Power Pivot in Excel while I'm importing data so I can get a table looking something like this:
productName
Planed sales 01.01.
Sold 01.01.
Planned sales 02.01.
Sold 02.01.
product_A
10
10
9
11
product_B
14
15
15
20
I have lots of different products and a lot of dates for at least two months.
I tried to just import both of these tables in Excels power pivot with a simple 'select *' query, and then create a relationship, but I don't have unique values because productName is displayed for every date. Unique key here would be 'productName' + 'Date', but I don't know how to add composite key in Excels Power Pivot if that is the solution for this. I know data is not well normalized but I can't change it much, it is what it is. Am I approaching this problem correctly? Is this even possible? If it is, how could I do it?

You need a Product dimension. There's a lot of help on the web on dimensional modeling for Power BI and PowerPivot. It's worth learning.
Add a table that is just a list of all the unique productNames and then add a relationship from this new table to your 2 existing tables.
If you have a data source with the product list, use that. If you don't, combine the two tables and remove duplicates. You can do this in either DAX or Power Query, but Power Query would be the better way to do this.
In Power Query, append the 2 tables (Home > Append Queries), then right click the productName column and Remove Other Columns, and right click again to Remove Duplicates.
Then go into PowerPivot to create the relationships. In your pivot, use productName from the Product dimension, not from the other 2 tables. Best practice is to right click the productName column in those 2 tables and hide them to make sure the correct table is used.

Related

How to get the missing courses for each employee in Transact SQL (SQL Server)?

I currently have the information of all the employees and the courses they've taken, what I need is to have both the data of who have taken the courses and who hasn't in Power BI. The idea is to measure how is the organization doing with the trainings and to be able to speak with bosses to arrange trainings to for the employees who haven't taken them.
This is the structure right now:
Table Employees
ID_Employee Names Age Job title Department ...
1 Robert 44 Chief Procurement Officer Procurement ...
2 James 32 Warehouse Supervisor Operations ...
3 Natalie 47 Sales Manager Commercial ...
4 John 29 Planning Executive Operations ...
5 Matthew 33 Finance Executive Finance ...
Table Courses
ID_Course Course
1 Cybersecurity
2 Effective Negotiation
3 Safety Policies
4 Performance Analysis
Table EmployeexCourses
ID_Employee ID_Course Date_Taken
1 1 20201015
1 3 20201018
2 4 20201020
3 2 20201020
3 3 20201018
4 1 20201015
What I'm not sure is how to proceed, I can easily get who has taken each course because that's a table itself (EmployeesxCourses) and I was using a Pivot Table to have each employee in EmployeexCourses with all the courses in columns with their dates as values. I want to use this data in Power BI, so I have a question, what should I do to have the most flexible data in Power BI? Should I do a Pivot table with all the employees and all the courses (this number is small and is fixed for a while), it'd be a 1500 rows table and there would be multiple rows with no values in the columns (employees who've not taken any course). I'm a bit confused in the how of this table with all employees, because I'd need to use 'EmployeexCourses' and 'Employees' and then turn it into a Pivot Table (I'm sure I can I just haven't done it). Or should I get a separate table for all the employees with their missing courses?
Which option is better? Or do you think of another option? I need it to be efficient and flexible in Power BI for easy use (filter by area, by course missing and such). Any idea is welcome, TIA.

MS SQL Server, arrays, looping, and inserting qualified data into a table

I've searched around for answer and I'm not sure how best to frame the question since I'm rather new to SQL Server.
Here's what I got going on: I get a weekly report detailing the products that have been sold and the quantity of each. This data needs to go into a yearly totals table. In this table the first column is the product_id and the next 52 columns are week numbers, 1-52.
There's a JOIN that runs on the product_id of both the weekly and yearly tables. That finds the proper row and column to put the weekly quantity data for that product.
Here's where I'm not sure what to do. In 2019 there are no product_id in that column. So there's nothing to JOIN on. Those product_id need to be added weekly if they aren't there. I need to take the weekly report of product_id and quantity and check each product_id to see if it's in the yearly table. If not I need to add it.
If I had it my way I'd create an array of the product_id numbers from the weekly data and loop through each one creating a new record in the yearly table for any product_id that is not already there. I don't know how best to do that in SSMS.
I've searched around and have found different strategies for this. Nothing strikes me as being a perfect solution. There's creating a #temp table variable, a UNION using exclude to get just those that aren't in the table, and a WHILE loop. Any suggestions would be helpful.
I ended up using a MERGE to solve this. I create a table WeeklyParts to dump the weekly data into. Then I do a MERGE with the yearly table inserting only those where the is no match. Works well.
-- Merge the PartNo's so that only unique ones are added to the yearly table
MERGE INTO dbo.WeeklySales2018
USING dbo.WeeklyParts
ON (dbo.WeeklySales2018.PartNo = dbo.WeeklyParts.PartNo)
WHEN NOT MATCHED THEN
INSERT (PartNo) VALUES (dbo.WeeklyParts.PartNo);

Product revenue cube (using SSAS) too big and slow browsing

Got a question regarding big size SSAS cube:
Scenario: .. building product revenue cube on SQL Server Analysis Server (SSAS) 2012:
the product hierarchy is a dimension with 7 levels: cat1, cat2, … cat7(product_id);
The fact table is at product id level, key = (customer id + bill month + product id), this gives around 200M rows per month, may still go up.
Concern: Now the prototype is having only 3 month data, the cube browsing are slow already. When drag and drop 2 -3 dimensions, it might take couple of minutes or more.
Late on, there are 30+ other dimensions need to add in, and extend to 1-2 years data, 2-3B rows. So we are concerning the performance, can that much data be handled with an acceptable browsing speed?
Question: Is there any better way (design, performance tuning) for above scenario?
E.g. another thinking is to make the fact table flat, i.e. make it customer level, key = (customer id + bill month), one customer only have one row per bill month. While adding 40+ columns, one column for each product, that way the fact row count per month with go down to say 5M. But we can’t build/browse the product dimension anymore (that is an important requirement), can we?
Thanks in advance for any light shedding.

Access 2010 Database - Count number of instances

I'm a complete Access noob and I'm creating a database to keep track of orders shipped for work. I've got two tables in the database, one keeping track of the units shipped and the other keeping track of each purchase order and how many more items until that order can be closed. What I want to do is, basically, like a COUNTIF function from Excel on the entries from Table A and transfer to Table B. An example:
Table A has:
PO123
PO123
PO234
PO123
What I want Table 2 to do is count the number of instances of each PO and display the count in a field, like so:
Table B:
Row 1 Field 1: PO123
Row 1 Field 2: 3
Row 2 Field 1: PO234
Row 2 Field 2: 1
Anyone have any ideas? Any help is greatly appreciated.
You didn't say what the name of the field was, so I'm going to assume it's POTYPE. You need to create a query in Access, and go to the SQL view. Then you can do a query like
Select POTYPE, Count(POTYPE) From TableA Group By POTYPE

Merge two tables into a single indexed view in SQL Server 2008

So here is my dilemma, I'm currently storing all my records into a giant flat table that has movies, episodes, series, games etc. 70% of the records are episodes that I do NOT want indexed by the default full-text catalog as they make up over 2 million of the records and 90% of the time people are searching for movies/series, so I want to move these records into a separate table so they can have their own separate full-text catalog and a few additional columns that do not apply to movies/series (season, episode # etc.) as well.
My issue is with stored procedure I use to checkTable if an ID already exists before I go and download data or update data for that ID.
I wanted to create a view of all the ID's across both tables so I could lookup in a single place if that ID exists, however when I try to union the tables it will not allow me to create an index on the view.
ID Type | ID Type
1 movie | 2 episode
3 movie | 4 episode
5 movie | 6 episode
The ID's are unique and will never have duplicates in either table.
I feel that since this view will have over 2 million records an indexed might be important being it is called over 250-500+ a second so optimization is a huge factor.
Is there a better approach than using UNION ALL to get ID's 1,2,3,4,5,6 into a single view?
Probably your best solution is to create a view that does UNION ALL of the two tables, but instead of trying to index the view, make sure you have a good indexing strategy implemented on the tables themselves.
You can use Index Hints in your view to force the view to use those indexes whenever someone selects from the view.

Resources