How to accumulate a column based on a condition - powerquery

I am trying to go through a list and mark parents so I can join an flat list onto it self
ID Type Idx
200 EPIC 0
201 Feature 1
202 Feature 2
203 Feature 3
204 EPIC 4
205 Feature 5
206 Feature 6
Prefeered result
ID Type Idx Parent
200 EPIC 0 200
201 Feature 1 200
202 Feature 2 200
203 Feature 3 200
204 EPIC 4 204
205 Feature 5 204
206 Feature 6 204
I have tried making self-referencing conditional columns but thats not possible,
#"Added Custom" = Table.AddColumn(#"Added Index", "Parent", each if [Work Item Type] = "Epic" then [ID] else #"Added Custom"{[Index]-1}[ID]),

Add custom column. Fill down. Assuming data is in Table1 with 2 columns named ID and Type, then
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Added Custom" = Table.AddColumn(Source, "Parent", each if [Type]="EPIC" then [#"ID"] else null),
#"Filled Down" = Table.FillDown(#"Added Custom",{"Parent"})
in #"Filled Down"

Related

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

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

Oracle | Retrieval of records from tables having One to many relationship

I have two tables which share one-to-many relationship. MY_FACT is the parent table whereas MY_RMDETAILS is the child table having multiple records for a single parent record.
Table MY_FACT:
FACT_ID
FACT_DATE
TOTAL_DEMAND
1000
21/04/2022
500
2000
21/04/2022
500
Table MY_RMDETAILS:
RM_ID
FACT_ID
PROMISE_QTY
REQUEST_QTY
RM_ITEM_NAME
200
1000
500
500
RM1
201
1000
400
500
RM2
202
1000
500
500
RM3
203
1000
400
500
RM4
300
2000
500
500
RM1
301
2000
500
500
RM2
302
2000
500
500
RM3
303
2000
500
500
RM4
I need to write a query to have below output.
Logic:
If MY_RMDETAILS.PROMISE_QTY is less than MY_RMDETAILS.REQUEST_QTY, the supply is insufficient.
So for any given MY_FACT record, if any one of its children records from MY_RMDETAILS has PROMISE_QTY less than REQUEST_QTY, the flag SUPPLY_SUFFICIENT in output should be N else it should be Y.
And INSUFFICIENT_RMs column in output should show the MY_RMDETAILS.RM_ITEM_NAME of "insufficient" records as comma separated format.
EXPECTED OUTPUT:
FACT_ID
FACT_DATE
TOTAL_DEMAND
SUPPLY_SUFFICIENT?
INSUFFICIENT_RMs
1000
21/04/2022
500
N
RM2,RM4
2000
21/04/2022
500
Y
Please help. Thanks in advance.
You can try to use subquery with condition aggregate function.
SELECT t2.*,
CASE WHEN t1.INSUFFICIENT_cnt > 0 THEN 'N' ELSE 'Y' END,
t1.INSUFFICIENT_RMs
FROM (
SELECT FACT_ID,
LISTAGG(CASE WHEN PROMISE_QTY < REQUEST_QTY THEN RM_ITEM_NAME END, ', ') WITHIN GROUP (ORDER BY RM_ID) INSUFFICIENT_RMs,
COUNT(CASE WHEN PROMISE_QTY < REQUEST_QTY THEN RM_ITEM_NAME END) INSUFFICIENT_cnt
FROM MY_RMDETAILS
GROUP BY FACT_ID
) t1 INNER JOIN MY_FACT t2
ON t1.FACT_ID = t2.FACT_ID

How to pivot duplicate rows to distinct columns in excel

I have an excel data where I need to transpose the duplicate rows to columns suitable for analysis. Please let me know how to do this?
For example,
My excel data looks like
id metrics date value
1 A 20190812 100
1 A 20190813 100
1 A 20190814 100
1 B 20190812 200
1 B 20190813 130
2 A 20190812 100
2 B 20190813 106
2 C 20190814 104
The result I look forward to is
id A B C date
1 100 200 null 20190812
1 100 130 null 20190813
1 100 null null 20190814
2 100 null null 20190812
2 null 106 null 20190813
2 null null 104 20190814
Try below in Pivot:
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content]
Pivot =
Table.Pivot(
Source,
List.Distinct(Source[metrics]),
"metrics",
"value",
List.Sum)
in
Pivot

Hive: Joining tables with different scenarios

I have a question on joining tables in a different scenario. Please find the sample tables below.
Capacity of expected table row 3-5 should be repeated as table 2 does not have those fields.
could anyone please help to get expected table?
Table 1:
No ProjectID Capacity
1 514 4
2 418 10
3 418 30
4 401 40
5 502 41
Table2:
NO ProjectID Capacity1 Capacity2
1 514 4 10
2 418 10 20
Expected Table:
NO ProjectID Capacity1 Capacity2
1 514 4 10
2 418 10 20
3 418 30 30
4 401 40 40
5 502 41 41
1.Do left outer join
2.For the values not matching take them from table 1 with if condition.
select t1.no,t1.projectid,t1.capacity1,if(t2.capacity2 is null,t1.capacity,t2.capacity)
from table1 t1 left outer join table2 t2 on t1.no=t2.no
I think above query meets your requirement let me know if need any more help.

How can I use LINQ to find the Intersect between two properties of the same collection?

Given an IList<Foo> with a data set that looks like this:
ID CHILD PARENT TYPE
1 102 101 UPSELL
1 103 101 UPSELL
2 102 101 BONUS
2 104 103 BONUS
3 102 101 BONUS
4 102 101 PRODUCT
4 104 102 PRODUCT
How can I use LINQ to find a child which has a parent with the same ID?
Desired output is
ID CHILD PARENT TYPE
4 102 101 PRODUCT
I think this is what you're looking for. I grouped by ID first so I could process each group individually, but there's probably a way to combine it into a single query.
var grouping = foos.GroupBy(f => f.ID);
foreach(var g in grouping)
{
var result = (from f1 in g from f2 in g where f1.Child == f2.Parent select f1);
if( result.Any())
{
// you have your answer
}
}

Resources