How to put measures from multiple tables into one matrix in Power BI? - matrix

I have 8 tables with data of sold products. Each table is about a unique product. In Power BI, I want to create a matrix, containing the sold quantities (values) per product (rows), per month (columns), and the number of unique customers who bought the products.
Each of the 8 tables with the sales data has the following structure. So the App ID is different for each table, but is constantly the same within a table. Example for a Cars table:
Customer ID Month App ID
29273 2020-3 1
90283 2018-5 1
55824 2016-12 1
55824 2018-10 1
55824 2021-1 1
So, a bicycle table would have the same structure, but then the App ID's would be, for example 2, in the entire table.
I have two tables that are connected with the 8 product tables in a one-to-many relationship. The Calendar table based on the Month column, and the App table based on the App ID column.
The table Calendar:
Month
2015-1
2015-2
2015-3
2015-4
2015-5
...
...
The table Apps:
ID Name
1 Cars
2 Bicycle
3 Scooter
4 ...
So, the structure is:
I created the Calendar en Apps tables so that I could use them for the matrix, but it doesn't work like I want so far. At the end, I want to create a matrix like this (where P = the number of products sold, and C = the number of customers in that month for that product):
Product 2015-1 2015-2 2015-3 2015-4 2015-5 ...
P C P C P C P C P C
Cars 3 2 5 5 7 6 2 1 4 2
Bicycle 11 9 17 14 5 5 4 4 8 6
Scooter ...
Skateboard ...
As mentioned, I made that Calendar and App table so that I can use the columns from it to fill the labels in the rows and columns. What I am unable to do is create such a 'general variable' of the number of products sold per product, and the number of customers associated with it.
Can someone explain to me how I can fill the matrix with the numbers of products (and customers) sold, so that the matrix looks like the one described above?

I think this is pretty straight forward. You actually don't need the 'Calendar' table as it only contains the same info as is already in the 'Sales' table.
You should configure the matrix like this:
Rows: 'Name' (from the 'Apps' table)
Columns: 'Month' (from the
'Sales' table)
Values:
C = Count distinct of CustomerId (from 'Sales' table) [this counts the unique customers per month and app)
P = Count of CustomerId (from 'Sales' table) [this counts the rows of the 'Sales' table which is your number of products if every row represents 1 sale)
The different aggregations (count distinct, count) can be found under the Values' options:

Related

Complex count row etl requirement

I have a requirement related as below
1-If there is employee record then count the number of rows
a-if there are four rows then follow the layout 1,
and populate the column1 and column 2 with values in report and ltrimrtrim
b- if there are three rows, then follow the layout 2,
and hardcode the column 1 and column 2 with NULL
Otherwise, look for the employee record.
Couldn't get the logic, I used the router with as if column 1 and two null the send to layout two else 1. But the requirement is different.
router transformation, if null, layout one else 2
Step 1 - Use SRT>AGG>JNR to calculate count. create new column as count_all and set to COUNT(*). Please group by proper key columns.
Step 2 - Use RTR next to split data based on your condition.
group 1- count_all =4 then follow the layout 1 and...
group 2- count_all =3 then follow the layout 2 and...
group 3 - if count <3 then do employee record.

Oracle looping query to get value plus child values from same table

I have a table which contains values in a hierarchy structure.
I was wondering if anyone knew of a query where I could loop through each row finding it's ID and then search for rows with a PARENTID of the same value. So for example:
With a table
ID PARENTID LEVEL VALUE
-------------------------------
1 0 COUNTRY USA
2 1 CITY NYC
3 1 CITY LA
4 2 TEAM GIANTS
5 2 TEAM JETS
6 3 TEAM RAMS
7 3 TEAM CHARGERS
I could start by searching for ID:2 (NYC) and from there find all teams in that city. Something like (but I do not know the total loops I'll need to do)
SELECT ID2,VALUE FROM TABLE1 WHERE PARENTID = ID1;
Gives me:
3,LA
6,RAMS
7,CHARGERS
connect by is a common way to loop through a hierarchy like that. If you add start with, you can pick a starting point in the hierarchy.
SELECT table1.*, level
FROM table1
START WITH id = 3
CONNECT BY parentid = PRIOR id;
Please note that level is an Oracle keyword which will tell you how many loops you have stepped through so far. I wouldn't recommend using it as a column name. There's some other pseudocolumns and functions you might find helpful too.

How to filter rows in a table based on values in another table in power query

I have two tables in power query.
Price table
Date Company Price
01/01/2000 A 10
01/02/2000 A 12
01/03/2000 A 15
01/01/2000 B 15
01/02/2000 B 85
01/03/2000 B 98
Size table
Date Company Size
01/06/2000 A 10
01/06/2001 A 12
01/06/2002 A 15
01/06/2000 B 15
01/06/2001 B 85
01/06/2002 B 98
In Price table, I want only to have companies which are in size table. In other words, If company C is not in the size table, I do not need that company data points in the price table. Here no need to consider the date.
In Power Query you can use the Merge Queries function to achieve that. (In the Home --> Combine section of the ribbon.
Select the Join Kind to determine which rows to keep.
In your example, create a query from the 2nd table and apply the following steps:
Remove the date and the size column
Remove duplicates
Afterwards you can join the first table with the newly created query and do a inner join. (Only keep matching entries)

Laravel 4 - save order in pivot table

I have two tables actividadsand fichas... and a pivot table actividad_ficha. I have created another column in my pivot table: orden_actividad where I would like to save the order of the 'activities' in every 'ficha'.
My main problem is that I have no idea how to create an order in my pivot table. I mean, for example, ficha 4 is related in my pivot table with actividad 1, actividad 6 and actividad 9, and I want to create the order of the activities:
ficha - actividad - orden_actividad
4 1 1
4 6 2
4 9 3
My purpose is, after that, order the activities by 'orden_actividad' and allow the user to reorder them.
Any idea how to do that?
You can add values to custom columns in your pivot table like this:
$actividad_id = /* fetch $actividad_id here */;
$orden_actividad = $ficha->actividad->count() + 1;
$ficha->actividad()->attach($actividad_id, array('orden_actividad', $orden_actividad));

Consolidating multiple rows to a single row

Is it possible to join multiple row values to a single row? The stored procedure from where I acquire the data that I use return multiple, almost identical rows except that the category-column dfferentiates for products that have been assigned multiple categories. I would like to consolidate these categories to one column, separated by new lines. Example data:
Name Article number Sales Sales Category
------------------------------------------------
Product 1 2059102-1 20520 Retailer 1
------------------------------------------------
Product 1 2059102-1 20520 Retailer 2
------------------------------------------------
Product 1 2059102-1 20520 Retailer 3
------------------------------------------------
Product 2 2059102-2 2050 Retailer 1
------------------------------------------------
Product 2 2059102-2 5302 Retailer 3
Desired result:
Name Article number Sales Sales Category
------------------------------------------------
Product 1 2059102-1 20520 Retailer 1
Retailer 2
Retailer 3
------------------------------------------------
Product 2 2059102-2 2050 Retailer 1
Retailer 3
Thank you!
Create an RDL table and setup a Grouping on the Detail section of the table with two grouping expressions (so you have a single Grouping in the Table Details, but with multiple grouping expressions). The grouping expressions should be one for each of the fields: Name, Article number.
Then put a column in the RDL table for each field (Name, Article number, Sales, Sales Category). The trick is putting a List control in the Sales Category cell. In the list add a textbox for the "Sales Category" field and I think you'll get the result you want.
There is also some nasty SQL tricks for "row concatenation", but that is not very maintainable IMHO.

Resources