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
Daniff
Helper I
Helper I

Vba To power bi

Hello, please help me. I want convert this vba code for use on power bi with dax. Please help me. Many thanks

 

Option Explicit

Public Function Trocar(Cat As String, Produto As String, PVP As Double, Rentab As Double) As String
'(c) Vlamir Pereira - jan2021
'www.youtube.com/c/excelfutura
Dim Lin As Long
Dim PVPLimite As Double
Dim PVPAtual As Double
Dim Trocou As Boolean
Trocou = False
Lin = 3
If Application.WorksheetFunction.CountIfs(Sheets("TABELA1").Range("B:B"), Produto) > 0 Then

Trocar = "Parceiro"
Exit Function
End If
Trocar = "NO SWITCH"
Cat = UCase(Cat)
PVPLimite = PVP + 1
PVPAtual = PVP
Do While Sheets("TABELA1").Cells(Lin, 1) <> ""
If UCase(Sheets("TABELA1").Cells(Lin, 1)) = Cat Then

If Sheets("TABELA1").Cells(Lin, 3) <= PVPLimite And Sheets("TABELA1").Cells(Lin, 4) > Rentab Then

Trocar = Sheets("TABELA1").Cells(Lin, 2)
Rentab = Sheets("TABELA1").Cells(Lin, 4)
PVPAtual = Sheets("TABELA1").Cells(Lin, 3)
Trocou = True
ElseIf Sheets("TABELA1").Cells(Lin, 3) <= PVPLimite And Sheets("TABELA1").Cells(Lin, 4) = Rentab And Trocou Then

If PVPAtual > PVP And PVPAtual > Sheets("TABELA1").Cells(Lin, 3) Then

Trocar = Sheets("TABELA1").Cells(Lin, 2)
Rentab = Sheets("TABELA1").Cells(Lin, 4)
PVPAtual = Sheets("TABELA1").Cells(Lin, 3)
ElseIf PVPAtual < PVP And PVPAtual < Sheets("TABELA1").Cells(Lin, 3) And PVP > Sheets("TABELA1").Cells(Lin, 3) Then

Trocar = Sheets("TABELA1").Cells(Lin, 2)
Rentab = Sheets("TABELA1").Cells(Lin, 4)
PVPAtual = Sheets("TABELA1").Cells(Lin, 3)
End If
End If
End If
Lin = Lin + 1
Loop
End Function

1 REPLY 1
Anonymous
Not applicable

Hi @Daniff,

I don't think you can use DAX expressions to reproduce the 'do while loop' part. (search value, if statement, compare value parts and output the result are supports)
Power bi data model table does not include the row and column index. For the reference operation to the specific cells, you need to add filters to reduce the result ranges to point to the specific values.
For the DAX iterator functions, they are looping on the parameter table rows instead of looping in the range with particular exit conditions. 

BTW, the recursive calculation also not supported in dax expressions.

Regards,

Xiaoxin Sheng

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.