Combine rows based on multiple criteria and then sum one column - filter

Group rows together when columns A,B,C,D,F match (D can be blank and still be considered a match) and then sum the "Qty" column.
I can FILTER and I can SUM, but I can't seem to do both.
This formula will be in 'estimate-Tally'!A2

=QUERY(QUERY(A2:X6,
"select A,B,C,' ',' ',F,sum(G),H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X
where A is not null
group by A,B,C,' ',' ',F,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X", 0),
"offset 1", 0)

Related

Oracle counting distinct rows using two columns merged as one

I have one table where each row has three columns. The first two columns are a prefix and a value. The third column is what I'm trying to get a distinct count for columns one/two.
Basically I'm trying to get to this.
Account
Totals
prefix & value1
101
prefix & value2
102
prefix & value3
103
I've tried a lot of different versions but I'm basically noodling around this.
select prefix||value as Account, count(distinct thirdcolumn) as Totals from Transactions
It sounds like you want
SELECT
prefix||value Account,
count(distinct thirdcolumn) Totals
FROM Transactions
GROUP BY prefix, value
The count(distinct thirdcolumn) says you want a count of the distinct values in the third column. The GROUP BY prefix, value says you want a row returned for each unique prefix/value combination and that the count applies to that combination.
Note that "thirdcolumn" is a placeholder for the name of your third column, not a magic keyword, since I didn't see the actual name in the post.
If you want the number of rows for each prefix/value pair then you can use:
SELECT prefix || value AS account,
COUNT(*) AS totals
FROM Transactions
GROUP BY prefix, value
You do not want to count the DISTINCT values for prefix/value as if you GROUP BY those values then different values for the pairs will be in different groups so the COUNT of DISTINCT prefix/value pairs would always be one.

PowerBI: same IDs for consecutive data

I have a question about generating IDs in power BI.
Question: I have the table like following:
I'd like to generate same IDs for consecutive data (it means that end date of previous row equals to start date of current row), that has equal values in column 'Type', but IDs have to be different, if data is not consecutive. The example of correct IDs are below.
(Note that 'Bike' has different Ids because these rows are NOT consecutive)
Can you help me please with writing DAX command or Query? Every advice would be very helpful.
I created 3 separate calculated columns in order to accomplish this , with the final IDs column being the result you're looking for. I've included the DAX below, just replace the "Table1" references with your table's name. Skip down to the bottom to see an image of how the final table looks (Results Table).
First Calculated Column
Create a unique string ID for each row in the table. Specify "Same" for rows that will inherit the previous unique ID. This makes use of the EARLIER function and a solution similar to Get Value from Previous Row post on the Community page in order to evaluate the previous row in a calculation.
First Iteration IDs =
VAR previousRow =
TOPN(
1,
FILTER(
Table1,
Table1[Start Date] < EARLIER(Table1[Start Date])
&& Table1[Type] = EARLIER(Table1[Type])
),
[Start Date],
DESC
)
VAR previousEndDate = MINX(previousRow, [End Date])
VAR previousType = MINX(previousRow, [Type])
RETURN
IF(
Table1[Start Date] = previousEndDate && Table1[Type] = previousType,
"Same",
CONCATENATE(FORMAT(Table1[Start Date], "dd/mm/yyyy"), CONCATENATE(" ", Table1[Type]))
)
Second Calculated Column
Convert all of the "Same" IDs from the previous column to their respective IDs in that column.
Second Iteration IDs =
VAR previousRow =
TOPN(
1,
FILTER(
Table1,
Table1[Start Date] < EARLIER(Table1[Start Date])
&& Table1[Type] = EARLIER(Table1[Type])
&& NOT(Table1[First Iteration IDs] = "Same")
),
[First Iteration IDs],
DESC
)
VAR previousID = MINX(previousRow, [First Iteration IDs])
RETURN
IF(
Table1[First Iteration IDs] = "Same",
previousID,
Table1[First Iteration IDs]
)
Third Calculated Column
Use the RANKX function to create more user-friendly number IDs from the string IDs in the previous column. This is the original column that you had asked for.
IDs = RANKX(Table1, Table1[Second Iteration IDs], , 1, Dense) - 1
I've included a results table below based on the sample table that you provided. You may need to play around with the unique string ID format or the RANKX function depending on how your actual data looks.

How to divide the count of 2 different values within the same 1 column in Tableau?

I am trying to divide the data above, that is in the same column within my raw data. The one column name is "Status". I split it under this column into 2 separate columns within Tableau.
How would I create a calculation to divide T & D by each other? For an example, I would like to divide 166/1523, 155/1535, etc.
How do I differentiate between these 2 under the same column "Status".
SUM (IF [Status] = "T" THEN [Your Measure] END)
/
SUM (IF [Status] = "D" THEN [Your Measure] END)

ArrayFormula with sum of previous rows

I have an ArrayFormula to calculate a value for each row, and for each 6th row I want it to calculate the sum of the previous 5 instead.
Example sheet: https://docs.google.com/spreadsheets/d/18g2bOOBqsUgmy3ZXINOl6hcaMXf-uYJv7PGft247FjU/edit?usp=sharing
I have tried several routes, including google script, but keep banging my head against limitations of ArrayFormula.
You need make group by rows
My E.g
Cell A2 (Name groups):
=ArrayFormula(IF(B2:B<>"",FLOOR((ROW(A2:A)-2)/5)+1,""))
Column B (Your Data)
Cell E2 (Result):
=QUERY({QUERY({A2:B},"select Col1,sum(Col2) where Col1>0 group by Col1");
QUERY({A2:B},"select Col1,Col2 where Col1>0")},
"select Col2 where Col1>0 order by Col1,Col2 label Col2 ''")
Function References
Query

How can I select two columns in a query and order them by a third column?

Basically, is there a way to do this?
select B,C,SUM(I) group by B,C order by L
I've added the row numbers to L, but if there's a way to get row numbers in the formula, I'd love to know. Thanks.
try perhaps:
=ARRAY_CONSTRAIN(QUERY(A:L,
"select B,C,sum(I),L
where B is not null
group by B,C,L
order by L
label sum(I)''", 0), 999^99, 3)
This formula will group by 2 columns and sort by the 3-d column.
=ARRAY_CONSTRAIN(QUERY(
FILTER({B:B,C:C,I:I,VLOOKUP(B:B&C:C,{B:B&C:C,L:L},2,)},B:B<>""),
"select Col1,Col2,sum(Col3),Col4
where Col1 is not null
group by Col1,Col2,Col4
order by Col4", 1), 999^99, 3)
ARRAY_CONSTRAIN to hide sort column
FILTER + VLOOKUP to select first entries of column 3 (L) in order to skip duplicates.

Resources