Excel - Power Pivot
I am trying to create a measure to calculate a disctinct count (or a sum of a disctinct count) of a column - grouped by a second column.
Based on the column "lot" an the column "sku", I want to calculate the column "count_distinct_sku_by_lot". Column "sqm" is not relevant.
Which DAX code I could use?
(picture of data added)
lot sku sqm count_distinct_sku_by_lot
lot1 sku1 1 1
lot2 sku2 2 2
lot2 sku2 3 2
lot2 sku3 4 2
lot3 sku4 5 3
lot3 sku4 6 3
lot3 sku5 7 3
lot3 sku5 8 3
lot3 sku6 9 3
lot4 sku7 11 1
You can create a calculated Column :
DistinctCount =
VAR VrCurrentRow = [lot]
RETURN
CALCULATE(
DISTINCTCOUNT(Sheet1[sku]);
FILTER(
Sheet1;
Sheet1[lot] = VrCurrentRow
)
)
Result :
Related
I want to filter a pivot table in the following set up:
My Table:
Key Value1 Value2
1 23 a
2 33 b
3 1 c
4 5 d
My pivot table (simplified):
Key SUM of Value1 COUNTA of Value2
1 23 1
2 33 1
3 1 1
4 5 1
Grand Total 62 4
I now want to filter the pivot table by the values in this list:
Keys
1
2
4
So the resulting pivot table should look like this:
Key SUM of Value1 COUNTA of Value2
1 23 1
2 33 1
4 5 1
Grand Total 61 3
I thought this should be possible by using a custom formula in the pivot filter but it seems I have no way of using the current cell in the pivot e.g. to make a lookup.
I created a simple example of this setup here: https://docs.google.com/spreadsheets/d/1GlQDYtW8v8ri5L68RhryTZxwTikV_NXZQlccSI6_7pU/edit?usp=sharing
paste this formula in Filters!B1:
=ARRAYFORMULA(IFERROR(VLOOKUP(A1:A, Table!A1:C, {2,3}, 0), ))
and create a resulting pivot table from there:
demo spreadsheet
Consider, I have the following in a select query:
ID Flag
5 Y
5 Y
5 N
6 Y
6 Y
6 Y
6 N
I should be adding a new column count in the same select which counts the number of 'Y' records for the ID and assigns it to all. (Eg: ID=5 has 3 records. All of them should be assigned the count value as '2').
Output required in select query:
ID Flag count
5 Y 2
5 Y 2
5 N 2
6 Y 3
6 Y 3
6 Y 3
6 N 3
Use a window function:
select id,
flag,
count(case when flag = 'Y' then 1 end) over (partition by id) as "count"
from the_table
order by id;
The case expression will return null for flags with N and thus they will be ignored by the count() function
I am having trouble solve. I am suppose to be getting a record every time there is a change to an account in our data warehouse, but I am only receiving one. The table below is a sample of what I am working with.
Row Acct1 Acct2 Date Total_Reissued Reissue_Per_Day
1 A 1 1/1/2016 2 2
2 A 1 1/2/2016 3 1
3 A 1 1/3/2016 5 2
4 A 1 1/4/2016 6 1
1 B 3 1/1/2016 1 1
2 B 3 1/2/2016 2 1
1 B 4 1/1/2016 1 1
2 B 4 1/2/2016 2 1
The Reissued Column is a running total. For Acct A on 1/1/2016 there were 2 reissues, then On 1/2/2016 there was 1 more making a total of 3. My problem is calculating the actual number of reissues per day.
You can use the lag() function to peek back at the previous row; assuming that 'previous' is the last date you saw for the acct1/acct2 combination you can do:
select row_number() over (partition by acct1, acct2 order by dt) as row_num,
acct1, acct2, dt, total_reissued,
total_reissued - nvl(lag(total_reissued)
over (partition by acct1, acct2 order by dt), 0) as reissue_per_day
from your_table;
ROW_NUM A ACCT2 DT TOTAL_REISSUED REISSUE_PER_DAY
---------- - ---------- ---------- -------------- ---------------
1 A 1 2016-01-01 2 2
2 A 1 2016-01-02 3 1
3 A 1 2016-01-03 5 2
4 A 1 2016-01-04 6 1
1 B 3 2016-01-01 1 1
2 B 3 2016-01-02 2 1
1 B 4 2016-01-01 1 1
2 B 4 2016-01-02 2 1
I'm not sure if your 'row' column actually exists, or is required, or was just to illustrate your data. I've generated it anyway, in case you need it.
The main bit of interest is:
lag(total_reissued) over (partition by acct1, acct2 order by dt)
which finds the previous date's value (using dt as a column name, since date isn't a valid name). That then has an nvl() wrapper so the first row sees a dummy value of zero instead of null. And then that is subtracted from the current row's value to get the difference.
I need to apply interactive soring for matrix columns, which contain aggregated data.
The report is counting products sold in different places:
Product A Product B Product C
---------------------------------------------------------------
Country 1 5 10 4
City A 3 0 3
City B 2 10 1
---------------------------------------------------------------
Country 2 10 5 5
City C 2 4 2
City D 8 1 3
After descending sorting on "Product A" the table rows should be sorted by "Product A" sales in country, and also by sales in city:
Product A Product B Product C
---------------------------------------------------------------
Country 2 10 5 5
City D 8 1 3
City C 2 4 2
---------------------------------------------------------------
Country 1 5 10 4
City A 3 0 3
City B 2 10 1
The matrix scheme looks like this:
| [Product]
[Country] | [City] | [Count(Product)]
Interactive sort is not supported in matrix.
A workaround could be the one below:
Create a sort by parameter with the values:
Label Value
Country ASC, City ASC 1
Country DESC, City ASC 2
Country ASC, City DESC 3
Country DESC, City DESC 4
Then in the country create two sorting expressions:
=Iif(Parameters!SortBy.Value = 1 OR Parameters!SortBy.Value = 3,Fields!country.Value,"")
ASCENDING sort
=Iif(Parameters!SortBy.Value = 2 OR Parameters!SortBy.Value = 4,Fields!country.Value,"")
DESCENDING sort
Do the same for city:
=Iif(Parameters!SortBy.Value = 1 OR Parameters!SortBy.Value = 2,Fields!city.Value,"")
ASCENDING sort
=Iif(Parameters!SortBy.Value = 3 OR Parameters!SortBy.Value = 4,Fields!city.Value,"")
DESCENDING sort
I have a table that's like this
rank continuationofrow
1 row
2 row
3 row
4 row
4 row
4 row
I'm trying to identify the previous rows rank number within an Oracle statement. Any help is greatly appreciated. I've searched the internet and haven't found much.
You must have another column that establishes the order of the rows with the same rank, otherwise the concept of "previous row" is meaningless. Let's suppose you do:
seq rank continuationofrow
1 1 row
2 2 row
3 3 row
4 4 row
5 4 row
6 4 row
No you can use an analytic function:
select seq, rank, continuationofrow, lag(rank) over (order by seq) as prev_rank
from mytable;
seq rank continuationofrow prev_rank
1 1 row
2 2 row 1
3 3 row 2
4 4 row 3
5 4 row 4
6 4 row 4
select
...
lag(rank, 1) over (order by ordering-columns)
from
..