Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

Converting a VBA UDF into DAX

Hi experts,

 

Wondering if someone has already tried to convert a VBA function to DAX? 

 

Function Cover(Stock As Double, Sales As Range) As Double

s = Stock
c = 0

For Each sale In Sales.Cells
If s = 0 Then Exit For
If s >= Val(sale.Value) Then
c = c + 1
s = s - Val(sale.Value)
Else
c = c + s / Val(sale.Value)
s = 0
Exit For
End If
Next

If s > 0 Then c = 9999

Cover = c

End Function

 

Cheers,

 

Gilly

1 ACCEPTED SOLUTION
Anonymous
Not applicable

2 REPLIES 2
v-cherch-msft
Microsoft Employee
Microsoft Employee

Hi @Anonymous 

I'm not familiar with VBA Function.Could you explain more about your expected output?If you need further help,please follow the How to Get Your Question Answered Quickly to post your simple assumed data and expected output.It would be better if you can upload the .pbix file to OneDrive and post the link here. Do mask sensitive data before uploading.

Regards,

Community Support Team _ Cherie Chen
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Anonymous
Not applicable

Thanks! Have found this link which helped to get what I nedded...

 

https://community.powerbi.com/t5/Community-Blog/For-and-While-Loops-in-DAX/ba-p/636314

 

 

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.