Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.
Hi there,
I have a calculated column that counts the number of consecutive days an item is out-of-stock. It works if there isn't that much data in the tables. However, when there is more data in the tables, the performance is very bad or I even get an error message (Memory error: Memory Allocation failure . Try simplifying or reducing the number of queries).
The data model consists of a Keys table and a FactTable. The Keys table creates unique keys for each unique item + date combination, so it has three columns: item, date, key. The FactTable stores data on each key, such as the current stock level.
Are there any recommendations on how I can improve the performance of this calculated column with the same data structure?
#Days out-of-stock =
IF (
'FactTable'[Stock] > 0,
0,
VAR vCurrentDate =
RELATED ( Keys[Date] )
VAR vCurrentID =
RELATED ( Keys[Item] )
VAR vPrevTbl =
FILTER (
'FactTable',
RELATED ( Keys[Date] ) <= vCurrentDate
&& RELATED ( Keys[Item] ) = vCurrentID
&& 'FactTable'[Stock] <= 0
)
VAR vPrevDate =
MAXX (
FILTER (
vPrevTbl,
VAR vCD =
RELATED ( Keys[Date] )
VAR r =
MAXX (
FILTER ( vPrevTbl, RELATED ( Keys[Date] ) < vCD ),
RELATED ( Keys[Date] )
)
RETURN
vcd - 1 <> r
),
RELATED ( Keys[Date] )
)
RETURN
vCurrentDate - vPrevDate + 1
)
Thanks in advance!
Solved! Go to Solution.
Hi, @Gertjan
Obviously, there is nothing wrong with your code and the code doesn't look like there's anything to simplify. Mainly because you have a large amount of data.
For your case, I have these suggestions:
If you are interested in the above suggestions but have problems, please add information, or share a sample file, and I will help you solve it.
Did I answer your question? Please mark my reply as solution. Thank you very much.
If not, please feel free to ask me.
Best Regards,
Community Support Team _ Janey
Hi, @Gertjan
Obviously, there is nothing wrong with your code and the code doesn't look like there's anything to simplify. Mainly because you have a large amount of data.
For your case, I have these suggestions:
If you are interested in the above suggestions but have problems, please add information, or share a sample file, and I will help you solve it.
Did I answer your question? Please mark my reply as solution. Thank you very much.
If not, please feel free to ask me.
Best Regards,
Community Support Team _ Janey
Hi,
Here is one approach to this:
End result:
The idea is pretty simple: we calculate days that we have items in stock and substract this from all the dates during the period we are interested in. So here 1.1.2021-8.1.2021 = 8 days -> for item1 we have stock on 6 days -> 2 days out of stock. If you have 0 values for days in your stock table you can add this to the FILTER:
Proud to be a Super User!
Thanks for your quick reply, however I meant the number of consecutive days. I have now edited my initial post to make it a bit more clear.
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
47 | |
26 | |
19 | |
14 | |
10 |
User | Count |
---|---|
57 | |
49 | |
44 | |
18 | |
18 |