I'm trying to do something that is simple in excel but I need it to be added in my query. I am trying to create a cumulative sum in a new column of time values in one column if a name column equals a previous row. Example of before and after below
Data Input Table
ID
Time
A
2
B
3
C
1
D
0.5
E
1
E
3
E
5
F
2
G
3
G
4
H
1
Table After Query
ID
Time
BeforeStart
A
2
0
B
3
0
C
1
0
D
0.5
0
E
1
0
E
3
1
E
5
4
F
2
0
G
3
0
G
4
3
H
1
0
Basically if column ID equals the row above itself then sum the time and BeforeStart rows above itself, if it doesn't then it is 0.
In powerquery, try below and replace Table2 with the name of your source data table
let
xFunction = (xTable as table) as table => let
#"Added Index" = Table.AddIndexColumn(xTable, "Index", 1, 1, Int64.Type),
#"Added Custom" = Table.AddColumn(#"Added Index", "Running Total", each List.Sum(List.FirstN(#"Added Index"[Time],[Index]-1)))
in #"Added Custom",
Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", type text}, {"Time", type number}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"ID"}, {{"data", each xFunction(_), type table }}),
#"Expanded data" = Table.ExpandTableColumn(#"Grouped Rows", "data", {"Time", "Running Total"}, {"Time", "Running Total"})
in #"Expanded data"
Try this function:
=IF(COUNTIF($A2:$A$2,$A2)=1,0,SUMPRODUCT((A2:$A$2=A2)*B2:$B$2)-B2)
Related
I have 2 table as follows:
Case_No.
Month
Month_Prev
Code
Stage
Code_Prev
Stage_Prev
Status
1
2022.09
2022.08
b
2
a
1
1
2
2022.09
2022.08
a
2
b
1
1
and
Month
Code
Stage
Rate
Status
2022.09
a
1
0.2
1
2022.09
a
2
0.1
1
2022.09
b
1
0.3
1
2022.09
b
2
0.1
1
2022.08
a
1
0.3
1
2022.08
a
2
0.2
1
2022.08
b
1
0.15
1
2022.08
b
2
0.25
1
My desired output:
Case_No.
Month
Month_Prev
Code
Stage
Code_Prev
Stage_Prev
Status
Rate
Rate_Prev
1
2022.09
2022.08
b
2
a
1
1
0.1
0.3
2
2022.09
2022.08
a
2
b
1
1
0.1
0.15
Basically, I want to obtain the rate corresponding to each individual set of {Month, Code, Stage, Status} and {Month_Prev, Code_Prev, Stage_Prev, Status} and I'm using Oracle. Anyone can help?
Well, you have already shown the keys for the join, so simply apply them. You'll have to join the second table twice, once for Month, once for Month_Prev.
select
t1.*
this.rate,
prev.rate as prev_rate
from t1
join t2 this on this.month = t1.month and this.code = t1.code and this.stage = t1.stage and this.status = t1.status
join t2 prev on prev.month = t1.month_prev and prev.code = t1.code and prev.stage = t1.stage and prev.status = t1.status
order by t1.month, t1.code, t1.stage, t1.status;
(In case there can be t1 rows without a match in t2 and you still want to show the row without a rate then, then change the inner joins to left outer joins.)
In Power Query (M) there is a table with columns A and B. Column C is generated. Column C get value "T" if: A < 3 and the value of B exists elsewhere in column B where A is >= 3. This is true for the 4th row.
Furthermore, to complete it:
if column A >= 3 then column C = column B
if column A < 3 and NOT
(the value of B exists elsewhere in column B where A is >= 3) then
"n"
Is there a (simple) way to write this in M?
A
B
C
3
x
x
3
x
x
3
x
x
1
x
T
2
y
n
2
y
n
Thanks in advance!
UPDATED
You can use this
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Added Custom" = Table.AddColumn(Source,"C",(i)=>
if i[A]>=3 then i[B] else if
i[A]<3 and Table.RowCount(
Table.SelectRows(Source, each [B]=i[B] and [A]>=3)
) >1 then "T" else "n"
)
in #"Added Custom"
For example, if I have a table that looks like this:
Group
Subgroup
Values
1
Subgroup 1
1
1
Subgroup 2
2
1
Subgroup 3
3
1
Subtotal
null
2
Subgroup 1
3
2
Subgroup 2
5
2
Subgroup 3
6
2
Subgroup 4
2
2
Subtotal
null
3
Subgroup 1
2
3
Subtotal
null
4
Subgroup 1
3
4
Subgroup 2
4
4
Subgroup 3
5
4
Subtotal
null
How can I transform it into this:
Group
Subgroup
Values
1
Subgroup 1
1
1
Subgroup 2
2
1
Subgroup 3
3
1
Subtotal
6
2
Subgroup 1
3
2
Subgroup 2
5
2
Subgroup 3
6
2
Subgroup 4
2
2
Subtotal
16
3
Subgroup 1
2
3
Subtotal
2
4
Subgroup 1
3
4
Subgroup 2
4
4
Subgroup 3
5
4
Subtotal
12
Every Subtotal line has at least one Subgroup line above it; there are no consecutive Subtotal lines.
ClLick select Group and right click to Group By...
Do operation Sum on Values column and name the column Values
Add column custom column Subgroup with ="Subtotal" as the formula
Combine the new table with the old table then remove the null rows
Resort
let Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Group", Int64.Type}, {"Subgroup", type text}, {"Values", Int64.Type}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"Group"}, {{"Values", each List.Sum([Values]), type number}}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "Subgroup", each "Subtotal"),
Custom = Table.SelectRows(#"Changed Type", each ([Values] <> null)) & #"Added Custom",
#"Sorted Rows" = Table.Sort(Custom,{{"Group", Order.Ascending}, {"Subgroup", Order.Ascending}})
in #"Sorted Rows"
I have a list which returns.
Grade Col1 Col2 Col3 Col4
t1 2 1 3 2
t2 2 5 1 5
t1 2 6 4 4
First, i need to add the values in each row for the corresponding Grade so that , i would have
Grade Sum(Sum of the 4 columns)
t1 8
t2 13
t1 16
and finally my result should be like:
Grade Total
t1 8+16 = 24
t2 13
I'm a newbie to Linq., can someone suggest me the approach using Linq, for returning this result.??
Thanks
Following LINQ query will group all rows by Grade and then produce sum of columns for each grade:
var query = from r in list
group r by r.Grade into g
select new {
Grade = g.Key,
Total = g.Sum(x => x.Col1 + x.Col2 + x.Col3 + x.Col4)
};
I have a table with these columns
create table demo (
ID integer not null,
INDEX integer not null,
DATA varchar2(10)
)
Example data:
ID INDEX DATA
1 1 A
1 3 B
2 1 C
2 1 D
2 5 E
I want:
ID INDEX DATA
1 1 A
1 2 B -- 3 -> 2
2 1 C or D -- 1 -> 1 or 2
2 2 D or C -- 1 -> 2 or 1
2 3 E -- 5 -> 3 (closing the gap)
Is there a way to renumber the INDEX per ID using a single SQL update? Note that the order of items should be preserved (i.e. the item "E" should still be after the items "C" and "D" but the order of "C" and "D" doesn't really matter.
The goal is to be able to create a primary key over ID and INDEX.
If that matters, I'm on Oracle 10g.
MERGE
INTO demo d
USING (
SELECT rowid AS rid, ROW_NUMBER() OVER (PARTITION BY id ORDER BY index) AS rn
FROM demo
) d2
ON (d.rowid = d2.rid)
WHEN MATCHED THEN
UPDATE
SET index = rn