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

Help translating VBA to Power Query

Hi, I want to create a dynamic query in Power BI. But I need to change my VBA in query to Power Query such that I can use it with a parameter

 

My original MDX query contains this piece of VBA:

 

Str(Int(CDbl(Now()) - 38718))

It takes todays date and time, converts it to a number, subtracts a value needed to get right row from cube, converts it to a whole number (integer) and then finally changes it to a string so that it can be used in the query.

 

I have tried to do the following in Power Query, without luck.

 

Value = Text.From(Int64.From(Number.From(DateTime.LocalNow()) - 38718))
1 ACCEPTED SOLUTION
jomikk
Helper I
Helper I

Figured out what I did wrong!

 

I used Power Query formula inside StrToMember (which expects VBA formulaes):

 

StrToMember("[Report Date].[Report Date].&[" + Str(Int(CDbl(Now()) - 38718)) + "]")

So I changed this to:

 

[Report Date].[Report Date].&["& Value &"]

And then my original Value formula worked as I expected.

 

Value = Text.From(Int64.From(Number.From(DateTime.LocalNow()) - 38718))

But I don't know if this is the "best" way to do it, but it works

View solution in original post

3 REPLIES 3
jomikk
Helper I
Helper I

Figured out what I did wrong!

 

I used Power Query formula inside StrToMember (which expects VBA formulaes):

 

StrToMember("[Report Date].[Report Date].&[" + Str(Int(CDbl(Now()) - 38718)) + "]")

So I changed this to:

 

[Report Date].[Report Date].&["& Value &"]

And then my original Value formula worked as I expected.

 

Value = Text.From(Int64.From(Number.From(DateTime.LocalNow()) - 38718))

But I don't know if this is the "best" way to do it, but it works

MattAllington
Community Champion
Community Champion

You want something like this

 

Duration.Days(DateTime.Date(DateTime.LocalNow())-#date(2015,1,1))



* Matt is an 8 times Microsoft MVP (Power BI) and author of the Power BI Book Supercharge Power BI.
I will not give you bad advice, even if you unknowingly ask for it.

Cheers Matt, can you explain what this is doing? Keep in mind that I want a number, not a date.

 

Here is the cube member for context:

 

StrToMember("[Report Date].[Report Date].&[" + Str(Int(CDbl(Now()) - 38718)) + "]")

So using this VBA code for today it would yield:

 

[Report Date].[Report Date].&[3502]

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.