I am trying to improve this measure where if no SKU is selected it only turns say the top 1st relevant SKU: To be more clearer about what the below measure is doing it will find the Start date when the SKU reached 0 example:
SKU Date On-Hand Quantity test at 0
1234 8/23/23 5000 8/28/23
1234 8/28/23 0 8/28/23
1234 8/29/23 0 8/28/23
1234 8/30/23 2000 8/28/23
Notice in this example it is NOT 8/29/23 because it reached zero on the 28th which is the start date of when it reached 0.
The main issue I am having is when no SKU is selected I get a over 1,000,000,000 rows reached exception and I want to default it to the first SKU that is in this requirement. The user confirmed that they look up a single SKU at a time however it could be helpful if they were to recieve say the first 50 SKUS.
I also tried using the RANK function in conjunction with the TOPN to no avail: Here is my working Code
Test at 0 =
//default to today if no date is selected, force a sku and warehouse to be chosen
VAR selected_date =
IF (
HASONEVALUE ( ‘Date’[Date] ),
SELECTEDVALUE ( ‘Date’[Date] ),
TODAY ()
)
VAR selected_warehouse =
IF (
SELECTEDVALUE ( ‘Warehouse’[Code] )
<> BLANK (),
SELECTEDVALUE ( ‘Warehouse’[Code] ),
“”
)
VAR selected_sku =
IF (
SELECTEDVALUE ( ‘Product’[SKU] )
<> BLANK (),
SELECTEDVALUE ( ‘Product’[SKU] ),
“”
) //obtain the most recent (max date) where the on hand quantity = 0 for that SKU
VAR last_date_zero =
CALCULATE (
MAX ( ‘Date’[Date] ),
CROSSFILTER ( ‘Date’[Date Key], ‘RSC Inventory’[dimAsOfDateKey], BOTH ),
‘RSC Inventory’[On_Hand_Quantity] = 0,
‘Date’[Date] <= selected_date
&& selected_date <= TODAY (),
‘Product’[SKU] = selected_sku,
‘Warehouse’[Code] = selected_warehouse
) //obtain the most recent (max date) where the on hand quantity is NOT 0 for that SKU
VAR last_date_not_zero =
CALCULATE (
MAX ( ‘Date’[Date] ),
CROSSFILTER ( ‘Date’[Date Key], ‘RSC Inventory’[dimAsOfDateKey], BOTH ),
‘RSC Inventory’[On_Hand_Quantity] <> 0,
‘Date’[Date] < last_date_zero,
‘Date’[Date] <= selected_date
&& selected_date <= TODAY (),
‘Product’[SKU] = selected_sku,
‘Warehouse’[Code] = selected_warehouse
) //if the on hand quantity never reaches 0 up to the selected date, output a blank
//if the SKU came into the database (06/29/2023) as out of stock and the logic is trying to grab that day, output a blank since we don’t know the actual out of stock start date
//otherwise add one day to the most recent day where the on hand quantity is NOT 0
VAR result =
IF (
last_date_zero = BLANK ()
|| last_date_not_zero = BLANK (),
BLANK (),
last_date_not_zero + 1
)
RETURN
result