I have a table with 2 columns (DateKey and StoreCode) and I want to calculate age of each store in each day(AgeOfStore) using DAX. Actually I want to know how many days each store have worked?
For example, in 20210101, Store 1001 did its first work day and then this store in 20210102 did its second day,...
DateKey
StoreCode
AgeOfStore
20210101
1001
1
20210101
1002
1
20210102
1001
2
20210102
1002
2
20210102
1003
1
20210103
1001
3
20210103
1002
3
20210103
1003
2
20210104
1001
4
20210104
1002
4
20210104
1003
3
Thank you in advance.
DAX Measure
AgeOfStore =
RANKX (
FILTER(ALL(tbl),tbl[StoreCode]=max(tbl[StoreCode])),
CALCULATE ( MAX ( 'tbl'[DateKey] ) ),
,
ASC
)
Related
I have 3 tables - Folders, Documents & Versions
FolderID
Folder Name
1
Folder 1
2
Folder 2
3
Folder 3
Documents looks like this:
DocID
Doc Name
FolderID
1000
Doc 1
1
1001
Doc 2
1
1002
Doc 3
2
1003
Doc 4
2
1004
Doc 5
3
Versions looks like this:
VersionID
DocID
1
1000
2
1001
3
1001
4
1002
5
1003
6
1003
7
1004
So Doc 1, 3 & 5 have 1 version each, and Doc 2 & 4 have 2 versions.
I would like to count the documents that have more than 1 version. In this example Folder 1 & 2 both have 1 document with more than 1 version, and Folder 3 has none.
I'd like some DAX that will accomplish that. I'm managing to confuse myself because the filter is based on a count of a related table.
This is what I came up with, but I know I'm off
Count Docs =
VAR VersionsMin = 2
RETURN
CALCULATE (
COUNT ( 'Documents'[DocID] ),
FILTER ( 'Versions', COUNT ( 'Versions'[VersionID] ) >= VersionsMin )
)
Try this out:
In the first step add a new column to your Documents table
Version Count =
COUNTROWS(RELATEDTABLE(Versions))
In the second step you can use this column for filtering
Docs with multiple versions =
CALCULATE(
COUNT(Documents[DocID]),
Documents[Version Count] > 1
)
This allows you to create the following table visual:
Apologies in advance I do not have a question instead a situation to which I need to find the solution. Trying to explain the setup with the example -
I have the oracle table as follows -
ID Name Qty1 Qty2 Qty3 Date
-- ---- ---- ---- ---- -----------
1 ABC 0 0 -10 4th-May
1 ABC 5 0 -5 5th-May
1 ABC 0 0 -5 6th-May
1 ABC 0 0 -5 7th-May
1 ABC 0 0 -10 8th-May
1 ABC 0 5 0 9th May
1 ABC 0 0 0 10th May
1 ABC 1 0 15 11th May
...
1 ABC 0 0 10 (100th Day)
--- So on till 100 day from today's date.
Basically, for a single ID,Name I have three quantities starting from the current date till 100th day.
First, I need to find the row where the first occurrence of non zero Qty1 or Qty2 happens( In this case 5th-May record).
Next, I need to find the row where the next occurrence of non-zero Qty1 or Qty2 happens( In this case 9th May). Then I will have to find the record preceding this date (in this case 8th-May).
Qty3 will be picked up from this preceding record that qty will be inserted for the previous occurrence of the date when Qty1 or Qty2 are non-zero.
The resultant data:
ID Name Qty3 Date
-- ---- ---- --------
1 ABC -10 5th-May
1 ABC -10 9th-May
1 ABC 10 11th-May
The last non-zero record will have the Qty3 from the 100th day if there are no non-zero records from the day till 100th day.
Any leads how can I achieve the requirement. I need to find some solution without using cursors or loop.?
Thanks in Advance and apologies for the bad writeup, but really need an answer to this question.
I've got an inventory transactions table :
Product
Date
Direction
Quantity
A
Date 1
IN
3
B
Date 2
IN
55.7
A
Date 3
OUT
1
B
Date 3
OUT
8
B
Date 3
IN
2
I can easily get the stock for any date with the following query :
SELECT Product,
SUM(CASE Direction WHEN 'IN' THEN Quantity ELSE -1 * Quantity END)
FROM Transactions
WHERE Date <= '#DateValue#'
GROUP BY Product;
Now my purpose is to get stocks aged like this using the FIFO principle :
Product
Total stock
0-30 days
31-60 days
61-90 days
91+ days
A
3
3
0
0
0
B
34.2
10
14.2
7
3
C
25
20
3
1
1
D
10
2
8
0
0
E
1
0
0
1
0
I am using SQL Server 2016 & SSMS 18.
The solution should be fast as it will be working against a table with 3,000,000+ rows.
A single query is preferred since it will be integrated into an ERP system.
I have yet to find a solution based on a single query after weeks of research. Any help is appreciated. Thanks in advance.
In my game application I have a combats table:
id player_one_id player_two_id
---- --------------- ---------------
1 1 2
2 1 3
3 3 4
4 4 1
Now I need to know hoy many unique users played the game. How can I apply distinct, count on both columns player_one_id and player_two_id?
Many thanks.
By using union you can get unique distinct value.
$playerone = DB::table("combats")
->select("combats.player_one_id");
$playertwo = DB::table("combats")
->select("combats.player_two_id")
->union($playerone)
->count();
I am trying clean up this employee volunteer data. There is no way to track if employee already is registered volunteer so he can sign up as new volunteer and will get a new VOLUNTEER_ID. I have a data feeding into where i can tie each VOLUNTEER_ID to its EMP_ID. The volunteer data needs to be cleaned up so we can figure out how the employee moved from a volunteer_level to another and when.
The business logic is that, when there is a overlaping dates, we give the highest level to the employee for the timeframe of between start_date and end_date.
I posted a Input sample of data and what the output should be.
Is it possible to do this a PIG script ? Can someone please help me
INPUT:
EMP_ID VOLUNTEER_ID V_LEVEL STATUS START_DATE END_DATE
10001 100 1 A 1/1/2006 12/31/2007
10001 200 1 A 5/1/2006
10001 100 1 A 1/1/2008
10001 300 3 P 3/1/2008 3/1/2008
10001 300 3 A 3/2/2008 12/1/2008
10001 1001 2 A 5/1/2008 6/30/2008
10001 1001 3 A 7/1/2008
10001 300 2 A 12/2/2008
OUTPUT NEEDED:( VOLUNTEER_ID is not needed in output but adding below to show which ID was selected for output and which did not)
EMP_ID VOLUNTEER_ID V_LEVEL STATUS START_DATE END_DATE
10001 100 1 A 1/1/2006 12/31/2007
10001 300 3 P 3/1/2008 3/1/2008
10001 300 3 A 3/2/2008 12/1/2008
10001 1001 2 A 5/1/2008 6/30/2008
10001 1001 3 A 7/1/2008
It seems like you want the row in your data with the earliest start date for each V_LEVEL, STATUS, EMP_ID, and VOLUNTEER_ID
First we add a unix time column and then find the min for that column (this is in the latest version of pig so you may need to update your version).
data_with_unix = foreach data generate EMP_ID, VOLUNTEER_ID, V_LEVEL, STATUS, START_DATE, END_DATE, ToUnixTime((datetime)START_DATE) as unix_time;
grp = group data_with_unix by (EMP_ID, VOLUNTEER_ID, V_LEVEL, STATUS);
max_date = foreach grp generate group, MIN(data_with_unix.unix_time);
Then join the start and end date back into your dataset since there it doesn't look like there is currently a way to convert unix time back to date.