PowerPivot: get latest (date-based) record in table - powerquery

I am trying to do some analysis on PowerPivot.
I have mainly 2 tables.
List of Companies and Total number of employees
Company
Total number of employees
Company A
50
Company B
10
And a "Transaction" Table about a migration, it lists how many employees were migrated on a specific date. See below. On 10.10.22 1 employee of Company A was migrated on 11.10.22 in total 2 employees were migrated. So it's always the total. And on 16.10.22 the total was 4 so 1 less than on the 15.10.22 (yeah can happen)
Company
Total Migrated employees
Date
Company A
1
10.10.22
Company A
2
11.10.22
Company A
5
15.10.22
Company A
4
16.10.22
Company B
1
15.10.22
Company B
2
16.10.22
At the end I want a table showing the progress in %, so something like this
Company
Total number of employees
Total Migrated (last day)
%
Company A
50
4
8%
Company B
10
2
20%
Any direction really appreciated.
I am thinking about Calculate and some kind of oldest function...
Thanks a lot for your help

Unclear to me if you want powerquery or powerpivot since you tagged both of them, but this is powerquery (M)
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Added Custom" = Table.AddColumn(Source,"Total Migrated (last day)",(i)=>Table.Sort(Table.SelectRows(Table2, each [Company]=i[Company]),{{"Date", Order.Descending}})[Total Migrated employees]{0},type number),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "%", each [#"Total Migrated (last day)"]/[Total number of employees],Percentage.Type)
in #"Added Custom1"

Found this - which is just what I needed:
https://www.youtube.com/watch?v=hidJ5T_DYQ0

Related

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

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:

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 can i solve this query in sql oracle?

It's an exercise that is not solved in the book in which I am studying.
The goal is to find the seller who has had the highest number of sales per month,
during all the months for which there is registered information. The problem is that I do not know how to divide tuples into periods of one month.
First table is:
Table Sellers
Id_seller
Name_Product
And the other one is:
Table Product
Name_Product
View_datetime
Budget
What did i do?
I made this query:
SELECT id_seller FROM(SELECT id_seller, COUNT(id_seller)
FROM SELLERS INNER JOIN PRODUCT
ON SELLERS.name_product = PRODUCT.name_product
GROUP BY id_seller HAVING COUNT(id_seller)>= 1
ORDER BY 2 DESC)
WHERE ROWNUM = 1;
The query returns me the seller that most sales has done, but not "per month since there are records" as the statement asks. Any ideas? I'm so lost...
The idea is to compare the total sales of each salesman in this month (sysdate), with those of a month ago, two months ago ... so long as there are older records. And get the maximum from each seller. And then you print the seller with more sales from the previous list. If a seller sells 400 products this month(April, the sysdate), but another seller sold in October last year 500, the result would be the second seller . That's what I do not know how to do.
Thanks ^^
You could try this query
select MonthName, id_seller, max(TotalSales) from (
select to_char(sysdate, 'Month') MonthName, sellers.id_seller, count(sellers.id_seller) TotalSales
from sellers inner join product
on sellers.name_product = product.name_product
group by to_char(view_datetime, 'Month'), sellers.id_seller
) tab
group by MonthName, id_seller
There are a few points to make...
The tables are weird. I assume your table sellers would better be called sales, right?
In this example, having count... >= 1 is a no-op. Count could only be 0 if there were no rows at all, in which case there would be no row in the group- by output. You can just leave this count away, here.
To get the sales per month, just add the month to the group by. I.e. group by id_seller, To_date(view_datetime,'YYYYMM').

How to add one month to month and year

I am working in oracle and new to coding and new to this site so I apologize in advance for the newbie question:
I have a script I am trying to run that will return the sum of next months' sales orders and compare that figure against our budgeted sales forecast. It was working great last month (November) when I set it up but now that it's December, I believe it's having problems figuring out that next month is a new year.
Essentially I just want to sum of our sales order records from the next month and compare that number against our forecast number.
Here is what I have so far (I'm sure I am making lots of grammatical mistakes so please be patient!)
select
"Backlog", "Forecast Amount" , round("Backlog"/"Forecast Amount",4) as "Backlog Percent"
from
(select round(sum(NVL(unit_price,0) *NVL( ship_quan,0)),2) as "Backlog"
from v_backlog_releases
where
(TO_CHAR(V_BACKLOG_RELEASES.PROMISE_DATE,'MM\YYYY') = TO_CHAR(sysdate,'MM\YYYY')+1)),
(select budamount as "Forecast Amount"
from
glbudget,
glperiods
where
glbudget.glperiods_id=glperiods.id and
TO_CHAR(GLPERIODS.START_DATE,'MM') = TO_CHAR(sysdate,'MM')+1)
The system won't let me post images of the output since I am too new. Essentially I should get something that looks like this:
Backlog | Forecast Amount | Backlog Percent
100,000 | 200,000 | .50
The backlog column is just a sum of ship quantities * price for all orders due to ship the following month.
Your issue is that for December TO_CHAR(sysdate, 'MM') + 1 is returning 13 instead of 1 of the next year. Obviously there is no month 13...
Try using ADD_MONTHS(sysdate, 1) instead and handle that result as appropriate. Best advice is to handle dates as dates instead of chars whenever possible.
Update based on comments:
Try using:
EXTRACT(MONTH FROM GLPERIODS.START_DATE) = EXTRACT(MONTH FROM ADD_MONTHS(sysdate, 1))
Documentation: https://docs.oracle.com/cd/B14117_01/server.101/b10759/functions045.htm

obiee count distinct by as part of total count distinct

I need create following "numeric distribution" report in OBIEE :
item name,
count(distinct item_id by item_name),
count(distinct item_id by item_name)/count(distinct item_id)
(third column should be percent of count distinct by item name / count distinct by total)
How to get total count distinct ? I'm stuck and i will be appreciate any help.
The result should be:
Item 1 , 10, 50%
Item 2 , 20, 100%
where total number of customers is 20, and 10 od them buy Item_1, and 20 of them buy Item_2
Click on Measure in Pivot table then click on 'show Data as'>Percent of>Column.
Thats it

Resources